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

Tags:

formsauthenticationticketASp.net 2.0 step by step Membership Provider Asp.net Interview Questions for 2 years or 3 years experianceasp net image uploadASP.net File HandlingWhat is web garden in Asp.net?Asp.net Convert String To ImageAsp.net Convert Image To Base64Asp.net open new windowHack asp.netAsp.net How To Write a Cookie and Read

Author

My name is Satalaj, but people call me Sat. Here is my homepage: . I live in Pune, PN and work as a Software Engineer. I'm former MVP in ASP.net year 2010.
Disclaimer: Views or opinion expressed here are my personal research and it has nothing to do with my employer. You are free to use the code, ideas/hints in your projects. However, you should not copy and paste my original content to other web sites. Feel free to copy or extend the code.
If you want to fight with me, this website is not for you.
 

I'm Satalaj.