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!
"
);
}
查看全文
相关阅读:
技术期刊 · 白日照耀开鸿蒙 | 深入鸿蒙 ACE UI 框架解析;无限循环的 useEffect 类型;用 Three.js 实现 3D 房间;图神经网络入门;超基础的机器学习入门-原理篇
青岛敏捷之旅,来了!
痞子衡嵌入式:借助Serial Plot软件测量i.MXRT系列FlexSPI驱动Flash页编程执行时间分布
痞子衡嵌入式:超级下载算法RT-UFL v1.0在Segger Ozone下的使用
痞子衡嵌入式:超级下载算法RT-UFL v1.0在Keil MDK下的使用
痞子衡嵌入式:超级下载算法RT-UFL v1.0在IAR EW for Arm下的使用
痞子衡嵌入式:超级下载算法RT-UFL v1.0在MCUXpresso IDE下的使用
《痞子衡嵌入式半月刊》 第 42 期
痞子衡嵌入式:i.MXRT全系列下FlexSPI外设AHB Master ID定义与AHB RX Buffer指定的异同
CDP客户数据管理平台体系化搭建
原文地址:https://www.cnblogs.com/gwazy/p/158242.html
最新文章
怎么一本正经地秀技
new的过程是怎样的?看完这一篇就懂了
CH6 从文件中载入训练数据
结构型模式之适配器模式、桥接模式与装饰器模式(一)
Linux at命令详解
人人都写过的5个Bug!
[Git系列] Git 基本概念
[Git系列] 前言
wget命令8种实用用法
nginx配置解析域名
热门文章
php地址与经纬度之间转换
python配置anaconda
navicat 激活教程
分布式事务开山之作——《深入理解分布式事务:原理与实战》草图曝光!!
这部分布式事务开山之作,凭啥第一天预售就拿下当当新书榜No.1?
《深入理解计算系统》这本书到底怎样学?
.NET虚拟文件系统
性能测试之测试指标
(1)Zookeeper在linux环境中搭建集群
WebGL着色器渲染小游戏实战
Copyright © 2011-2022 走看看