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!
"
);
}
查看全文
相关阅读:
ElasticSearch 2 (1)
Vagrant (2) —— 基本安装与配置(下)
Vagrant (1) —— 基本安装与配置(上)
Vagrant (3) —— 复制/备份Vagrant Box
vue中$forceUpdate的使用
vue+ElementUi 选择框选中之后翻页进行状态保持及默认选中
loonflow 工单系统
一些后端知识
前端学习计划
async/await函数的执行顺序的理解
原文地址:https://www.cnblogs.com/gwazy/p/158242.html
最新文章
python的测试
python的迭代器、生成器、装饰器
python的Virtualenv
Python 图片转字符画
CSS 让 fontawesome 图标字体变细
Sass预定义一些常用的样式
移动端reset
选择器优先级的权重计算
关于伪类选择器中一个冒号和两个冒号的区别
画一条0.5px的边
热门文章
未知宽高元素怎么上下左右垂直居中
backface-visibility 属性
css布局-多行文字垂直居中
让google.com不跳转到google.com.hk
CAS (11) —— CAS TicketRegistry使用Ehcache的集群方案
Redis (1) —— 安装
ElasticSearch 2 (5)
ElasticSearch 2 (4)
ElasticSearch 2 (3)
ElasticSearch 2 (2)
Copyright © 2011-2022 走看看