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!
"
);
}
查看全文
相关阅读:
nginx优化之配置文件优化一常用参数
Centos7+LVS-DR+Apache负载均衡web实验
LVS负载均衡原理
Centos7+LVS-NAT+apache实验
CentOS7.6 使用ceph-deploy部署mimic 13.2.8集群(一)
linux关闭ACPI电源管理模块
Jenkins + pipeline + Git + PHP (九)
Jenkins-Master-slave架构(八)
Jenkins参数化构建(七)
Jenkins连接Git仓库时候报错Permission denied, please try again.
原文地址:https://www.cnblogs.com/gwazy/p/158242.html
最新文章
# 机器学习算法总结-第九天(XGboost)
# 机器学习算法总结-第八天(SKlearn中的kmeans/随机森林)
# 通过网格搜索、学习曲线对泰坦尼克号数据集调参
redis原理及集群主从配置
puppet工作原理及部署redis主从篇
puppet工作原理之模块使用
Centos7+puppet+foreman,实现部署OS
如何升级centos7 内核方法
vmware vSAN 入门
gitlab之source tree使用方法
热门文章
Mariadb/Mysql 主从复制(1)
Mariadb/Mysql命令行常用命令
简单介绍shell编程四剑客之sed
Centos7安装gitlab11 学习笔记之备份恢复及邮箱配置
Centos7安装gitlab11 学习笔记之基础概念、部署安装、权限管理、issue管理
简单介绍shell编程四剑客之grep
简单介绍shell编程四剑客之awk
Centos7+LVS-DR+keepalived实验(包含sorry-server、日志、及HTTP-GET的健康检测)
Centos7+nginx+keepalived集群及双主架构案例
高可用实现KeepAlived原理简介
Copyright © 2011-2022 走看看