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

推荐订阅源

Forbes - Security
Forbes - Security
GbyAI
GbyAI
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
S
SegmentFault 最新的问题
Y
Y Combinator Blog
Recorded Future
Recorded Future
博客园 - Franky
I
InfoQ
T
The Blog of Author Tim Ferriss
Recent Announcements
Recent Announcements
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
博客园_首页
阮一峰的网络日志
阮一峰的网络日志
T
Tailwind CSS Blog
Cyberwarzone
Cyberwarzone
The Register - Security
The Register - Security
H
Hackread – Cybersecurity News, Data Breaches, AI and More
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
雷峰网
雷峰网
P
Palo Alto Networks Blog
G
GRAHAM CLULEY
Cloudbric
Cloudbric
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
MongoDB | Blog
MongoDB | Blog
F
Full Disclosure
Google DeepMind News
Google DeepMind News
Recent Commits to openclaw:main
Recent Commits to openclaw:main
C
Check Point Blog
爱范儿
爱范儿
The GitHub Blog
The GitHub Blog
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
W
WeLiveSecurity
T
Threat Research - Cisco Blogs
U
Unit 42
N
Netflix TechBlog - Medium
The Cloudflare Blog
Spread Privacy
Spread Privacy
Microsoft Azure Blog
Microsoft Azure Blog
美团技术团队
T
Troy Hunt's Blog
Engineering at Meta
Engineering at Meta
H
Heimdal Security Blog
TaoSecurity Blog
TaoSecurity Blog
C
Cybersecurity and Infrastructure Security Agency CISA
T
Tenable Blog
B
Blog
S
Securelist
H
Hacker News: Front Page
Google Online Security Blog
Google Online Security Blog
G
Google Developers Blog

博客园 - 磊

淘宝店铺开业送优惠券 报表统计查询 Word 发布测试 【原创】hibernate中delete的一点见解 关于23种设计模式的有趣见解[转] 用Visual C#调用Windows API函数 DataGrid,GridView和DetailsView中添加删除确认提示 2.0新控件 Localize ASP.NET直接下载一个文件 - 磊 - 博客园 dFastlog.dll错误和数据库连接错误的解决办法 "Automation 服务器不能创建对象" 的解决方法 - 磊 "FSO"的禁用与启用 如何做最简单的url跳转 我的主页 更换SQL对象属主的方法 如何在WinXP中批量修改文件名? Nokia 中的暂停功能 利用Forms实现两种不同验证系统 改版网站真麻烦
Froms验证
· 2005-08-25 · via 博客园 - 磊

第一次使用基于Froms的验证.首先假设用户登陆成功(这个一般从数据库得到验证).
然后写入验证票据Authentication.以后的页面中判断这个用户是否通过验证,如果没有,重定向到用户登陆页面.如果已经登陆,则执行业务逻辑.本文重点在讨论Authentication在角色验证中的使用.对其他方面不与关注.
步骤如下:

一.在用户登陆按扭事件中加入以下代码:

这段代码主要是在用户登录后写入Cookie.
private void Button1_Click(object sender, System.EventArgs e)
  {
   string username=TextBox1.Text.Trim();
   string roles="Admin";

   //生成验证票据对象.
   FormsAuthenticationTicket authTicket=new FormsAuthenticationTicket(
    1,
    username,
    DateTime.Now,DateTime.Now.AddMinutes(20),
    false,
    roles);
      //加密验证票
   string encrytedTicket=FormsAuthentication.Encrypt(authTicket);
   //生成Cookie对象.
   //FormsAuthentication.FormsCookieName取得WebConfig中<Authentication>
   //配置节中Name的值作为Cookie的名字.
   HttpCookie authCookie=new HttpCookie(FormsAuthentication.FormsCookieName,
    encrytedTicket);
   Response.Cookies.Add(authCookie);
      
   //跳转到用户的初试请求页.
   Response.Redirect(FormsAuthentication.GetRedirectUrl(username,false));
   
  }
二.在Global.asax.cs 的Application_AuthenticateRequest事件中添加如下代码:
//获取用户的角色。
   string cookieName=FormsAuthentication.FormsCookieName;//从验证票据获取Cookie的名字。
           
   //取得Cookie.
   HttpCookie authCookie=Context.Request.Cookies[cookieName];

   if(null == authCookie)
   {
    return;
   }
      FormsAuthenticationTicket authTicket = null;
           
   //获取验证票据。
   authTicket = FormsAuthentication.Decrypt(authCookie.Value);

   if(null == authTicket)
   {
    return;
   }

   //验证票据的UserData中存放的是用户角色信息。
   //UserData本来存放用户自定义信息。此处用来存放用户角色。
   string[] roles = authTicket.UserData.Split(new char[] {','});

   FormsIdentity id=new FormsIdentity(authTicket);

   GenericPrincipal principal=new GenericPrincipal(id,roles);

   //把生成的验证票信息和角色信息赋给当前用户.
   Context.User=principal;
三.以后的各个页面中通过HttpContext.Current.User.Identity.Name判断用户标识,
  HttpContext.Current.User.IsInRole("Admin")判断用户是否属于某一角色(或某一组)

四.WebConfig的修改:
 <authentication mode="Forms" >
    <forms name="MyAppFormsAuth" loginUrl="LoginTest.aspx" protection="All" timeout="20" path="/"></forms>
    </authentication>
上面这段大家都明白的了,我不多说了.主要是下面这段配置.
如果你在虚拟目录中有一个文件夹.这个文件夹只能由某些组访问.那么就可以向下面这样设置.
<!-- 设置对本地目录的访问。如果验证票据未通过,则无法访问
 -->
 <location path="Admin">
  <system.web>
   <authorization>
    <!-- Order and case are important below -->
    <allow roles="Admin"/>
    <deny users="*"/>
   </authorization>
  </system.web>
 </location>

这样,你就可以使用基于角色的窗体身份验证了.
我的一点浅见,共享出来,欢迎大家批评.