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!
"
);
}
查看全文
相关阅读:
Atlas 在web.config中的配置
解决Windows 7 IIS7.5 用户 'IIS APPPOOL\{站点名} AppPool'登录失败
IIS7 无法访问请求的页面,因为该页的相关配置数据无效。
三国演义60条职场启示
程序员,请对自己好一点!
HTTP 错误 403.14 Forbidden Web 服务器被配置为不列出此目录的内容
[转] 这样学英语三个月超过你过去学三年
HTTP 错误 500.19 Internal Server Error
软件项目经理新手上路(12) 给新手的建议
CPU指令集扫盲帖 四
原文地址:https://www.cnblogs.com/gwazy/p/158242.html
最新文章
[转]字符串和字符数组之间的转换
Table转换Div+CSS_工具下载
CSharp写的迷你操作系统
详解.NET中的动态编译(2)
安全工具(免费杀毒软件Avast、免费防火墙费尔、免费木马清理工具arswp,AVG/Ewido,超级兔子)
[转]URL参数乱码处理
[转]robots.txt语法
[转]240多个jQuery插件
[转]Web页面报错: Eval()、XPath() 和 Bind() 这类数据绑定方法只能在数据绑定控件的上下文中使用
Sql集成身份验证_Sql连接不上问题
热门文章
vss 命令行获取
[转]Framework源码查看
通用分页存储过程
reflector下载
IHttpModule 分块上传大文件
[转]搜索优化设置和收藏夹小图标
基于列选择的CodeSmith代码生成
调试.Net Framework内置程序集(System.Web.dll)
URL传递中文UTF8编码问题解决办法
Atlas中window.onload/自动加载自定义脚本的解决(ClientSide)
Copyright © 2011-2022 走看看