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

推荐订阅源

博客园_首页
H
Help Net Security
腾讯CDC
宝玉的分享
宝玉的分享
H
Hackread – Cybersecurity News, Data Breaches, AI and More
L
LangChain Blog
爱范儿
爱范儿
T
The Blog of Author Tim Ferriss
J
Java Code Geeks
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
MyScale Blog
MyScale Blog
Engineering at Meta
Engineering at Meta
N
Netflix TechBlog - Medium
D
Docker
V
V2EX
Last Week in AI
Last Week in AI
G
Google Developers Blog
IT之家
IT之家
C
Check Point Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
人人都是产品经理
人人都是产品经理
博客园 - 叶小钗
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
博客园 - 聂微东

博客园 - microtea

Jmeter多机联合产生负载 (转) 【转载】关于Java文件路径问题 - microtea - 博客园 安装CVSNT UTF-8的繁体与简体转换 - microtea - 博客园 利用MSSQL sp自制未公开的加密函数(转) wap开发的点小经验 no suitable driver java.sql.SQLException: [Microsoft][SQLServer 2000 Driver for JDBC][SQLServer]对象名 'xxx' 无效 关于临时表的个人经验 - microtea - 博客园 java的对像克隆 在Redhat9上安装Oracle 9.2 SQL中的单记录函数 常用javascript Java:int 和 String 互相转换的多种方法 JAVA:Duplicate name in Manifest: Class-Path 关于The underlying connection was closed ServerVariables 集合环境变量列表 初步的对防盗连接的方法的设想 在asp.net中对web.config内容的添加
Asp.net(C#)中基于Forms验证的角色(用户组)验证授权过程
microtea · 2005-03-13 · via 博客园 - microtea

本文已经假设你了解Forms验证的一般知识.
Asp.net中基于Forms验证的角色(用户组)验证授权,其实就是在一般的Forms验证上边加多一个名为UserDate的string内容,
大家可以分三步完成验证:
1,设置web.config

在这里大家要注意:
<allow roles="Admin" />
<deny users="*" />
的顺序,如果反来就谁也进不了了!

2,在login.aspx页面的验证

//定义角色
private void ibtLogin_Click(object sender, System.Web.UI.ImageClickEventArgs e)
  
{
     
int UserID = MyAuthentication(UserName,PassWord);//验证一般用户
     string userData = "Member";//获取角色字符串
     if(MyAdminAuthentication(UserID))//验证用户角色
     {  
      userData 
= "Admin,Member";
     }
 
     System.Web.Security.FormsAuthenticationTicket Ticket 
= new System.Web.Security.FormsAuthenticationTicket(1,UserID.ToString(),DateTime.Now,DateTime.Now.AddMinutes(30), true,userData) ; //建立身份验证票对象
     string HashTicket = System.Web.Security.FormsAuthentication.Encrypt (Ticket) ; //加密序列化验证票为字符串
     HttpCookie UserCookie = new HttpCookie(System.Web.Security.FormsAuthentication.FormsCookieName, HashTicket) ; //生成Cookie
     Context.Response.Cookies.Add (UserCookie) ; //输出Cookie
     
     
// 重定向到用户申请的初始页面  
     if(Context.Request["ReturnUrl"] != null)
     {
      Context.Response.Redirect(Context.Request["ReturnUrl"]) ; 
     }
     else
     {
      Context.Response.Redirect("Default.aspx");
     }

}



private int MyAuthentication(string UserName,string PassWord)
{
   
//验证一般用户
}


private bool MyAdminAuthentication(int UserID)
{
    
//验证用户角色
}


3,最后是Global.asax了:)

哈哈...这样一个基于Forms验证的角色(用户组)验证授权就完成了!^O^

参考文章:
http://www.howtodothings.com/ViewArticle.aspx?Article=31
http://www.cnblogs.com/wuchang/archive/2004/07/26/27474.aspx