您的位置:首页 > 其它

使用 Forms 身份验证的一点点心得

2010-01-28 15:44 288 查看
如果采用 Forms 验证,当用户通过验证后 Context.User.Identity 或 Page.User.Identity 是一个 System.Security.Principal.IIdentity

System.Security.Principal 命名空间定义表示代码在其中运行的安全上下文的用户对象。

未验证:

Context.User: System.Security.Principal.GenericPrincipal,实现 System.Security.Principal.IPrincipal 接口,表示一般用户

Context.User.Identity: System.Security.Principal.GenericIdentity,实现 System.Security.Principal.IIdentity 接口,表示一般用户

Forms 验证后:

Context.User: 同与未验证

Context.User.Identity:
System.Web.Security.FormsIdentity,实现
System.Security.Principal.IIdentity 接口,表示一个使用 Forms 身份验证进行了身份验证的用户标识。

可以根据需要创建自己的用户对象(Principal),需要实现 System.Security.Principal.IPrincipal 接口。

验证后,可以用 FormsAuthentication.SetAuthCookie("UserName", true); 给以
UserName 为用户创建一个票据,事实上是创建一个
Cookies,创建之后,用户在访问那些需要通过用户验证才能访问的页时就不用每次提供票据了(也就是不用每次都输入用户名和密码去登录)。此
Cookies 的过期时间在 web.config 里设置如:

<authentication mode="Forms">

<forms name="GLUserAuth" loginUrl="Login.aspx" timeout="10" />

</authentication>

如以上设置,若用户在 10 分钟内未对应用程序进行任何操作,此票据(cookies)将自动失效。

怎样设置哪些页需要用户通过身份验证后才能访问,用 <location> 配置节:

<location path="EditUser.aspx">

<system.web>

<authorization>

<deny users="?" />

</authorization>

</system.web>

</location>

当然啦,如果所有页都需要就不是这样设置的了。最后可以用:FormsAuthentication.SignOut() 方法从浏览器删除 Forms 身份验证票证。

清水’s Blog
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: