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

推荐订阅源

WordPress大学
WordPress大学
Spread Privacy
Spread Privacy
T
The Exploit Database - CXSecurity.com
Simon Willison's Weblog
Simon Willison's Weblog
P
Privacy & Cybersecurity Law Blog
L
LINUX DO - 热门话题
T
Threat Research - Cisco Blogs
T
Tenable Blog
TaoSecurity Blog
TaoSecurity Blog
Security Archives - TechRepublic
Security Archives - TechRepublic
AI
AI
P
Proofpoint News Feed
A
About on SuperTechFans
P
Privacy International News Feed
月光博客
月光博客
雷峰网
雷峰网
S
Secure Thoughts
博客园 - 叶小钗
博客园 - 聂微东
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Project Zero
Project Zero
The Cloudflare Blog
SecWiki News
SecWiki News
The Hacker News
The Hacker News
V
Vulnerabilities – Threatpost
罗磊的独立博客
A
Arctic Wolf
阮一峰的网络日志
阮一峰的网络日志
Know Your Adversary
Know Your Adversary
酷 壳 – CoolShell
酷 壳 – CoolShell
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
T
Troy Hunt's Blog
The Last Watchdog
The Last Watchdog
Schneier on Security
Schneier on Security
小众软件
小众软件
有赞技术团队
有赞技术团队
博客园 - 司徒正美
T
Tailwind CSS Blog
量子位
C
Cybersecurity and Infrastructure Security Agency CISA
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Hugging Face - Blog
Hugging Face - Blog
人人都是产品经理
人人都是产品经理
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
S
Security @ Cisco Blogs
大猫的无限游戏
大猫的无限游戏
S
SegmentFault 最新的问题
Apple Machine Learning Research
Apple Machine Learning Research
宝玉的分享
宝玉的分享
L
Lohrmann on Cybersecurity

博客园 - wdx2008

ORM框架 sql语句字符串处理大全 - wdx2008 - 博客园 收集的ASP.NET中常用正则表达式 - wdx2008 - 博客园 web.config详解 教你合理设计数据表,将优化进行到底 为你的mail server增加SPF记录 马云在《赢在中国》对创业者的经典点评 给不同文件夹设置主题并指定其编码 - wdx2008 - 博客园 在线支付类封装 - wdx2008 - 博客园 SET XACT_ABORT各种用法及显示结果 ASP.NET邮件外发 asp.net Request.ServerVariables 各参数说明集合 - wdx2008 sql清除事务日志命令 js不允许单独打开left.aspx或top.apsx - wdx2008 - 博客园 ASP.NET2.0 TreeView的数据库绑定 - wdx2008 - 博客园 Forms 验证进行角色控制全攻略 收藏的一些ASP.net 2.0资料 关于asp.net 2.0的用户、角色管理总结(转) - wdx2008 - 博客园 開源圖表控件ZedGraph
forms验证:怎么验证两种身份?
wdx2008 · 2007-06-19 · via 博客园 - wdx2008

    

  一、问题描述:有两种用户,买方,卖方;在数据库中分别有两个用户表。要求:网站根目录下有两个目录假设为DW目录、GR目录。目录下都  
   
  有一个login.aspx文件,分别是dwlogin.aspx,grlogin.aspx,登录时分别用自己的login.aspx文件,登录后不能访问对方的目录。  
   
  二、总体思路:创建自定义的身份验证票据,将roles信息放在票据的userData中.  
   
  三、设置Web.config  
  <configuration>  
  <system.web>  
  <authentication   mode="Forms">  
  <forms   name=".ASPXAUTH"  
  loginUrl="login.aspx"//根目录中的,就是下面的第六条  
  protection="All"  
  path="/"/>  
  </authentication>  
                    </system.web>  
  <location   path="DW">  
  <system.web>  
  <authorization>  
  <allow   roles="DW"/>  
  <deny   users="*"   />//这里必须用*,用?达不到要求  
  </authorization>  
  </system.web>  
  </location>  
  <location   path="GR">  
  <system.web>  
  <authorization>  
  <allow   roles="GR"/>  
  <deny   users="*"   />  
  </authorization>  
  </system.web>  
  </location>  
  <location   path="/DW/DWLogin.aspx">  
  <system.web>  
  <authorization>  
  <allow   users="*"   />  
  </authorization>  
  </system.web>  
  </location>  
  <location   path="GR/GRLogin.aspx">  
  <system.web>  
  <authorization>  
  <allow   users="*"   />  
  </authorization>  
  </system.web>  
  </location>  
  四、DWLogin.aspx中  
   
  private   void   Button1_Click(object   sender,   System.EventArgs   e)  
  {  
  HttpCookie   objCookie;  
  string     strReturnURL;  
  if   (IsValid)    
  {  
  switch   (GetUser(   Username.Text,Password.Text))//   GetUser函数得到用户名和密码,正确返回0,密码错返回2  
   
  ,用户名错返回1  
  {  
  case   0:  
  FormsAuthenticationTicket   ticket   =   new   FormsAuthenticationTicket(  
  1,    
  Username.Text,    
  DateTime.Now,    
  DateTime.Now.AddMinutes(30),    
  false,    
  "DW");//我是直接写进去的,可能还有更好的方法  
  objCookie   =   new   HttpCookie(   =(".ASPXAUTH"   );  
  objCookie.Value   =   FormsAuthentication.Encrypt(   ticket   );  
  Response.Cookies.Add(   objCookie   );  
  strReturnURL   =   Request.Params[   "ReturnURL"   ];  
  if   (   strReturnURL   !=   null   )    
  {  
  Response.Redirect(   strReturnURL   );//返回到原来访问的页面  
  }    
  else    
  {  
  Response.Redirect(   "Default.aspx"   );//这里也可以设置你想设置的页面  
  }  
  break;  
  case   1:  
  this.Page.RegisterStartupScript("Info","<script>alert('该用户不存在!!');</script>");  
  break;  
  case   2:  
  this.Page.RegisterStartupScript("Info","<script>alert('密码错误!!');</script>");  
  break;  
  }  
  }  
  }  
   
  个人的登录页面GRLogin.aspx中写法同上面  
   
   
   
  五、Global.asax中添加如下代码:  
   
  protected   void   Application_AuthenticateRequest(Object   sender,   EventArgs   e)  
  {  
  if   (HttpContext.Current.User   !=   null)  
  {  
  if   (HttpContext.Current.User.Identity.IsAuthenticated)  
  {  
  if   (HttpContext.Current.User.Identity   is   FormsIdentity)  
  {  
  FormsIdentity   id   =(FormsIdentity)HttpContext.Current.User.Identity;  
  FormsAuthenticationTicket   ticket   =   id.Ticket;  
  string   userData   =   ticket.UserData;  
  string[]   roles   =   userData.Split(',');  
  HttpContext.Current.User   =   new   GenericPrincipal(id,   roles);  
  }  
  }  
  }  
  }  
   
  六、根目录下建login.aspx,其中添加如下代码:(这个login.aspx没有用户名和密码输入框,主要是用来重定位各自的登录页面)  
   
  private   void   Page_Load(object   sender,   System.EventArgs   e)  
  {  
  string   ParamName   =   "ReturnUrl";  
  string   ReturnUrl   =   Request.QueryString[ParamName];  
  if   (ReturnUrl.ToLower().IndexOf("DW")   >=0)  
  {  
  Response.Redirect("DW/DWLogin.aspx?"   +   ParamName     +   "="   +   ReturnUrl);  
  }  
  else   if   (ReturnUrl.ToLower().IndexOf("GR")   >=0   )  
  {  
  Response.Redirect("GR/GRLogin.aspx?"   +   ParamName     +   "="   +   ReturnUrl);  
  }  
  else  
  {  
  //这里添加一些你的提示,指定页面也可以  
  }  
  }