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!
"
);
}
查看全文
相关阅读:
字符串系列复习
点分治总结
LCT总结
网络流总结
centOS7下安装GUI图形界面
周记 2014.10.8
周记 2014.9.28
周记 2014.9.20
tar命令
[转]bit与byte
原文地址:https://www.cnblogs.com/gwazy/p/158242.html
最新文章
这十道经典Python笔试题,全做对算我输
SpringBoot_配置-自动配置原理(超重点)
SpringBoot_配置-Profile多环境支持
eclipse依赖spring-boot-configuration-processor,编写properties和yml没有提示
eclipse安装sts出现could not find jar:file解决办法
SpringBoot_配置-@PropertySource、@ImportResource、@Bean
缓冲区溢出漏洞(转载)
Laravel日记
传智健康——一、项目概述和环境搭建
LNMP安装步骤详细教程以及一键部署脚本
热门文章
Shell脚本一键部署——源码编译安装LNMP加discuz论坛!
什么是可串行化MVCC
使用shell脚本一键部署LNMP架构
长虹佳华-AWS云上迁移方法论
LNMP安装步骤详细教程以及一键部署脚本
LNMP(Nginx服务,MySQL 服务,安装PHP服务 手动安装技术文档)
大厂Redis高并发场景设计,面试问的都在这!
[HDU4609]3-idiots(生成函数+FFT)
[BZOJ3676][APIO2014]回文串(Manacher+SAM)
快速傅里叶变换(FFT)相关内容汇总
Copyright © 2011-2022 走看看