Solved

Capture username and password at login

Posted on 2014-09-18
8
205 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
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 

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
 
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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Foolproof security solutions has become one of the key necessities of every e-commerce or Internet banking website. If you too own an online shopping site then its vital for you to equip your web portal with customer security features that can allow…
These days, all we hear about hacktivists took down so and so websites and retrieved thousands of user’s data. One of the techniques to get unauthorized access to database is by performing SQL injection. This article is quite lengthy which gives bas…
Wufoo.com provides powerful tools for surveying targeted groups, and utilizing data from completed surveys to find trends, discover areas of demand or customer expectation, and make business decisions on products or services.
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…

777 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