Bryuk
Bryuk

Reputation: 3345

How to Authenticate user in ASP.NET

I have SQL Database with all my users. I want to use default login page of ASP.NET 4 WebSite Template. What should I change if user is past validation?

This is my Login button

protected void LoginButton_Click(object sender, EventArgs e)
{
    string userName = LoginUser.UserName;
    string password = LoginUser.Password;

    DBAccess dba = new DBAccess();
    DataTable dt = dba.GetUser(userName, password);

    if (dt.Rows.Count > 0)
    {
        Session["UserID"] = dt.Rows[0]["UserID"].ToString();
        string name = dt.Rows[0]["FirstName"].ToString() + " " + dt.Rows[0]["LastName"].ToString();
        FormsAuthentication.SetAuthCookie(userName, true);

    }
    else
    {
        LoginUser.FailureText = "Wrong Password or User Name";
    }
}

How to change HeadLoginStatus and how to set Authentication to True?

Thank you for any help!

Upvotes: 3

Views: 8245

Answers (2)

Bryuk
Bryuk

Reputation: 3345

Actually I realized, that Log in will work with any methods, that redirect you to another page. So I could use next variations:

FormsAuthentication.RedirectFromLoginPage(userName,yourRememberMeCheckBox.Checked);

Or

Response.Redirect("Default.aspx");

Or

Server.Transfer("Default.aspx");

All the time you will authorize user

Upvotes: 0

Majid
Majid

Reputation: 14253

To log user in,use this:

FormsAuthentication.RedirectFromLoginPage(userName,yourRememberMeCheckBox.Checked);

Upvotes: 2

Related Questions