刚刚学完如果用配置文件验证管理员登陆 感觉有点晕晕的 和之前用Session写登陆有些混肴了 所以写下来 总节一下 顺便清楚 一下头脑
<authentication mode="Forms"></authentication>是在客户端保存用户信息的方式并加密处理
例:
<authentication mode="Forms">
<forms loginUrl="Manage/Login.aspx" defaultUrl="Manage/AdminIndex.aspx"></forms>
</authentication>
注:配置文件中的节点属性相同的只能有一个哦
在<configuration></configuration>节点中添加一个权限控件
<configuration>
<!--后台登陆权权-->
<location path="Manage">
<system.web>
<authorization>
<allow users="Tsoft"/> //可以添加角色
<deny users="*"/>
</authorization>
</system.web>
</location>
</configuration>
user 或 roles一旦确定后就要在Global.asax中添加一个Application_AuthenticationRequest(object sender,EventArgs e){}事件,确保对authorization中的限制进行匹配,并通过验证
注:Application_AuthenticationRequest事件名超长,也不过不难记,但也不需专门去记,可以查MSDN哦
确保一切都完成后在登陆按钮后要添加这样一句代码 确保页面可以回传
FormsAuthentication.RedirectFromLoginPage(admin.AdminName, false);
false是不保存Cookie true是保存Cookie
admin.AdminName 是要在配置文件中进行验证的用户名
哈哈 简单吧 我也是刚刚学完刚刚总结的 如果有不足的地方 请大家 批评指证 我一定认真学习