Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Capture username and password at login

Posted on 2014-09-18
8
Medium Priority
?
212 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 35

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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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 35

Accepted Solution

by:
it_saige earned 2000 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 35

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

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

Question has a verified solution.

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

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
This video teaches users how to migrate an existing Wordpress website to a new domain.
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.

916 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