asp.net Login Session

user6824563

I'm new in asp.net webform. I am trying to create a Login Session. I have my Login.aspx with a button. In the action of this button I execute the login:

if(username.Text == "" || password.Text == "")
    {
        ScriptManager.RegisterStartupScript(this, this.GetType(), "Pop", "$(function() { requiredLoginFields(); });", true);
    }
    else
    {
        var query = from u in db.Users
                    where u.Username == username.Text &&
                          u.Password == password.Text
                    select u;
        if(query.Count() > 0){
            Session["Logged"]="Yes";
            Session["User"]=username.Text;
            Server.Transfer("Home.aspx", true);
        }

        else
            // Working on it
    }

On every other page I verify the session in the Page_Load:

protected void Page_Load(object sender, EventArgs e)
{
    if(Session == null)
    {
        Response.Redirect("Login.aspx";)
    }
}

My questions. Is it the correct way to implement a login session? Do I need to check the session in every Page_Load of all my webforms?

Is there a way to load automatically my Home.aspx? I mean when I run the project I would like to open http://localhost:64716/ and it load my Home.aspx.

Thank you

Daniel Vinagre

Use Forms Authentication.

The .NET Framework will do most of the job for you.

In web.config you will configure that your site's pages are only acessible when logged in.

ASP.NET even has a user control for you to put in the login page and that will do the login part. Very few code needed.

Please read this How to: Implement Simple Forms Authentication

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related