惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

L
LangChain Blog
博客园 - 司徒正美
美团技术团队
WordPress大学
WordPress大学
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
人人都是产品经理
人人都是产品经理
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
T
Troy Hunt's Blog
S
Schneier on Security
T
The Exploit Database - CXSecurity.com
P
Proofpoint News Feed
云风的 BLOG
云风的 BLOG
Engineering at Meta
Engineering at Meta
Cisco Talos Blog
Cisco Talos Blog
T
Tor Project blog
B
Blog
NISL@THU
NISL@THU
月光博客
月光博客
博客园 - 【当耐特】
AWS News Blog
AWS News Blog
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
腾讯CDC
L
Lohrmann on Cybersecurity
The Cloudflare Blog
L
LINUX DO - 最新话题
S
Security @ Cisco Blogs
S
Secure Thoughts
Spread Privacy
Spread Privacy
有赞技术团队
有赞技术团队
The Last Watchdog
The Last Watchdog
Project Zero
Project Zero
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Vercel News
Vercel News
H
Hacker News: Front Page
S
SegmentFault 最新的问题
Schneier on Security
Schneier on Security
aimingoo的专栏
aimingoo的专栏
P
Privacy & Cybersecurity Law Blog
博客园 - 三生石上(FineUI控件)
Forbes - Security
Forbes - Security
C
CXSECURITY Database RSS Feed - CXSecurity.com
I
InfoQ
T
Tailwind CSS Blog
Application and Cybersecurity Blog
Application and Cybersecurity Blog
G
GRAHAM CLULEY
W
WeLiveSecurity
小众软件
小众软件
Recorded Future
Recorded Future
Cyberwarzone
Cyberwarzone
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org

博客园 - torome

做开发这么多年,都没有一个自己的站? 我希望四年前就有人告诉我的事情 用php_screw加密PHP代码 gSoap下Server端接口函数的数据传出 gsoap简单实例 js的comet各个浏览器封装lib 原来CASTLE RC2 还不支持IBATIS 1.6,郁闷了很久 硬盘数据恢复原理与方法 用QQWry.Dat作数据源实现IP地址检索 缓存用户控件实例 URL重写 使用csc命令将.cs文件编译成.dll的过程 automation服务器不能创建对象 解决 ASP.NET 中文本框上按回车时系统缺省提交按钮的问题 ASP.NET生成静态HTML页面 sql server 2000 以前的某个程序安装已在安装计算机上创建挂起的文件操作解 马斯若需求层次论 运行VS.net2003出现"找不到一个或多个组件.请重新安装" 人生的35个好习惯
Forms验证中的roles
torome · 2007-03-01 · via 博客园 - torome

我们来看下Forms身份验证基本原理:
一 身份验证
要采用Forms身份验证,先要在应用程序根目录中的Web.config中做相应的设
置:
<authentication mode="Forms">
<forms name=".ASPXAUTH" loginUrl="login.aspx" timeout="30"
path="/"/>
</authentication>
其中<authentication mode="Forms"> 表示本应用程序采用Forms验证方
式。
<forms>标签中的name表示指定要用于身份验证的Cookie。默认是.ASPXAUTH,其实你可以用任何名字,这也就是你在本地硬盘上看到的cookie里面的前面的几个字.
Forms的验证过程如下:1,生成身份验证票,2,加密身份验证票.3,写回客户端,4,浏览器重新定向.其实这一系列的动作如果我们不用roles的话都是通过FormsAuthentication.RedirectFromLoginPage方法来完成了这一系列的工作任务.但是既然我们要使用roles授权,我们就不能够使用这个方法,而要分开来,一步步完成.
首先是创建身份验证票,首先我们看看FormsAuthenticationTicket类的一个构造函数:
public FormsAuthenticationTicket(
int version, //设为1
string name, //用户标示
DateTime issueDate, //Cookie 的发出时间, 设置为 DateTime.Now
DateTime expiration, //过期时间
bool isPersistent, //是否持久性(根据需要设置,若是设置为持久性,在发出
cookie时,cookie的Expires设置一定要设置)
string userData, //这里用上面准备好的用逗号分割的role字符串
string cookiePath // 设为”/”,这要同发出cookie的路径一致,因为刷新cookie
要用这个路径
);
最后个参数可以省略
FormsAuthenticationTicket Ticket = new FormsAuthenticationTicket
(1,”kent”,DateTime.Now, DateTime.Now.AddMinutes(30), false,UserRoles)

然后加密:

string hashTicket = FormsAuthentication.Encrypt(ticket);
//设置验证票cookie,第一个参数为cookie的名字,第二个参数为cookie的值也就是加密后的票
HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName,hashTicket);
//设置cookie的有效期是一个礼拜
cookie.Expires = DateTime.Now.AddDays(7);
//把cookie加进Response对象发生到客户端
Response.Cookies.Add(cookie);
//得到请求的url
string requestUrl = FormsAuthentication.GetRedirectUrl(FormsAuthentication.FormsCookieName,false);
//不要使用FormsAuthentication.RedirectFromLoginPage方法,因为这个方法会重写cookie
//重新定向到请求的url
Response.Redirect(requestUrl);

以后的各个页面中通过HttpContext.Current.User.Identity.Name判断用户标识,
  HttpContext.Current.User.IsInRole("Admin")判断用户是否属于某一角色(或某一组)]

/Files/torome/roleFrom.rar