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!
"
);
}
查看全文
相关阅读:
程序员书单
36条极简人生建议
Nacos
jvm详解
22种世界500强都在用的高效工作方法,你了解几种?
道德经39经典
积累的力量
JUC之线程间定制化通信
JVM调优参考
docker开机启动和dockercompose开机启动执行相应的各个docker容器
原文地址:https://www.cnblogs.com/gwazy/p/158242.html
最新文章
jmeter逻辑控制器
hive 视图view和物化视图materialized view
python 正态分布scipy.stats norm
Impala Catalog加载
springboot中rest风格的使用
swagger3的使用
@Import注解
常用参数注解使用
springboot静态资源目录的使用
springboot主配置类注解解析
热门文章
idea重新编译的快捷键
lombok使用
@ImportResource("")的作用
springboot静态资源目录
setCookie 参数
舒克的修仙之路
中国将大规模调整经济布局
六中基本算法思想
openFegin
Python 学习路径
Copyright © 2011-2022 走看看