zoukankan
html css js c++ java
基于角色的身份验证
web.config
<
authentication
mode
="Forms"
>
<
forms
name
="app"
loginUrl
="bb.aspx"
/>
</
authentication
>
<
authorization
>
<
deny
users
="?"
/>
</
authorization
>
Roles.xml
<?
xml version="1.0" encoding="utf-8"
?>
<
roles
>
<
user
name
="Bob"
roles
="Sales"
/>
<
user
name
="Jane"
roles
="Supervisor,Sales"
/>
</
roles
>
bb.aspx
private
void
Button1_Click(
object
sender, System.EventArgs e)
{
System.Web.Security .FormsAuthentication .RedirectFromLoginPage(
this
.TextBox1 .Text,
false
);
}
Global.asax
protected
void
Application_AuthenticateRequest(Object sender, EventArgs e)
{
string
strUserName;
XmlDocument objRoles;
XmlNode objNode;
string
strXPath;
objRoles
=
GetRoles();
if
( Context.Request.IsAuthenticated )
{
strUserName
=
Context.User.Identity.Name;
strXPath
=
string
.Format(
"
user[@name='{0}']
"
, strUserName );
objNode
=
objRoles.DocumentElement.SelectSingleNode( strXPath );
if
(objNode
!=
null
)
{
string
[] arrRoles
=
objNode.Attributes[
"
roles
"
].Value.Split (
new
char
[]
{
'
,
'
}
);
// 这很重要返回为 string[] 类型,要保证被分割.......
foreach
(
string
s
in
arrRoles)
{
this
.Response .Write (s
+
arrRoles.Length .ToString ());
}
Context.User
=
new
GenericPrincipal( Context.User.Identity, arrRoles);
}
}
}
XmlDocument GetRoles()
{
XmlDocument objRoles;
objRoles
=
(XmlDocument)Context.Cache[
"
Roles
"
];
if
( objRoles
==
null
)
{
objRoles
=
new
XmlDocument();
objRoles.Load( Server.MapPath(
"
Roles.xml
"
) );
Context.Cache.Insert(
"
Roles
"
, objRoles,
new
CacheDependency( Server.MapPath(
"
Roles.xml
"
) ) );
}
return
objRoles;
}
Default.aspx
if
( User.IsInRole(
"
Sales
"
) )
{
Response.Write(
"
You have Sales permissions!
"
);
//
User.Identity .AuthenticationType.ToString ();
}
if
(User.IsInRole (
"
Supervisor
"
))
{
Response.Write(
"
You have supervisor permissions!
"
);
}
查看全文
相关阅读:
Android开发笔记——WebView
字符串_最小表示法求循环串的最小序列(HDU_4162)
STL_map简单应用(HDU_1075)
DP_最大子序列和(HDU_1003)
STL map 使用方法(转)
数学_线性筛法建立素数表(HDU_1262)
學習筆記 ADO數據庫訪問技術
C#多线程学习
Java容器
选取单元格的基本语句
原文地址:https://www.cnblogs.com/gwazy/p/158242.html
最新文章
CentOS 5.5 编译安装mysql 5.5.18
Nginx正向代理
ESX 4 无法启动vSphere Web Access
搭建一个大型网站架构的实验环境(Nginx代理服务器篇)
在nginx中配置如何防止直接用ip访问服务器web server及server_name特性讲解
打印服务重启
如何利用Nginx架设Http代理服务器
如何给VSFTP增加用户,只能访问指定目录
nginx 正向代理的问题
CISCO动态VLAN配置
热门文章
一个简单的算法,定义一个长度为n的数组,随机顺序存储1至n的的全部正整数,不重复。
Android开发笔记——android:process=":remote"
Android开发笔记——Uri.parse的详细资料
Android开发笔记——PendingIntent
Android开发笔记——权限大全
Android开发笔记——线程的start和run方法
Android开发笔记——Itent.ACTION_PICK Intent.ACTION_GET_CONTENT妙用
Android开发笔记——Activity与Task
Android开发笔记——Matrix变换
Android开发笔记——Bitmap
Copyright © 2011-2022 走看看