Solved

Capture username and password at login

Posted on 2014-09-18
8
209 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 34

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
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 

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 34

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 34

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

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!

Question has a verified solution.

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

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…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Use Wufoo, an online form creation tool, to make powerful forms. Learn how to selectively show certain fields based on user input using rules to gather relevant information and data from your forms. The rules feature provides you with an opportunity…
Learn how to set-up PayPal payment integration in your Wufoo form. Allow your users to remit payment through PayPal upon completion of your online form. This is helpful for collecting membership payments, customer payments, donations, and more.

628 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