Asp.net forms authentication

Asp.net forms authentication

How quickly, I can set forms authentication to allow access for only one user!

In web.config authentication section you need to add below code

<authentication mode="Forms">
   <forms defaultUrl="~/accounts/authuser.aspx" loginUrl="login.aspx" slidingExpiration="true" >
    <credentials>
     <user name="admin" password="password"></user>
    </credentials>
   </forms>
  </authentication>

You can see here I have added credentials tag and added admin user their with password as password.

Now, I will let him login to system useing below code


        if(!FormsAuthentication.Authenticate( txtUserName.Text , txtPassword.Text ))
        {
             FormsAuthentication.RedirectToLoginPage();
        }
        else
        {
            // FormsAuthentication.SetAuthCookie( txtUserName.Text, true);
            FormsAuthentication.RedirectFromLoginPage(txtUserName.Text,true)
        }

FormsAuthentication.Authonticate will validate the user against Forms credentials stored in web.config file.

If user is not valid, you can redirect him back to LoginPage
FormsAuthentication.RedirectToLoginPage();


FormsAuthentication.RedirectFromLoginPage(txtUserName.Text,true)

It will redirect the user from login page to authorised page and it will set authontication cookie at client side browser.

Satalaj

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Comments