一、安全验证方式
1.Windows: IIS验证,在内联网环境中非常重要。
2.Passport: 微软集中式验证,一次登录便可访问所有成员站点,需要收费。
3.Form:窗体验证,验证帐号/密码,Web编程最佳最流行的验证方式。
4.None:表示ASP.NET自己根本不执行身份验证,完全依赖IIS身份验证。
二、Form验证:
使用表单验证的两个重要属性LoginUrl和DefaultUrl,分别设置了默认的登录页面和登录跳转的页面。
Web.config的代码
<system.web>
<!--身份验证标签-->
<authentication>
<!--表单验证标签-->
<forms name="admin" loginUrl="url" defaultUrl="url">
<!--身份证书标签, passwordFormat指明了密码加密格式, 需要填入对应格式转化后的密码-->
<credentials passwordFormat="MD5">
<user name="" password="">
</credentials>
</forms>
</authentication>
<!--授权标签, 指明了可以访问到目录的授权用户-->
<authorization>
<!--allow表示授权的用户,deny表示无授权用户,?表示匿名用户,*表示全体用户-->
<allow users="···" />
<deny users="?,*" />
</authorization>
</system.web>
<!--通过location标签指定添加的用户访问某些文件夹目录, path指定了访问的目录url-->
<location path="">
<system.web>
<authorization>
<allow users="" />
<deny users="" />
</authorization>
</system.web>
</location>
Web.config文件配置好后就可以通过实现登录方法访问页面了。
//添加using指令
using System.Web.Security;
登录点击按钮方法实现:
protected void BtnClick(object sender, EventArgs e) {
if(FormsAuthentication.Authenticate(name, pwd) == true) {
//第二个参数为false,则cookie将在页面关闭后就失效
FormsAuthentication.RedirectFromLoginPage(name, true);
//验证用户登录状态,执行后用户状态将变成已登录
//FormsAuthentication.SetAuthCookie(name, false);
//if(Request.IsAuthenticate) {
// Response.Redirect("url");
//}
}
}