0

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

2
  • 1
    Try understanding forms authentication and how it works by going thru support.microsoft.com/en-us/help/301240/… Commented Feb 25, 2017 at 18:09
  • 1
    Is there a reason you are not using MVC? It makes the programming and testing of your code so much more maintainable. Commented Feb 25, 2017 at 18:15

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.