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!
"
);
}
查看全文
相关阅读:
js 自定义属性
js innerText、textContent、innerHTML的区别和各自用法
js 的常用选择器
js Array属性和用法
js---String对象
iframe自适应高度js
thinkphp 的save()不能更新数据解决办法
转义字符
获取客户端真实ip
thinkphp条件查询和模糊查询的一些方法
原文地址:https://www.cnblogs.com/gwazy/p/158242.html
最新文章
SQL Server 2012 启动
SQL Server 2012 安装
把ISO文件加载到虚拟光驱
一个C#解决方案中各文件夹存放了些什么
命令行中的关键字,边学边添加
签名有元程序集 Signed Friend Assemblies
生成秘钥文件 sn.exe(Strong Name Tool)
未签名有元程序集 Unsigned Friend Assemblies
关于Thread.Sleep(0)
Lock的用法,为什么要用?
热门文章
pcl曲面重建模块-贪婪三角形投影算法实例
Ubuntu14.04下配置固定IP
ubuntu14.04为安装fcitx卸载ibus后出现system setting (系统设置)中图标消失的问题
原版ubuntu 系统下,emacs24无法输入中文问题解决方案
linux swap 分区那点事儿
JS 只创建一个元素
js 删除removeChild与替换replaceChild
js appendChild与insertBefore 区别和用法
JS 创建元素的三种方法
JS 节点的属性 与 元素
Copyright © 2011-2022 走看看