使用FormsAuthentication
来源:互联网 发布:云计算应用市场 编辑:程序博客网 时间:2024/05/16 23:59
FormsAuthentication是微软为RBAC(基于角色的访问控制)提供的一个重要类,如何使用这么一个类?
这里先要介绍一下配置节
首先要把站点主Web.config 中的配置节改为<authentication mode="Forms">
其次在需要控制访问的目录下建立Web.config
<system.web>
<authorization >
<deny users="?"/>
</authorization>
</system.web>
在Web.config里面加入这么一个节点后 Web.config所在控制域的页面就不能被未登录用户访问
在使用了自定义的认证方法后 设置验证票就可以访问了
代码如下
if(DataBaseCheck(userid,pwd))
FormsAuthentication.SetAuthCookie(userid);
DataBaseCheck是我们自己的验证法则,对参数用户名和密码做验证,通过则返回true
如果需要登录回转的话可以用FormsAuthentication.RedirectFromLoginPage(userid,false);
其实如果要有更细粒度的控制 可以这么来
FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(userid, false, 3000);
Response.Cookies[FormsAuthentication.FormsCookieName].Value = FormsAuthentication.Encrypt(ticket);
数字3000为登录timeout数字
如果文件夹没有配置Web.config的authorization,那么在登录后的页面里 判定是否认证可以通过User.Identity.IsAuthenticated
登录后的userid可以通过User.Identity.Name获得。
如果需要在页面保存更多user信息 可以做一个UserProfile类在数据库获取,假设UserInfo为用户信息实体,
class UserProfile
{
public UserInfo LoginUserInfo
{
get
{
UserInfo user = Session["userinfo"] as UserInfo
if(user == null)
{
user = BizLayer.GetUserInfo(userid);
Session["userinfo"] = user
}
return user
}
}
- 使用FormsAuthentication
- FormsAuthentication 简单使用
- FormsAuthentication.SetAuthCookie函数使用无效
- FormsAuthentication.SetAuthCookie函数使用无效
- 使用FormsAuthentication进行身份验证的优缺点
- FormsAuthentication与Session同时使用时的问题
- FormsAuthentication 类
- FormsAuthentication,response
- FormsAuthentication.RedirectFromLoginPage
- FormsAuthentication.SetAuthCookie
- FormsAuthentication使用指南
- FormsAuthentication使用指南
- FormsAuthentication.SetAuthCookie
- FormsAuthentication使用指南
- FormsAuthentication.SetAuthCookie
- FormsAuthentication.SetAuthCookie
- FormsAuthentication.SignOut(); FormsAuthentication.SetAuthCookie(u.UserName, false);
- FormsAuthentication.SignOut()问题
- 我爱你
- 飞秋问题观
- 二是方便其“飞鸽下载”人看
- 系统分区及挂载点(转)
- 多线程编程(14) - 多线程同步之 WaitableTimer (等待定时器对象)
- 使用FormsAuthentication
- 易名网——中小企业管家式“银行”落地岛城
- epoll
- 对于vistual studio 不能输入汉字的问题的解决方法
- ubuntu10.04下virtualbox使用usb设备
- 教育的结晶
- 函数:strrchr()
- windows与linux字符集转换
- 客户名单