Solved

Capture username and password at login

Posted on 2014-09-18
8
203 Views
Last Modified: 2014-09-25
Dear experts,
I need to do what I think should be a very easy task but cannot get it to work. I need to capture the username and password at login. I have tried several suggestions found through a Google search but they didn't seem to work. The way I'm testing it is to try and capture the Username and Password.
Assign them to a variable.
Send them to session.
Redirect to a simple, clean white page with two labels.
Assign the session variable to the text of the label.
I can't get anything to show up in the labels.

I'm using the Login control from VS 2012, C#.
Below is the code from the back page of the login page and the back page of the page with the labels on it. If you need anything else, let me know.

Thanks in advance.

Login page
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class EdSrvcs_Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Session["UNSession"] = "";
        Session["PWDSession"] = "";
    }

    private void OnAuthenticated(object sender, EventArgs e)
    {
        string UN = Login1.UserName;
        Session["UNSession"] = UN;
        string PWD = Login1.Password;
        Session["PWDSession"] = PWD;
    }
    protected void LoginUser_LoggedIn(object sender, EventArgs e)
    {
    }

}

Label Page
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using MySql.Data;
using MySql.Data.MySqlClient;
using System.Globalization;
using System.Security;
using System.Data.SqlClient;
using System.Data;


public partial class EdSrvcs_CleanWhite : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {  
        lblFirstName.Text = (string)Session["UNSession"];
        lblLastName.Text = (string)Session["PWDSession"];

    }
}
0
Comment
Question by:bobbellows
  • 4
  • 3
8 Comments
 
LVL 12

Expert Comment

by:Ramkisan Jagtap
ID: 40330116
Try adding !IsPostBack on page load event:

public partial class EdSrvcs_Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
       if(!IsPostBack)
       {
        Session["UNSession"] = "";
        Session["PWDSession"] = "";
        }
    }

    private void OnAuthenticated(object sender, EventArgs e)
    {
        string UN = Login1.UserName;
        Session["UNSession"] = UN;
        string PWD = Login1.Password;
        Session["PWDSession"] = PWD;
    }
    protected void LoginUser_LoggedIn(object sender, EventArgs e)
    {
    }

Open in new window

0
 

Author Comment

by:bobbellows
ID: 40336392
Ramkisan Jagtap,
Thank you for your reply.b I'm sorry for it being so long to get back to you. I'm a school teacher by day and trying to create a site for some teachers at night. We go through a grading period (they come every 3 weeks) so I couldn't get back to this.

I added  the !IsPostBack as you suggested. I'm still not getting anything in Session. When I go to the basic page with the labels nothing shows up. It's as if nothing is getting sent to session..

I moved the code section to the LoginUser_LoggedIn declaration but that didn't help. How hard can it be to get the login and password? What am I missing? Thanks.
Bob Bellows
0
 
LVL 33

Expert Comment

by:it_saige
ID: 40340203
What is bound to the OnAuthenticated method?

If you are certain the OnAuthenticated is being called, then you will probably want to change the method as such:

	private void OnAuthenticated(object sender, EventArgs e)
	{
		Session["UNSession"] = Login1.UserName;
		Session["PWDSession"] = Login1.Password;
		Response.Redirect("~/EdSrvcs_CleanWhite.aspx");
	}

Open in new window


You can also get more information here:

http://www.thegeekyway.com/sessions-in-asp-net/

-saige-
0
 

Author Comment

by:bobbellows
ID: 40341382
it_saige
You are correct. The OnAuthenticated is not being called. I commented out the "Sessions" statements only leaving the Redirect and nothing happened. Why would that be? All I want is at the end of the login process to capture the username and password and then send it to session to use later. The reason I used OnAuthenticated is because I didn't want to run the method if the person hadn't logged in yet. I also tried the "LoggedIn" and it also appears is not being called. I'm somewhat new to this and can't figure it out. I think I'm making it harder than it is.

I went to the page you suggested. This is basically what I'm looking for.  I'm using the Login Control. Would I have to convert the Control to a Template in order to access the text boxes?

Bob
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 33

Accepted Solution

by:
it_saige earned 500 total points
ID: 40341564
Well using the LoginControl in this instance is overkill, however, that does not mean that you cannot use it.

Here are probably the changes that you would need to make:

EdSrvcs_Default.aspx.cs:
public partial class EdSrvcs_Default : System.Web.UI.Page
{
	protected void Page_Load(object sender, EventArgs e)
	{
		if (!IsPostBack)
		{
			Session["UNSession"] = "";
			Session["PWDSession"] = "";
		}
	}

	protected void OnAuthenticate(object sender, AuthenticateEventArgs e)
	{
		Session["UNSession"] = Login1.UserName;
		Session["PWDSession"] = Login1.Password;

		e.Authenticated = true;
	}

	protected void OnLoggedIn(object sender, EventArgs e)
	{
		Response.Redirect("~/EdSrvcs_CleanWhite.aspx");
	}
}

Open in new window


EdSrvcs_Default.aspx:
<html xmlns="http://www.w3.org/1999/xhtml">
	<head runat="server">
		<title></title>
	</head>
	<body>
		<form id="form1" runat="server">
			<div>
				<asp:Login ID="Login1" runat="server" onauthenticate="OnAuthenticate" onloggedin="OnLoggedIn"></asp:Login>
			</div>
		</form>
	</body>
</html>

Open in new window


EdSrvcs_CleanWhite.aspx.cs:
public partial class EdSrvcs_CleanWhite : System.Web.UI.Page
{
	protected void Page_Load(object sender, EventArgs e)
	{
		lblFirstName.Text = Session["UNSession"].ToString();
		lblLastName.Text = Session["PWDSession"].ToString();
	}
}

Open in new window


-saige-
0
 

Author Comment

by:bobbellows
ID: 40343169
saige
That worked! Thanks so much. You mentioned that using the login control was "overkill". I'm trying to learn the best practices. Can you clarify for me what would have been a better, cleaner way to get this to occur.
Thanks.
0
 

Author Closing Comment

by:bobbellows
ID: 40343171
it-saige was awesome. He read carefully what I was trying to accomplish and address the issue at hand. When the first proposed solution didn't work he took a second look and made a perfect suggestion to solve my problem. Thank you -saige-.
0
 
LVL 33

Expert Comment

by:it_saige
ID: 40343808
What I meant by overkill was that the Login control is usually used when authenticating users.  The user enters in their username and password which is then is validated against a datasource of sometype (i.e. - database, encrypted file, etc. etc.).

As no authentication is required in this particular situation, just a couple of text boxes and a button would have probably sufficed.

However, it is still a good learning experience.  And who's to say that you ultimately do not plan on using an authentication method later on.

Glad we got it worked out for you and happy coding.  ;)

-saige-
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
What is Thread.Sleep? 5 46
C# Json POSt to Rest API 4 37
Adware on IIS hosted asp.net website 1 17
Where can I ask about my ASP.NET MVC project? 6 21
If I have to fix slow responding website my first thoughts are server side optimizations: the database may not be optimized or caching is not enabled, or things like that. We often overlook another major part of our web application: the client. We o…
What is Node.js? Node.js is a server side scripting language much like PHP or ASP but is used to implement the complete package of HTTP webserver and application framework. The difference is that Node.js’s execution engine is asynchronous and event…
This video teaches viewers how to create their own website using cPanel and Wordpress. Tutorial walks users through how to set up their own domain name from tools like Domain Registrar, Hosting Account, and Wordpress. More specifically, the order in…
Use Wufoo, an online form creation tool, to make powerful forms. Learn how to choose which pages of your form are visible to your users based on their inputs. The page rules feature provides you with an opportunity to create if:then statements for y…

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now