会员
周边
新闻
博问
闪存
众包
赞助商
Chat2DB
所有博客
当前博客
我的博客
我的园子
账号设置
会员中心
简洁模式
...
退出登录
注册
登录
ReturnKing
博客园
首页
新随笔
联系
订阅
管理
Forms认证中cookie,角色管理,访问权限
Code
1
FormsAuthenticationTicket ticket
=
new
FormsAuthenticationTicket(
1
,
"
jcj
"
, DateTime.Now, DateTime.Now.AddDays(
1
),
true
,
"
taxt
"
);
2
string
encryptedticked
=
FormsAuthentication.Encrypt(ticket);
3
HttpCookie cookie
=
new
HttpCookie(FormsAuthentication.FormsCookieName,encryptedticked);
4
Response.Cookies.Add(cookie);
5
Response.Redirect(
"
WebForm2.aspx
"
);
Code
1
protected
void
Application_AuthenticateRequest(
object
sender, EventArgs e)
2
{
3
HttpApplication app
=
(HttpApplication)sender;
4
HttpCookie cookie
=
Request.Cookies[FormsAuthentication.FormsCookieName];
5
if
(cookie
!=
null
)
6
{
7
string
decryptedticket
=
cookie.Value;
8
FormsAuthenticationTicket ticket
=
FormsAuthentication.Decrypt(decryptedticket);
9
FormsIdentity identity
=
new
FormsIdentity(ticket);
10
string
[] role
=
new
string
[]
{ ticket.UserData}
;
11
System.Security.Principal.GenericPrincipal user
=
new
System.Security.Principal.GenericPrincipal(identity,role);
12
Response.Write(user.Identity.Name
+
user.IsInRole(
"
taxt
"
));
//
返回true
13
Response.Write(user.Identity.Name
+
user.IsInRole(
"
其他角色
"
));
//
返回false
14
Response.Write(
"
<br/>
"
);
15
Context.User
=
user;
16
}
17
}
posted @
2009-07-30 21:55
returnKing
阅读(
204
) 评论(
0
)
收藏
举报
刷新页面
返回顶部
公告