Asp.net简单实现forms验证

 1 <configuration>
 2     <system.web>
 3         <compilation debug="true" targetFramework="4.0" />
 4         <!--创建一个 authentication 元素,并将它的 mode 特性设置为 Forms-->
 5         <authentication mode="Forms">
 6             <!--设置为“Logon.aspx”。Logon.aspx 是 ASP.NET 在找不到包含请求内容的身份验证 Cookie 的情况下进行重定向时所使用的 URL-->
 7             <!--设置为“.ASPXFORMSAUTH”。 这是为包含身份验证票证的 Cookie 的名称设置的后缀-->
 8             <forms loginUrl="Login.aspx" name=".ASPXFORMSAUTH" timeout="2880"></forms>
 9         </authentication>
10         <authorization>
11             <!--创建一个 deny 元素,并将其 users 特性设置为“?”。 这是指定将拒绝未通过身份验证的用户(由“?”表示)访问该应用程序中的资源-->
12             <deny users="?"/>
13         </authorization>
14     </system.web>
15  
16 </configuration>
1  <!--不需要验证就能访问-->
2     <location path="About.aspx">
3         <system.web>
4             <authorization>
5                 <allow users="*"/>
6             </authorization>
7         </system.web>
8     </location>

后台代码:

1 if (username == "admin" && password == "123")//验证
2           {
3 System.Web.Security.FormsAuthentication.RedirectFromLoginPage(username, false);
4 }
1 // SignOut 方法以清除用户标识并移除身份验证票证 (Cookie)---注销
2 System.Web.Security.FormsAuthentication.SignOut();
3 Response.Redirect("Login.aspx");
原文地址:https://www.cnblogs.com/Mryjp/p/forms.html