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!
"
);
}
查看全文
相关阅读:
POJ 1797 Heavy Transportation (Dijkstra算法变形)
HDU 2883 kebab (最大流)
HDU 3338 Kakuro Extension (最大流)
简单的敏捷工具更受敏捷开发团队青睐
让敏捷工具在敏捷开发中发挥高效作用
看板工具和Scrum工具选择之惑!
敏捷开发如何在创业公司实施
八个垂手可得的Scrum敏捷开发工具
那些我们常用的scrum工具、敏捷开发工具
Scrum/Sprint敏捷开发方法.
原文地址:https://www.cnblogs.com/gwazy/p/158242.html
最新文章
Java课堂作业02
Java课堂作业01
读《大道至简》有感
ngin日志格式
JENKINS安装及新建用户,权限配置
jenkins出现的问题
redis
nginx做本地目录映射
Nginx配置SSL报错 nginx: [emerg] unknown directive "ssl"
SSL 认证之后,request.getScheme()获取不到https的问题记录
热门文章
nginx配置ssl证书实现https访问
Nginx设置Js、Css等静态文件的缓存过期时间
设置Linux shell超时自动退出
二分图匹配(匈牙利算法模板)
POJ 1062 昂贵的聘礼 (最短路)
POJ 2253 Frogger (求每条路径中最大值的最小值,Dijkstra变形)
【转】图的割点、桥与双连通分支
HDU 4725 The Shortest Path in Nya Graph (最短路)
POJ 1502 MPI Maelstrom (最短路)
POJ 3268 Silver Cow Party (最短路)
Copyright © 2011-2022 走看看