Solved

Capture username and password at login

Posted on 2014-09-18
8
198 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
Comment Utility
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
Comment Utility
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 32

Expert Comment

by:it_saige
Comment Utility
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
Comment Utility
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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 32

Accepted Solution

by:
it_saige earned 500 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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 32

Expert Comment

by:it_saige
Comment Utility
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Foreword (May 2015) This web page has appeared at Google.  It's definitely worth considering! https://www.google.com/about/careers/students/guide-to-technical-development.html How to Know You are Making a Difference at EE In August, 2013, one …
Thoughout my experience working on eCommerce web applications I have seen applications succumbing to increased user demand and throughput. With increased loads the response times started to spike, which leads to user frustration and lost sales. I ha…
This video teaches users how to migrate an existing Wordpress website to a new domain.
Learn how to set-up custom confirmation messages to users who complete your Wufoo form. Include inputs from fields in your form, webpage redirects, and more with Wufoo’s confirmation options.

762 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

10 Experts available now in Live!

Get 1:1 Help Now