- Set related sections in Web. config:
<Authentication
Mode="Forms">
<Forms
Loginurl="Login. aspx"
Protection="All"
Timeout="30"
Name=". Aspxauth"
Path="/"
Requiressl="False"
Slidingexpiration="True"
Defaurl URL="Default. aspx"
Cookieless="Usedeviceprofile"
Enablecrossappredirects="False"/>
</Authentication>
- Put different kinds of files into different folders. This is because: before being visited, some files need users to login, while others needn't.
- Set related sections in Web. config to realize the function which is described in the 2nd point.
<System. Web>
...
<Authorization>
<Allow
Users="*"/>
</Authorization>
</System. Web>
<Location
Path="Admin">
<System. Web>
<Authorization>
<Deny
Users="?"/>
</Authorization>
</System. Web>
</Location>
<Location
Path="Admin/neednotlogin. aspx">
<System. Web>
<Authorization>
<Allow
Users="*"/>
</Authorization>
</System. Web>
</Location>
- Write codes in clicking event of the login button:
Protected
VoidBtnlogin_click (ObjectSender,EventargsE)
{
If(Validateuser (txtusername. Text, txtpassword. Text ))
{
HttpcookieAuthenticationcookie =Formsauthentication. Getauthcookie (txtusername. Text,True);
Authenticationcookie. expires =Datetime. Now. adddays (3 );// Three days.
Response. Cookies. Add (authenticationcookie );
Response. Redirect (Formsauthentication. Getredirecturl (txtusername. Text,True));
}
Else
{
Response. Write ("Invalid user! ");
}
}