- 浏览: 1813490 次
- 性别:
- 来自: 北京
文章分类
最新评论
-
yeying12:
强大
HTTP协议详解,你所不知道的 -
夏雪纷纷:
据我所知,9月12日有个对于DXperience 2013新功 ...
DXperience 9.1.3 -
Emy:
现在面临第一个坎。。
IT高薪者所具备的人格魅力 -
yangxiutian:
很详细,楼主很会总结。
IE下 z-index 的各种坑 -
idleone:
真心求指教,全选怎么实现
Android开发之ListView,加入CheckBox(复选框),实现选择列表
Enterprise Library 2.0 -- Security Application Block
今天来介绍一下Enterprise Library 2.0中的Security Application Block ,它可以帮助程序开发者在应用程序中实现与授权有关的大部分功能,不仅如此,它还支持缓存我们系统中用户的授权以及,授权数据。主要包括一下几个部分的功 能:
1、减少代码编写量;
2、保持应用程序中安全管理的一致性;
3、提供了应用程序中很多安全问题的实现方法;
4、可扩展,支持自定义Provider。
...
<p> 关于Security Application Block的介绍部分,我不再多说,具体可以参考帮助文档。下面我们来看一下如何配置Security Application Block,首先打开配置工具,新建一个Security Application Block,如下:
<img border="0" alt="" src="http://pw.cnblogs.com/images/cnblogs_com/pw/641.gif">
然后在Authorization节点下创建一个Authorization Rule Provider,名称为RuleProvider,如下:
<img border="0" alt="" src="http://pw.cnblogs.com/images/cnblogs_com/pw/642.gif">
现在就可以在RuleProvider节点下,创建一系列的规则(Rule)了。如下图:
<img border="0" alt="" src="http://pw.cnblogs.com/images/cnblogs_com/pw/643.gif">
新建了规则,我们还需要为这个规则定义一个表达式,用于在程序中判断是否满足该规则,如下图:
<img border="0" alt="" src="http://pw.cnblogs.com/images/cnblogs_com/pw/644.gif">
如图所示,我们定义了能通过该规则的用户的身份必须是User或Admin.我们可以根据我们的实际需要定义各种各样的规则。
最后我们可以为我们建立的Security Application Block分配一个默认的Authorization实例,如图:
<img border="0" alt="" src="http://pw.cnblogs.com/images/cnblogs_com/pw/645.gif">
此时,我们的配置文件中就多了如下内容:
</p>
<div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; font-size: 13px; width: 98%; background-color: rgb(238, 238, 238);">
<img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif"><span style="color: rgb(0, 0, 255);"><</span><span style="color: rgb(128, 0, 0);">section</span><span style="color: rgb(255, 0, 0);">name</span><span style="color: rgb(0, 0, 255);">="securityConfiguration"</span><span style="color: rgb(255, 0, 0);">type</span><span style="color: rgb(0, 0, 255);">="Microsoft.Practices.EnterpriseLibrary.Security.Configuration.SecuritySettings,Microsoft.Practices.EnterpriseLibrary.Security,Version=2.0.0.0,Culture=neutral,PublicKeyToken=null"</span><span style="color: rgb(255, 0, 0);"></span><span style="color: rgb(0, 0, 255);">/></span><span style="color: rgb(0, 0, 0);">
<img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif"><img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif"></span><span style="color: rgb(0, 0, 255);"><</span><span style="color: rgb(128, 0, 0);">securityConfiguration</span><span style="color: rgb(255, 0, 0);">defaultAuthorizationInstance</span><span style="color: rgb(0, 0, 255);">="RuleProvider"</span><span style="color: rgb(255, 0, 0);">
<img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif">defaultSecurityCacheInstance</span><span style="color: rgb(0, 0, 255);">=""</span><span style="color: rgb(0, 0, 255);">></span><span style="color: rgb(0, 0, 0);">
<img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif"></span><span style="color: rgb(0, 0, 255);"><</span><span style="color: rgb(128, 0, 0);">authorizationProviders</span><span style="color: rgb(0, 0, 255);">></span><span style="color: rgb(0, 0, 0);">
<img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif"></span><span style="color: rgb(0, 0, 255);"><</span><span style="color: rgb(128, 0, 0);">add</span><span style="color: rgb(255, 0, 0);">type</span><span style="color: rgb(0, 0, 255);">="Microsoft.Practices.EnterpriseLibrary.Security.AuthorizationRuleProvider,Microsoft.Practices.EnterpriseLibrary.Security,Version=2.0.0.0,Culture=neutral,PublicKeyToken=null"</span><span style="color: rgb(255, 0, 0);">
<img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif">name</span><span style="color: rgb(0, 0, 255);">="RuleProvider"</span><span style="color: rgb(0, 0, 255);">></span><span style="color: rgb(0, 0, 0);">
<img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif"></span><span style="color: rgb(0, 0, 255);"><</span><span style="color: rgb(128, 0, 0);">rules</span><span style="color: rgb(0, 0, 255);">></span><span style="color: rgb(0, 0, 0);">
<img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif"></span><span style="color: rgb(0, 0, 255);"><</span><span style="color: rgb(128, 0, 0);">add</span><span style="color: rgb(255, 0, 0);">expression</span><span style="color: rgb(0, 0, 255);">="R:UserORR:Admin"</span><span style="color: rgb(255, 0, 0);">name</span><span style="color: rgb(0, 0, 255);">="Rule"</span><span style="color: rgb(255, 0, 0);"></span><span style="color: rgb(0, 0, 255);">/></span><span style="color: rgb(0, 0, 0);">
<img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif"></span><span style="color: rgb(0, 0, 255);"></</span><span style="color: rgb(128, 0, 0);">rules</span><span style="color: rgb(0, 0, 255);">></span><span style="color: rgb(0, 0, 0);">
<img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif"></span><span style="color: rgb(0, 0, 255);"></</span><span style="color: rgb(128, 0, 0);">add</span><span style="color: rgb(0, 0, 255);">></span><span style="color: rgb(0, 0, 0);">
<img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif"></span><span style="color: rgb(0, 0, 255);"></</span><span style="color: rgb(128, 0, 0);">authorizationProviders</span><span style="color: rgb(0, 0, 255);">></span><span style="color: rgb(0, 0, 0);">
<img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif"></span><span style="color: rgb(0, 0, 255);"></</span><span style="color: rgb(128, 0, 0);">securityConfiguration</span><span style="color: rgb(0, 0, 255);">></span>
</div>
<p> 关于Security Application Block的配置就说到这里。</p>
<p> 下面来看看Enterprise Library 2.0 的Security Application Block 与1.0版本中的一点区别,因为ASP.NET2.0中提供了角色与成员管理的新特性,所以在某些方面和1.0版本还是有一定的区别:
因为ASP.NET2.0提供了MemberShip,Roles两个类,其中包含了验证用户、角色管理以及对Profile信息的设置与读取功能,所以 Security Application Block 2.0将这些功能去掉了。因此和1.0版本的主要区别在于下面三方面:
首先导入下面两个命名空间:
using System.Web.Profile;
using System.Web.Security;
1、验证用户的方法如下:
</p>
<div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; font-size: 13px; width: 98%; background-color: rgb(238, 238, 238);">
<img align="top" alt="" id="Codehighlighter1_8_11_Open_Image" src="http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif"><img align="top" alt="" id="Codehighlighter1_8_11_Closed_Image" style="display: none;" src="http://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif"><span style="color: rgb(0, 0, 0);"></span><span id="Codehighlighter1_8_11_Closed_Text" style="border: 1px solid rgb(128, 128, 128); display: none; background-color: rgb(255, 255, 255);">/**/</span><span id="Codehighlighter1_8_11_Open_Text"><span style="color: rgb(0, 128, 0);">/**/</span></span><span id="Codehighlighter1_12_224_Closed_Text" style="border: 1px solid rgb(128, 128, 128); display: none; background-color: rgb(255, 255, 255);">/**/</span><span id="Codehighlighter1_12_224_Open_Text"><span style="color: rgb(128, 128, 128);">///</span><span style="color: rgb(0, 128, 0);"></span><span style="color: rgb(128, 128, 128);"><summary></span><span style="color: rgb(0, 128, 0);">
<img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif"></span><span style="color: rgb(128, 128, 128);">///</span><span style="color: rgb(0, 128, 0);">验证用户
<img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif"></span><span style="color: rgb(128, 128, 128);">///</span><span style="color: rgb(0, 128, 0);"></span><span style="color: rgb(0, 128, 0); text-decoration: underline;">http://pw.cnblogs.com</span><span style="color: rgb(0, 128, 0);">
<img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif"></span><span style="color: rgb(128, 128, 128);">///</span><span style="color: rgb(0, 128, 0);"></span><span style="color: rgb(128, 128, 128);"></summary></span><span style="color: rgb(0, 128, 0);">
<img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif"></span><span style="color: rgb(128, 128, 128);">///</span><span style="color: rgb(0, 128, 0);"></span><span style="color: rgb(128, 128, 128);"><paramname="username"></span><span style="color: rgb(0, 128, 0);">用户名</span><span style="color: rgb(128, 128, 128);"></param></span><span style="color: rgb(0, 128, 0);">
<img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif"></span><span style="color: rgb(128, 128, 128);">///</span><span style="color: rgb(0, 128, 0);"></span><span style="color: rgb(128, 128, 128);"><paramname="password"></span><span style="color: rgb(0, 128, 0);">密码</span><span style="color: rgb(128, 128, 128);"></param></span><span style="color: rgb(0, 128, 0);">
<img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif"></span><span style="color: rgb(128, 128, 128);">///</span><span style="color: rgb(0, 128, 0);"></span><span style="color: rgb(128, 128, 128);"><returns></returns></span><span style="color: rgb(128, 128, 128);"></span></span>
<img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif"><span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 0, 255);">bool</span><span style="color: rgb(0, 0, 0);">CheckUser(</span><span style="color: rgb(0, 0, 255);">string</span><span style="color: rgb(0, 0, 0);">username,</span><span style="color: rgb(0, 0, 255);">string</span><span style="color: rgb(0, 0, 0);">password)
<img align="top" alt="" id="Codehighlighter1_297_371_Open_Image" src="http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif"><img align="top" alt="" id="Codehighlighter1_297_371_Closed_Image" style="display: none;" src="http://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif"></span><span id="Codehighlighter1_297_371_Closed_Text" style="border: 1px solid rgb(128, 128, 128); display: none; background-color: rgb(255, 255, 255);"><img alt="" src="http://www.cnblogs.com/Images/dot.gif"></span><span id="Codehighlighter1_297_371_Open_Text"><span style="color: rgb(0, 0, 0);">{
<img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif"></span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);">Membership.ValidateUser(username,password);
<img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif">}</span></span>
</div>
2、读取或设置Profile信息
<div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; font-size: 13px; width: 98%; background-color: rgb(238, 238, 238);">
<img align="top" alt="" id="Codehighlighter1_8_11_Open_Image" src="http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif"><img align="top" alt="" id="Codehighlighter1_8_11_Closed_Image" style="display: none;" src="http://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif"><span style="color: rgb(0, 0, 0);"></span><span id="Codehighlighter1_8_11_Closed_Text" style="border: 1px solid rgb(128, 128, 128); display: none; background-color: rgb(255, 255, 255);">/**/</span><span id="Codehighlighter1_8_11_Open_Text"><span style="color: rgb(0, 128, 0);">/**/</span></span><span id="Codehighlighter1_12_105_Closed_Text" style="border: 1px solid rgb(128, 128, 128); display: none; background-color: rgb(255, 255, 255);">/**/</span><span id="Codehighlighter1_12_105_Open_Text"><span style="color: rgb(128, 128, 128);">///</span><span style="color: rgb(0, 128, 0);"></span><span style="color: rgb(128, 128, 128);"><summary></span><span style="color: rgb(0, 128, 0);">
<img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif"></span><span style="color: rgb(128, 128, 128);">///</span><span style="color: rgb(0, 128, 0);">使用Profile
<img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif"></span><span style="color: rgb(128, 128, 128);">///</span><span style="color: rgb(0, 128, 0);"></span><span style="color: rgb(0, 128, 0); text-decoration: underline;">http://pw.cnblogs.com</span><span style="color: rgb(0, 128, 0);">
<img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif"></span><span style="color: rgb(128, 128, 128);">///</span><span style="color: rgb(0, 128, 0);"></span><span style="color: rgb(128, 128, 128);"></summary></span><span style="color: rgb(128, 128, 128);"></span></span>
<img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif"><span style="color: rgb(0, 0, 0);">[TestMethod]
<img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif"></span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);">UseProfile()
<img align="top" alt="" id="Codehighlighter1_168_885_Open_Image" src="http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif"><img align="top" alt="" id="Codehighlighter1_168_885_Closed_Image" style="display: none;" src="http://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif"></span><span id="Codehighlighter1_168_885_Closed_Text" style="border: 1px solid rgb(128, 128, 128); display: none; background-color: rgb(255, 255, 255);"><img alt="" src="http://www.cnblogs.com/Images/dot.gif"></span><span id="Codehighlighter1_168_885_Open_Text"><span style="color: rgb(0, 0, 0);">{
<img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif">IIdentityidentity;
<img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif">identity</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 0, 255);">new</span><span style="color: rgb(0, 0, 0);">GenericIdentity(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">SHY520</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">,Membership.Provider.Name);
<img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif"><img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif"></span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">设置Profile</span><span style="color: rgb(0, 128, 0);">
<img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif"></span><span style="color: rgb(0, 0, 0);">ProfileBasesetuserProfile</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">ProfileBase.Create(identity.Name);
<img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif"><img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif">setuserProfile[</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">Name</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">]</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">SHY520</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">;
<img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif">setuserProfile[</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">Address</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">]</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">WuhuChina</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">;
<img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif"></span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">保存Profile信息</span><span style="color: rgb(0, 128, 0);">
<img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif"></span><span style="color: rgb(0, 0, 0);">setuserProfile.Save();
<img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif"><img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif"></span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">读取Profile信息</span><span style="color: rgb(0, 128, 0);">
<img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif"></span><span style="color: rgb(0, 0, 0);">ProfileBasereaduserProfile</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">ProfileBase.Create(identity.Name);
<img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif"><img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif"></span><span style="color: rgb(0, 0, 255);">string</span><span style="color: rgb(0, 0, 0);">Name</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">readuserProfile[</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">Name</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">].ToString();
<img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif"></span><span style="color: rgb(0, 0, 255);">string</span><span style="color: rgb(0, 0, 0);">Address</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">readuserProfile[</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">Address</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">].ToString();
<img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif"><img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif">Assert.AreEqual(Name,</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">SHY520</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">);
<img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif">Assert.AreEqual(Address,</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">WuhuChina</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">);
<img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif">}</span></span>
</div>
3、得到某个用户所属角色列表
<div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; font-size: 13px; width: 98%; background-color: rgb(238, 238, 238);">
<img align="top" alt="" id="Codehighlighter1_8_11_Open_Image" src="http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif"><img align="top" alt="" id="Codehighlighter1_8_11_Closed_Image" style="display: none;" src="http://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif"><span style="color: rgb(0, 0, 0);"></span><span id="Codehighlighter1_8_11_Closed_Text" style="border: 1px solid rgb(128, 128, 128); display: none; background-color: rgb(255, 255, 255);">/**/</span><span id="Codehighlighter1_8_11_Open_Text"><span style="color: rgb(0, 128, 0);">/**/</span></span><span id="Codehighlighter1_12_142_Closed_Text" style="border: 1px solid rgb(128, 128, 128); display: none; background-color: rgb(255, 255, 255);">/**/</span><span id="Codehighlighter1_12_142_Open_Text"><span style="color: rgb(128, 128, 128);">///</span><span style="color: rgb(0, 128, 0);"></span><span style="color: rgb(128, 128, 128);"><summary></span><span style="color: rgb(0, 128, 0);">
<img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif"></span><span style="color: rgb(128, 128, 128);">///</span><span style="color: rgb(0, 128, 0);">得到某用户所属角色列表
<img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif"></span><span style="color: rgb(128, 128, 128);">///</span><span style="color: rgb(0, 128, 0);"></span><span style="color: rgb(0, 128, 0); text-decoration: underline;">http://pw.cnblogs.com</span><span style="color: rgb(0, 128, 0);">
<img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif"></span><span style="color: rgb(128, 128, 128);">///</span><span style="color: rgb(0, 128, 0);"></span><span style="color: rgb(128, 128, 128);"></summary></span><span style="color: rgb(0, 128, 0);">
<img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif"></span><span style="color: rgb(128, 128, 128);">///</span><span style="color: rgb(0, 128, 0);"></span><span style="color: rgb(128, 128, 128);"><returns></span><span style="color: rgb(0, 128, 0);">角色列表</span><span style="color: rgb(128, 128, 128);"></returns></span><span style="color: rgb(128, 128, 128);"></span></span>
<img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif"><span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 0, 255);">string</span><span style="color: rgb(0, 0, 0);">[]GetUserRoles()
<img align="top" alt="" id="Codehighlighter1_190_349_Open_Image" src="http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif"><img align="top" alt="" id="Codehighlighter1_190_349_Closed_Image" style="display: none;" src="http://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif"></span><span id="Codehighlighter1_190_349_Closed_Text" style="border: 1px solid rgb(128, 128, 128); display: none; background-color: rgb(255, 255, 255);"><img alt="" src="http://www.cnblogs.com/Images/dot.gif"></span><span id="Codehighlighter1_190_349_Open_Text"><span style="color: rgb(0, 0, 0);">{
<img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif">IIdentityidentity</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 0, 255);">new</span><span style="color: rgb(0, 0, 0);">GenericIdentity(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">SHY520</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">,Membership.Provider.Name);
<img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif"><img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif"></span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);">Roles.GetRolesForUser(identity.Name);
<img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif"><img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif">}</span></span>
</div>
<p> 与上一个版本的区别基本上就是以上几点,下面我们来看看Security Application Block2.0的基本使用方法:</p>
<p>1、为已授权的用户分配一个Token(记号):
Security Application Block将授权用户的身份信息放进缓存,并返回一个Token,所以我们在使用此方之前需要先配置一个Security Cache,具体方法我就不多说了,可以参考官方教程,下面贴出配置后的App.Config中的代码:
</p>
<div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; font-size: 13px; width: 98%; background-color: rgb(238, 238, 238);">
<img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif"><span style="color: rgb(0, 0, 255);"><</span><span style="color: rgb(128, 0, 0);">securityCacheProviders</span><span style="color: rgb(0, 0, 255);">></span><span style="color: rgb(0, 0, 0);">
<img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif"></span><span style="color: rgb(0, 0, 255);"><</span><span style="color: rgb(128, 0, 0);">add</span><span style="color: rgb(255, 0, 0);">cacheManagerInstanceName</span><span style="color: rgb(0, 0, 255);">="CacheManager"</span><span style="color: rgb(255, 0, 0);">defaultSlidingSessionExpirationInMinutes</span><span style="color: rgb(0, 0, 255);">="10"</span><span style="color: rgb(255, 0, 0);">
<img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif">defaultAbsoluteSessionExpirationInMinutes</span><span style="color: rgb(0, 0, 255);">="60"</span><span style="color: rgb(255, 0, 0);">type</span><span style="color: rgb(0, 0, 255);">="Microsoft.Practices.EnterpriseLibrary.Security.Cache.CachingStore.CachingStoreProvider,Microsoft.Practices.EnterpriseLibrary.Security.Cache.CachingStore,Version=2.0.0.0,Culture=neutral,PublicKeyToken=null"</span><span style="color: rgb(255, 0, 0);">
<img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif">name</span><span style="color: rgb(0, 0, 255);">="CachingStoreProvider"</span><span style="color: rgb(255, 0, 0);"></span><span style="color: rgb(0, 0, 255);">/></span><span style="color: rgb(0, 0, 0);">
<img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif"></span><span style="color: rgb(0, 0, 255);"></</span><span style="color: rgb(128, 0, 0);">securityCacheProviders</span><span style="color: rgb(0, 0, 255);">></span>
</div>
这段代码是在securityConfiguration节中的,其中cacheManagerInstanceName是我们在Caching Application Block中配置的实例。
<div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; font-size: 13px; width: 98%; background-color: rgb(238, 238, 238);">
<img align="top" alt="" id="Codehighlighter1_8_11_Open_Image" src="http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif"><img align="top" alt="" id="Codehighlighter1_8_11_Closed_Image" style="display: none;" src="http://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif"><span style="color: rgb(0, 0, 0);"></span><span id="Codehighlighter1_8_11_Closed_Text" style="border: 1px solid rgb(128, 128, 128); display: none; background-color: rgb(255, 255, 255);">/**/</span><span id="Codehighlighter1_8_11_Open_Text"><span style="color: rgb(0, 128, 0);">/**/</span></span><span id="Codehighlighter1_12_161_Closed_Text" style="border: 1px solid rgb(128, 128, 128); display: none; background-color: rgb(255, 255, 255);">/**/</span><span id="Codehighlighter1_12_161_Open_Text"><span style="color: rgb(128, 128, 128);">///</span><span style="color: rgb(0, 128, 0);"></span><span style="color: rgb(128, 128, 128);"><summary></span><span style="color: rgb(0, 128, 0);">
<img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif"></span><span style="color: rgb(128, 128, 128);">///</span><span style="color: rgb(0, 128, 0);">使用SaveIdentity方法,保存用户身份,并返回一个Token
<img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif"></span><span style="color: rgb(128, 128, 128);">///</span><span style="color: rgb(0, 128, 0);"></span><span style="color: rgb(0, 128, 0); text-decoration: underline;">http://pw.cnblogs.com</span><span style="color: rgb(0, 128, 0);">
<img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif"></span><span style="color: rgb(128, 128, 128);">///</span><span style="color: rgb(0, 128, 0);"></span><span style="color: rgb(128, 128, 128);"></summary></span><span style="color: rgb(0, 128, 0);">
<img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif"></span><span style="color: rgb(128, 128, 128);">///</span><span style="color: rgb(0, 128, 0);"></span><span style="color: rgb(128, 128, 128);"><returns></returns></span><span style="color: rgb(128, 128, 128);"></span></span>
<img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif"><span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);">ITokenSaveUserInfo()
<img align="top" alt="" id="Codehighlighter1_207_430_Open_Image" src="http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif"><img align="top" alt="" id="Codehighlighter1_207_430_Closed_Image" style="display: none;" src="http://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif"></span><span id="Codehighlighter1_207_430_Closed_Text" style="border: 1px solid rgb(128, 128, 128); display: none; background-color: rgb(255, 255, 255);"><img alt="" src="http://www.cnblogs.com/Images/dot.gif"></span><span id="Codehighlighter1_207_430_Open_Text"><span style="color: rgb(0, 0, 0);">{
<img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif">ISecurityCacheProvidersc</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">SecurityCacheFactory.GetSecurityCacheProvider(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">CacheManager</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">);
<img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif"><img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif"></span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">将用户身份装入缓存,并返回一个IToken类型的记号</span><span style="color: rgb(0, 128, 0);">
<img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif"></span><span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);">sc.SaveIdentity(</span><span style="color: rgb(0, 0, 255);">new</span><span style="color: rgb(0, 0, 0);">GenericIdentity(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">SHY520</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">));
<img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif">}</span></span>
</div>
2、使用Token来验证用户是否已被授权
<div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; font-size: 13px; width: 98%; background-color: rgb(238, 238, 238);">
<img align="top" alt="" id="Codehighlighter1_8_11_Open_Image" src="http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif"><img align="top" alt="" id="Codehighlighter1_8_11_Closed_Image" style="display: none;" src="http://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif"><span style="color: rgb(0, 0, 0);"></span><span id="Codehighlighter1_8_11_Closed_Text" style="border: 1px solid rgb(128, 128, 128); display: none; background-color: rgb(255, 255, 255);">/**/</span><span id="Codehighlighter1_8_11_Open_Text"><span style="color: rgb(0, 128, 0);">/**/</span></span><span id="Codehighlighter1_12_186_Closed_Text" style="border: 1px solid rgb(128, 128, 128); display: none; background-color: rgb(255, 255, 255);">/**/</span><span id="Codehighlighter1_12_186_Open_Text"><span style="color: rgb(128, 128, 128);">///</span><span style="color: rgb(0, 128, 0);"></span><span style="color: rgb(128, 128, 128);"><summary></span><span style="color: rgb(0, 128, 0);">
<img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif"></span><span style="color: rgb(128, 128, 128);">///</span><span style="color: rgb(0, 128, 0);">根据Token来判断用户是否已被授权
<img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif"></span><span style="color: rgb(128, 128, 128);">///</span><span style="color: rgb(0, 128, 0);"></span><span style="color: rgb(0, 128, 0); text-decoration: underline;">http://pw.cnblogs.com</span><span style="color: rgb(0, 128, 0);">
<img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif"></span><span style="color: rgb(128, 128, 128);">///</span><span style="color: rgb(0, 128, 0);"></span><span style="color: rgb(128, 128, 128);"></summary></span><span style="color: rgb(0, 128, 0);">
<img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif"></span><span style="color: rgb(128, 128, 128);">///</span><span style="color: rgb(0, 128, 0);"></span><span style="color: rgb(128, 128, 128);"><paramname="token"></param></span><span style="color: rgb(0, 128, 0);">
<img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif"></span><span style="color: rgb(128, 128, 128);">///</span><span style="color: rgb(0, 128, 0);"></span><span style="color: rgb(128, 128, 128);"><returns></returns></span><span style="color: rgb(128, 128, 128);"></span></span>
<img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif"><span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 0, 255);">bool</span><span style="color: rgb(0, 0, 0);">CheckUserByToken(ITokentoken)
<img align="top" alt="" id="Codehighlighter1_246_547_Open_Image" src="http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif"><img align="top" alt="" id="Codehighlighter1_246_547_Closed_Image" style="display: none;" src="http://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif"></span><span id="Codehighlighter1_246_547_Closed_Text" style="border: 1px solid rgb(128, 128, 128); display: none; background-color: rgb(255, 255, 255);"><img alt="" src="http://www.cnblogs.com/Images/dot.gif"></span><span id="Codehighlighter1_246_547_Open_Text"><span style="color: rgb(0, 0, 0);">{
<img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif">ISecurityCacheProvidersc</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">SecurityCacheFactory.GetSecurityCacheProvider(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">CacheManager</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">);
<img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif"><img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif"></span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">得到Token对应的身份IIdentity</span><span style="color: rgb(0, 128, 0);">
<img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif"></span><span style="color: rgb(0, 0, 0);">IIdentitysavedIdentity</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">sc.GetIdentity(token);
<img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif"></span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">返回该身份是否已被授权</span><span style="color: rgb(0, 128, 0);">
<img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif"></span><span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);">savedIdentity.IsAuthenticated;
<img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif"><img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif">}</span></span><span style="color: rgb(0, 0, 0);"></span>
</div>
3、结束User Session(即使Token过期)
<div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; font-size: 13px; width: 98%; background-color: rgb(238, 238, 238);">
<img align="top" alt="" id="Codehighlighter1_8_11_Open_Image" src="http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif"><img align="top" alt="" id="Codehighlighter1_8_11_Closed_Image" style="display: none;" src="http://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif"><span style="color: rgb(0, 0, 0);"></span><span id="Codehighlighter1_8_11_Closed_Text" style="border: 1px solid rgb(128, 128, 128); display: none; background-color: rgb(255, 255, 255);">/**/</span><span id="Codehighlighter1_8_11_Open_Text"><span style="color: rgb(0, 128, 0);">/**/</span></span><span id="Codehighlighter1_12_105_Closed_Text" style="border: 1px solid rgb(128, 128, 128); display: none; background-color: rgb(255, 255, 255);">/**/</span><span id="Codehighlighter1_12_105_Open_Text"><span style="color: rgb(128, 128, 128);">///</span><span style="color: rgb(0, 128, 0);"></span><span style="color: rgb(128, 128, 128);"><summary></span><span style="color: rgb(0, 128, 0);">
<img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif"></span><span style="color: rgb(128, 128, 128);">///</span><span style="color: rgb(0, 128, 0);">使用户Token过期
<img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif"></span><span style="color: rgb(128, 128, 128);">///</span><span style="color: rgb(0, 128, 0);"></span><span style="color: rgb(0, 128, 0); text-decoration: underline;">http://pw.cnblogs.com</span><span style="color: rgb(0, 128, 0);">
<img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif"></span><span style="color: rgb(128, 128, 128);">///</span><span style="color: rgb(0, 128, 0);"></span><span style="color: rgb(128, 128, 128);"></summary></span><span style="color: rgb(128, 128, 128);"></span></span>
<img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif"><span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);">CloseUserSession()
<img align="top" alt="" id="Codehighlighter1_153_419_Open_Image" src="http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif"><img align="top" alt="" id="Codehighlighter1_153_419_Closed_Image" style="display: none;" src="http://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif"></span><span id="Codehighlighter1_153_419_Closed_Text" style="border: 1px solid rgb(128, 128, 128); display: none; background-color: rgb(255, 255, 255);"><img alt="" src="http://www.cnblogs.com/Images/dot.gif"></span><span id="Codehighlighter1_153_419_Open_Text"><span style="color: rgb(0, 0, 0);">{
<img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif">ISecurityCacheProvidersc</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">SecurityCacheFactory.GetSecurityCacheProvider(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">CacheManager</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">);
<img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif"></span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">保存身份</span><span style="color: rgb(0, 128, 0);">
<img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif"></span><span style="color: rgb(0, 0, 0);">ITokentoken</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">sc.SaveIdentity(</span><span style="color: rgb(0, 0, 255);">new</span><span style="color: rgb(0, 0, 0);">GenericIdentity(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">SHY520</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">));
<img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif"></span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">使身份过期</span><span style="color: rgb(0, 128, 0);">
<img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif"></span><span style="color: rgb(0, 0, 0);">sc.ExpireIdentity(token);
<img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif">}</span></span>
</div>
4、判断用户是否满足一定的规则
首先需要导入下面的命名空间:
using System.Security;
using System.Security.Principal;
using Microsoft.Practices.EnterpriseLibrary.Security;
<div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; font-size: 13px; width: 98%; background-color: rgb(238, 238, 238);">
<img align="top" alt="" id="Codehighlighter1_4_7_Open_Image" src="http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif"><img align="top" alt="" id="Codehighlighter1_4_7_Closed_Image" style="display: none;" src="http://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif"><span style="color: rgb(0, 0, 0);"></span><span id="Codehighlighter1_4_7_Closed_Text" style="border: 1px solid rgb(128, 128, 128); display: none; background-color: rgb(255, 255, 255);">/**/</span><span id="Codehighlighter1_4_7_Open_Text"><span style="color: rgb(0, 128, 0);">/**/</span></span><span id="Codehighlighter1_8_272_Closed_Text" style="border: 1px solid rgb(128, 128, 128); display: none; background-color: rgb(255, 255, 255);">/**/</span><span id="Codehighlighter1_8_272_Open_Text"><span style="color: rgb(128, 128, 128);">///</span><span style="color: rgb(0, 128, 0);"></span><span style="color: rgb(128, 128, 128);"><summary></span><span style="color: rgb(0, 128, 0);">
<img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif"></span><span style="color: rgb(128, 128, 128);">///</span><span style="color: rgb(0, 128, 0);">判断用户是否满足一定的规则
<img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif"></span><span style="color: rgb(128, 128, 128);">///</span><span style="color: rgb(0, 128, 0);"></span><span style="color: rgb(0, 128, 0); text-decoration: underline;">http://pw.cnblogs.com</span><span style="color: rgb(0, 128, 0);">
<img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif"></span><span style="color: rgb(128, 128, 128);">///</span><span style="color: rgb(0, 128, 0);"></span><span style="color: rgb(128, 128, 128);"></summary></span><span style="color: rgb(0, 128, 0);">
<img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif"></span><span style="color: rgb(128, 128, 128);">///</span><span style="color: rgb(0, 128, 0);"></span><span style="color: rgb(128, 128, 128);"><paramname="username"></span><span style="color: rgb(0, 128, 0);">用户名称</span><span style="color: rgb(128, 128, 128);"></param></span><span style="color: rgb(0, 128, 0);">
<img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif"></span><span style="color: rgb(128, 128, 128);">///</span><span style="color: rgb(0, 128, 0);"></span><span style="color: rgb(128, 128, 128);"><paramname="role"></span><span style="color: rgb(0, 128, 0);">角色名称</span><span style="color: rgb(128, 128, 128);"></param></span><span style="color: rgb(0, 128, 0);">
<img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif"></span><span style="color: rgb(128, 128, 128);">///</span><span style="color: rgb(0, 128, 0);"></span><span style="color: rgb(128, 128, 128);"><paramname="rule"></span><span style="color: rgb(0, 128, 0);">规则名称</span><span style="color: rgb(128, 128, 128);"></param></span><span style="color: rgb(0, 128, 0);">
<img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif"></span><span style="color: rgb(128, 128, 128);">///</span><span style="color: rgb(0, 128, 0);"></span><span style="color: rgb(128, 128, 128);"><returns></returns></span><span style="color: rgb(128, 128, 128);"></span></span>
<img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif"><span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 0, 255);">bool</span><span style="color: rgb(0, 0, 0);">CheckUserRoleByRule(</span><span style="color: rgb(0, 0, 255);">string</span><span style="color: rgb(0, 0, 0);">username,</span><span style="color: rgb(0, 0, 255);">string</span><span style="color: rgb(0, 0, 0);">role,</span><span style="color: rgb(0, 0, 255);">string</span><span style="color: rgb(0, 0, 0);">rule)
<img align="top" alt="" id="Codehighlighter1_364_706_Open_Image" src="http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif"><img align="top" alt="" id="Codehighlighter1_364_706_Closed_Image" style="display: none;" src="http://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif"></span><span id="Codehighlighter1_364_706_Closed_Text" style="border: 1px solid rgb(128, 128, 128); display: none; background-color: rgb(255, 255, 255);"><img alt="" src="http://www.cnblogs.com/Images/dot.gif"></span><span id="Codehighlighter1_364_706_Open_Text"><span style="color: rgb(0, 0, 0);">{
<img align="top" alt="" id="Codehighlighter1_466_473_Open_Image" src="http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif"><img align="top" alt="" id="Codehighlighter1_466_473_Closed_Image" style="display: none;" src="http://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif">IPrincipalprincipal</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 0, 255);">new</span><span style="color: rgb(0, 0, 0);">GenericPrincipal(</span><span style="color: rgb(0, 0, 255);">new</span><span style="color: rgb(0, 0, 0);">GenericIdentity(username),</span><span style="color: rgb(0, 0, 255);">new</span><span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 0, 255);">string</span><span style="color: rgb(0, 0, 0);">[]</span><span id="Codehighlighter1_466_473_Closed_Text" style="border: 1px solid rgb(128, 128, 128); display: none; background-color: rgb(255, 255, 255);"><img alt="" src="http://www.cnblogs.com/Images/dot.gif"></span><span id="Codehighlighter1_466_473_Open_Text"><span style="color: rgb(0, 0, 0);">{role}</span></span><span style="color: rgb(0, 0, 0);">);
<img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif"><img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif"></span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">创建一个默认的Authorization实例</span><span style="color: rgb(0, 128, 0);">
<img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif"></span><span style="color: rgb(0, 0, 0);">IAuthorizationProviderautoprovider</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">AuthorizationFactory.GetAuthorizationProvider();
<img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif"><img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif"></span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">返回验证是否通过</span><span style="color: rgb(0, 128, 0);">
<img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif"></span><span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);">autoprovider.Authorize(principal,rule);
<img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif">}</span></span>
</div>
通过上面的方法可以很简单的实现对用户权限的鉴定,关于Security Application Block的简单应用就说到这里,下面我们来看一下如何实现自定义的authorization provider。
1、首先我们建立自己的Provider类,该类需继承AuthorizationProvider;
2、并且要加上[ConfigurationElementType(typeof(CustomAuthorizationProviderData))]的Attribute;
3、添加构造函数,参数为NameValueCollection类型;
4、覆盖基类中的Authorize方法,在其中添加自己的验证是否授权的逻辑
这样就完成了一个自定义的authorization provider。
完整的类定义如下:
<div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; font-size: 13px; width: 98%; background-color: rgb(238, 238, 238);">
<img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif"><span style="color: rgb(0, 0, 255);">using</span><span style="color: rgb(0, 0, 0);">System;
<img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif"></span><span style="color: rgb(0, 0, 255);">using</span><span style="color: rgb(0, 0, 0);">System.Collections.Generic;
<img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif"></span><span style="color: rgb(0, 0, 255);">using</span><span style="color: rgb(0, 0, 0);">System.Text;
<img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif"></span><span style="color: rgb(0, 0, 255);">using</span><span style="color: rgb(0, 0, 0);">Microsoft.Practices.EnterpriseLibrary.Security;
<img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif"></span><span style="color: rgb(0, 0, 255);">using</span><span style="color: rgb(0, 0, 0);">System.Collections.Specialized;
<img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif"></span><span style="color: rgb(0, 0, 255);">using</span><span style="color: rgb(0, 0, 0);">System.Security.Principal;
<img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif"></span><span style="color: rgb(0, 0, 255);">using</span><span style="color: rgb(0, 0, 0);">Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
<img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif"></span><span style="color: rgb(0, 0, 255);">using</span><span style="color: rgb(0, 0, 0);">Microsoft.Practices.EnterpriseLibrary.Security.Configuration;
<img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif"><img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif"></span><span style="color: rgb(0, 0, 255);">namespace</span><span style="color: rgb(0, 0, 0);">Enterprise_Library_2
<img align="top" alt="" id="Codehighlighter1_358_767_Open_Image" src="http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif"><img align="top" alt="" id="Codehighlighter1_358_767_Closed_Image" style="display: none;" src="http://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif"></span><span id="Codehighlighter1_358_767_Closed_Text" style="border: 1px solid rgb(128, 128, 128); display: none; background-color: rgb(255, 255, 255);"><img alt="" src="http://www.cnblogs.com/Images/dot.gif"></span><span id="Codehighlighter1_358_767_Open_Text"><span style="color: rgb(0, 0, 0);">{
<img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif">[ConfigurationElementType(</span><span style="color: rgb(0, 0, 255);">typeof</span><span style="color: rgb(0, 0, 0);">(CustomAuthorizationProviderData))]
<img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif"></span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 0, 255);">class</span><span style="color: rgb(0, 0, 0);">CustomProvider:AuthorizationProvider
<img align="top" alt="" id="Codehighlighter1_492_765_Open_Image" src="http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif"><img align="top" alt="" id="Codehighlighter1_492_765_Closed_Image" style="display: none;" src="http://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif"></span><span id="Codehighlighter1_492_765_Closed_Text" style="border: 1px solid rgb(128, 128, 128); display: none; background-color: rgb(255, 255, 255);"><img alt="" src="http://www.cnblogs.com/Images/dot.gif"></span><span id="Codehighlighter1_492_765_Open_Text"><span style="color: rgb(0, 0, 0);">{
<img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif"></span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);">CustomProvider(NameValueCollectionconfigurationItems)
<img align="top" alt="" id="Codehighlighter1_572_595_Open_Image" src="http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif"><img align="top" alt="" id="Codehighlighter1_572_595_Closed_Image" style="display: none;" src="http://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif"></span><span id="Codehighlighter1_572_595_Closed_Text" style="border: 1px solid rgb(128, 128, 128); display: none; background-color: rgb(255, 255, 255);"><img alt="" src="http://www.cnblogs.com/Images/dot.gif"></span><span id="Codehighlighter1_572_595_Open_Text"><span style="color: rgb(0, 0, 0);">{
<img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif"><img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif">}</span></span><span style="color: rgb(0, 0, 0);">
<img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif"><img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif"></span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 0, 255);">override</span><span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 0, 255);">bool</span><span style="color: rgb(0, 0, 0);">Authorize(IPrincipalprincipal,</span><span style="color: rgb(0, 0, 255);">string</span><span style="color: rgb(0, 0, 0);">context)
<img align="top" alt="" id="Codehighlighter1_683_758_Open_Image" src="http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif"><img align="top" alt="" id="Codehighlighter1_683_758_Closed_Image" style="display: none;" src="http://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif"></span><span id="Codehighlighter1_683_758_Closed_Text" style="border: 1px solid rgb(128, 128, 128); display: none; background-color: rgb(255, 255, 255);"><img alt="" src="http://www.cnblogs.com/Images/dot.gif"></span><span id="Codehighlighter1_683_758_Open_Text"><span style="color: rgb(0, 0, 0);">{
<img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif"></span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">TODO:在此处添加自己需要的验证授权的逻辑既可</span><span style="color: rgb(0, 128, 0);">
<img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif"></span><span style="color: rgb(0, 0, 0);">
<img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif"></span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 0, 255);">true</span><span style="color: rgb(0, 0, 0);">;
<img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif">}</span></span><span style="color: rgb(0, 0, 0);">
<img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif"><img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif">}</span></span><span style="color: rgb(0, 0, 0);">
<img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif">}</span></span>
</div>
<!--Beging Temp Save--> <script type="text/javascript">
function pageLoad()
{
Sys.WebForms.PageRequestManager.getInstance().add_initializeRequest(handleInitializeRequest);
//Sys.WebForms.PageRequestManager.getInstance().add_endRequest(handleEndRequest);
}
function handleInitializeRequest(sender, args)
{
var prm = Sys.WebForms.PageRequestManager.getInstance();
var eid = args.get_postBackElement().id;
if (eid.indexOf("DeleteLink")>0)
{
args.get_postBackElement().innerHTML = "<font color='red'>正在删除...</font>";
}
else if (eid.indexOf("btnSubmit")>0)
{
document.getElementById("AjaxHolder_PostComment_ltSubmitMsg").innerHTML="正在提交...";
document.getElementById("AjaxHolder_PostComment_btnSubmit").disabled = true;
}
else if(eid.indexOf("refreshList")>0)
{
document.getElementById("AjaxHolder_PostComment_refreshList").innerHTML="<font color='red'>正在刷新...</font>";
}
}
function TempSave(ElementID)
{
try
{
CommentsPersistDiv.setAttribute("CommentContent",document.getElementById(ElementID).value);
CommentsPersistDiv.save("CommentXMLStore");
}
catch(ex)
{
}
}
function Restore(ElementID)
{
CommentsPersistDiv.load("CommentXMLStore");
document.getElementById(ElementID).value=CommentsPersistDiv.getAttribute("CommentContent");
}
</script><!--Ene TempSave-->
1、减少代码编写量;
2、保持应用程序中安全管理的一致性;
3、提供了应用程序中很多安全问题的实现方法;
4、可扩展,支持自定义Provider。
...
<p> 关于Security Application Block的介绍部分,我不再多说,具体可以参考帮助文档。下面我们来看一下如何配置Security Application Block,首先打开配置工具,新建一个Security Application Block,如下:
<img border="0" alt="" src="http://pw.cnblogs.com/images/cnblogs_com/pw/641.gif">
然后在Authorization节点下创建一个Authorization Rule Provider,名称为RuleProvider,如下:
<img border="0" alt="" src="http://pw.cnblogs.com/images/cnblogs_com/pw/642.gif">
现在就可以在RuleProvider节点下,创建一系列的规则(Rule)了。如下图:
<img border="0" alt="" src="http://pw.cnblogs.com/images/cnblogs_com/pw/643.gif">
新建了规则,我们还需要为这个规则定义一个表达式,用于在程序中判断是否满足该规则,如下图:
<img border="0" alt="" src="http://pw.cnblogs.com/images/cnblogs_com/pw/644.gif">
如图所示,我们定义了能通过该规则的用户的身份必须是User或Admin.我们可以根据我们的实际需要定义各种各样的规则。
最后我们可以为我们建立的Security Application Block分配一个默认的Authorization实例,如图:
<img border="0" alt="" src="http://pw.cnblogs.com/images/cnblogs_com/pw/645.gif">
此时,我们的配置文件中就多了如下内容:
</p>
<div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; font-size: 13px; width: 98%; background-color: rgb(238, 238, 238);">
<img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif"><span style="color: rgb(0, 0, 255);"><</span><span style="color: rgb(128, 0, 0);">section</span><span style="color: rgb(255, 0, 0);">name</span><span style="color: rgb(0, 0, 255);">="securityConfiguration"</span><span style="color: rgb(255, 0, 0);">type</span><span style="color: rgb(0, 0, 255);">="Microsoft.Practices.EnterpriseLibrary.Security.Configuration.SecuritySettings,Microsoft.Practices.EnterpriseLibrary.Security,Version=2.0.0.0,Culture=neutral,PublicKeyToken=null"</span><span style="color: rgb(255, 0, 0);"></span><span style="color: rgb(0, 0, 255);">/></span><span style="color: rgb(0, 0, 0);">
<img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif"><img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif"></span><span style="color: rgb(0, 0, 255);"><</span><span style="color: rgb(128, 0, 0);">securityConfiguration</span><span style="color: rgb(255, 0, 0);">defaultAuthorizationInstance</span><span style="color: rgb(0, 0, 255);">="RuleProvider"</span><span style="color: rgb(255, 0, 0);">
<img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif">defaultSecurityCacheInstance</span><span style="color: rgb(0, 0, 255);">=""</span><span style="color: rgb(0, 0, 255);">></span><span style="color: rgb(0, 0, 0);">
<img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif"></span><span style="color: rgb(0, 0, 255);"><</span><span style="color: rgb(128, 0, 0);">authorizationProviders</span><span style="color: rgb(0, 0, 255);">></span><span style="color: rgb(0, 0, 0);">
<img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif"></span><span style="color: rgb(0, 0, 255);"><</span><span style="color: rgb(128, 0, 0);">add</span><span style="color: rgb(255, 0, 0);">type</span><span style="color: rgb(0, 0, 255);">="Microsoft.Practices.EnterpriseLibrary.Security.AuthorizationRuleProvider,Microsoft.Practices.EnterpriseLibrary.Security,Version=2.0.0.0,Culture=neutral,PublicKeyToken=null"</span><span style="color: rgb(255, 0, 0);">
<img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif">name</span><span style="color: rgb(0, 0, 255);">="RuleProvider"</span><span style="color: rgb(0, 0, 255);">></span><span style="color: rgb(0, 0, 0);">
<img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif"></span><span style="color: rgb(0, 0, 255);"><</span><span style="color: rgb(128, 0, 0);">rules</span><span style="color: rgb(0, 0, 255);">></span><span style="color: rgb(0, 0, 0);">
<img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif"></span><span style="color: rgb(0, 0, 255);"><</span><span style="color: rgb(128, 0, 0);">add</span><span style="color: rgb(255, 0, 0);">expression</span><span style="color: rgb(0, 0, 255);">="R:UserORR:Admin"</span><span style="color: rgb(255, 0, 0);">name</span><span style="color: rgb(0, 0, 255);">="Rule"</span><span style="color: rgb(255, 0, 0);"></span><span style="color: rgb(0, 0, 255);">/></span><span style="color: rgb(0, 0, 0);">
<img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif"></span><span style="color: rgb(0, 0, 255);"></</span><span style="color: rgb(128, 0, 0);">rules</span><span style="color: rgb(0, 0, 255);">></span><span style="color: rgb(0, 0, 0);">
<img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif"></span><span style="color: rgb(0, 0, 255);"></</span><span style="color: rgb(128, 0, 0);">add</span><span style="color: rgb(0, 0, 255);">></span><span style="color: rgb(0, 0, 0);">
<img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif"></span><span style="color: rgb(0, 0, 255);"></</span><span style="color: rgb(128, 0, 0);">authorizationProviders</span><span style="color: rgb(0, 0, 255);">></span><span style="color: rgb(0, 0, 0);">
<img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif"></span><span style="color: rgb(0, 0, 255);"></</span><span style="color: rgb(128, 0, 0);">securityConfiguration</span><span style="color: rgb(0, 0, 255);">></span>
</div>
<p> 关于Security Application Block的配置就说到这里。</p>
<p> 下面来看看Enterprise Library 2.0 的Security Application Block 与1.0版本中的一点区别,因为ASP.NET2.0中提供了角色与成员管理的新特性,所以在某些方面和1.0版本还是有一定的区别:
因为ASP.NET2.0提供了MemberShip,Roles两个类,其中包含了验证用户、角色管理以及对Profile信息的设置与读取功能,所以 Security Application Block 2.0将这些功能去掉了。因此和1.0版本的主要区别在于下面三方面:
首先导入下面两个命名空间:
using System.Web.Profile;
using System.Web.Security;
1、验证用户的方法如下:
</p>
<div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; font-size: 13px; width: 98%; background-color: rgb(238, 238, 238);">
<img align="top" alt="" id="Codehighlighter1_8_11_Open_Image" src="http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif"><img align="top" alt="" id="Codehighlighter1_8_11_Closed_Image" style="display: none;" src="http://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif"><span style="color: rgb(0, 0, 0);"></span><span id="Codehighlighter1_8_11_Closed_Text" style="border: 1px solid rgb(128, 128, 128); display: none; background-color: rgb(255, 255, 255);">/**/</span><span id="Codehighlighter1_8_11_Open_Text"><span style="color: rgb(0, 128, 0);">/**/</span></span><span id="Codehighlighter1_12_224_Closed_Text" style="border: 1px solid rgb(128, 128, 128); display: none; background-color: rgb(255, 255, 255);">/**/</span><span id="Codehighlighter1_12_224_Open_Text"><span style="color: rgb(128, 128, 128);">///</span><span style="color: rgb(0, 128, 0);"></span><span style="color: rgb(128, 128, 128);"><summary></span><span style="color: rgb(0, 128, 0);">
<img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif"></span><span style="color: rgb(128, 128, 128);">///</span><span style="color: rgb(0, 128, 0);">验证用户
<img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif"></span><span style="color: rgb(128, 128, 128);">///</span><span style="color: rgb(0, 128, 0);"></span><span style="color: rgb(0, 128, 0); text-decoration: underline;">http://pw.cnblogs.com</span><span style="color: rgb(0, 128, 0);">
<img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif"></span><span style="color: rgb(128, 128, 128);">///</span><span style="color: rgb(0, 128, 0);"></span><span style="color: rgb(128, 128, 128);"></summary></span><span style="color: rgb(0, 128, 0);">
<img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif"></span><span style="color: rgb(128, 128, 128);">///</span><span style="color: rgb(0, 128, 0);"></span><span style="color: rgb(128, 128, 128);"><paramname="username"></span><span style="color: rgb(0, 128, 0);">用户名</span><span style="color: rgb(128, 128, 128);"></param></span><span style="color: rgb(0, 128, 0);">
<img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif"></span><span style="color: rgb(128, 128, 128);">///</span><span style="color: rgb(0, 128, 0);"></span><span style="color: rgb(128, 128, 128);"><paramname="password"></span><span style="color: rgb(0, 128, 0);">密码</span><span style="color: rgb(128, 128, 128);"></param></span><span style="color: rgb(0, 128, 0);">
<img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif"></span><span style="color: rgb(128, 128, 128);">///</span><span style="color: rgb(0, 128, 0);"></span><span style="color: rgb(128, 128, 128);"><returns></returns></span><span style="color: rgb(128, 128, 128);"></span></span>
<img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif"><span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 0, 255);">bool</span><span style="color: rgb(0, 0, 0);">CheckUser(</span><span style="color: rgb(0, 0, 255);">string</span><span style="color: rgb(0, 0, 0);">username,</span><span style="color: rgb(0, 0, 255);">string</span><span style="color: rgb(0, 0, 0);">password)
<img align="top" alt="" id="Codehighlighter1_297_371_Open_Image" src="http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif"><img align="top" alt="" id="Codehighlighter1_297_371_Closed_Image" style="display: none;" src="http://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif"></span><span id="Codehighlighter1_297_371_Closed_Text" style="border: 1px solid rgb(128, 128, 128); display: none; background-color: rgb(255, 255, 255);"><img alt="" src="http://www.cnblogs.com/Images/dot.gif"></span><span id="Codehighlighter1_297_371_Open_Text"><span style="color: rgb(0, 0, 0);">{
<img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif"></span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);">Membership.ValidateUser(username,password);
<img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif">}</span></span>
</div>
2、读取或设置Profile信息
<div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; font-size: 13px; width: 98%; background-color: rgb(238, 238, 238);">
<img align="top" alt="" id="Codehighlighter1_8_11_Open_Image" src="http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif"><img align="top" alt="" id="Codehighlighter1_8_11_Closed_Image" style="display: none;" src="http://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif"><span style="color: rgb(0, 0, 0);"></span><span id="Codehighlighter1_8_11_Closed_Text" style="border: 1px solid rgb(128, 128, 128); display: none; background-color: rgb(255, 255, 255);">/**/</span><span id="Codehighlighter1_8_11_Open_Text"><span style="color: rgb(0, 128, 0);">/**/</span></span><span id="Codehighlighter1_12_105_Closed_Text" style="border: 1px solid rgb(128, 128, 128); display: none; background-color: rgb(255, 255, 255);">/**/</span><span id="Codehighlighter1_12_105_Open_Text"><span style="color: rgb(128, 128, 128);">///</span><span style="color: rgb(0, 128, 0);"></span><span style="color: rgb(128, 128, 128);"><summary></span><span style="color: rgb(0, 128, 0);">
<img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif"></span><span style="color: rgb(128, 128, 128);">///</span><span style="color: rgb(0, 128, 0);">使用Profile
<img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif"></span><span style="color: rgb(128, 128, 128);">///</span><span style="color: rgb(0, 128, 0);"></span><span style="color: rgb(0, 128, 0); text-decoration: underline;">http://pw.cnblogs.com</span><span style="color: rgb(0, 128, 0);">
<img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif"></span><span style="color: rgb(128, 128, 128);">///</span><span style="color: rgb(0, 128, 0);"></span><span style="color: rgb(128, 128, 128);"></summary></span><span style="color: rgb(128, 128, 128);"></span></span>
<img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif"><span style="color: rgb(0, 0, 0);">[TestMethod]
<img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif"></span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);">UseProfile()
<img align="top" alt="" id="Codehighlighter1_168_885_Open_Image" src="http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif"><img align="top" alt="" id="Codehighlighter1_168_885_Closed_Image" style="display: none;" src="http://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif"></span><span id="Codehighlighter1_168_885_Closed_Text" style="border: 1px solid rgb(128, 128, 128); display: none; background-color: rgb(255, 255, 255);"><img alt="" src="http://www.cnblogs.com/Images/dot.gif"></span><span id="Codehighlighter1_168_885_Open_Text"><span style="color: rgb(0, 0, 0);">{
<img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif">IIdentityidentity;
<img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif">identity</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 0, 255);">new</span><span style="color: rgb(0, 0, 0);">GenericIdentity(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">SHY520</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">,Membership.Provider.Name);
<img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif"><img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif"></span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">设置Profile</span><span style="color: rgb(0, 128, 0);">
<img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif"></span><span style="color: rgb(0, 0, 0);">ProfileBasesetuserProfile</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">ProfileBase.Create(identity.Name);
<img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif"><img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif">setuserProfile[</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">Name</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">]</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">SHY520</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">;
<img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif">setuserProfile[</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">Address</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">]</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">WuhuChina</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">;
<img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif"></span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">保存Profile信息</span><span style="color: rgb(0, 128, 0);">
<img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif"></span><span style="color: rgb(0, 0, 0);">setuserProfile.Save();
<img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif"><img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif"></span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">读取Profile信息</span><span style="color: rgb(0, 128, 0);">
<img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif"></span><span style="color: rgb(0, 0, 0);">ProfileBasereaduserProfile</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">ProfileBase.Create(identity.Name);
<img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif"><img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif"></span><span style="color: rgb(0, 0, 255);">string</span><span style="color: rgb(0, 0, 0);">Name</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">readuserProfile[</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">Name</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">].ToString();
<img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif"></span><span style="color: rgb(0, 0, 255);">string</span><span style="color: rgb(0, 0, 0);">Address</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">readuserProfile[</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">Address</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">].ToString();
<img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif"><img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif">Assert.AreEqual(Name,</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">SHY520</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">);
<img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif">Assert.AreEqual(Address,</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">WuhuChina</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">);
<img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif">}</span></span>
</div>
3、得到某个用户所属角色列表
<div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; font-size: 13px; width: 98%; background-color: rgb(238, 238, 238);">
<img align="top" alt="" id="Codehighlighter1_8_11_Open_Image" src="http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif"><img align="top" alt="" id="Codehighlighter1_8_11_Closed_Image" style="display: none;" src="http://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif"><span style="color: rgb(0, 0, 0);"></span><span id="Codehighlighter1_8_11_Closed_Text" style="border: 1px solid rgb(128, 128, 128); display: none; background-color: rgb(255, 255, 255);">/**/</span><span id="Codehighlighter1_8_11_Open_Text"><span style="color: rgb(0, 128, 0);">/**/</span></span><span id="Codehighlighter1_12_142_Closed_Text" style="border: 1px solid rgb(128, 128, 128); display: none; background-color: rgb(255, 255, 255);">/**/</span><span id="Codehighlighter1_12_142_Open_Text"><span style="color: rgb(128, 128, 128);">///</span><span style="color: rgb(0, 128, 0);"></span><span style="color: rgb(128, 128, 128);"><summary></span><span style="color: rgb(0, 128, 0);">
<img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif"></span><span style="color: rgb(128, 128, 128);">///</span><span style="color: rgb(0, 128, 0);">得到某用户所属角色列表
<img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif"></span><span style="color: rgb(128, 128, 128);">///</span><span style="color: rgb(0, 128, 0);"></span><span style="color: rgb(0, 128, 0); text-decoration: underline;">http://pw.cnblogs.com</span><span style="color: rgb(0, 128, 0);">
<img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif"></span><span style="color: rgb(128, 128, 128);">///</span><span style="color: rgb(0, 128, 0);"></span><span style="color: rgb(128, 128, 128);"></summary></span><span style="color: rgb(0, 128, 0);">
<img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif"></span><span style="color: rgb(128, 128, 128);">///</span><span style="color: rgb(0, 128, 0);"></span><span style="color: rgb(128, 128, 128);"><returns></span><span style="color: rgb(0, 128, 0);">角色列表</span><span style="color: rgb(128, 128, 128);"></returns></span><span style="color: rgb(128, 128, 128);"></span></span>
<img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif"><span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 0, 255);">string</span><span style="color: rgb(0, 0, 0);">[]GetUserRoles()
<img align="top" alt="" id="Codehighlighter1_190_349_Open_Image" src="http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif"><img align="top" alt="" id="Codehighlighter1_190_349_Closed_Image" style="display: none;" src="http://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif"></span><span id="Codehighlighter1_190_349_Closed_Text" style="border: 1px solid rgb(128, 128, 128); display: none; background-color: rgb(255, 255, 255);"><img alt="" src="http://www.cnblogs.com/Images/dot.gif"></span><span id="Codehighlighter1_190_349_Open_Text"><span style="color: rgb(0, 0, 0);">{
<img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif">IIdentityidentity</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 0, 255);">new</span><span style="color: rgb(0, 0, 0);">GenericIdentity(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">SHY520</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">,Membership.Provider.Name);
<img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif"><img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif"></span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);">Roles.GetRolesForUser(identity.Name);
<img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif"><img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif">}</span></span>
</div>
<p> 与上一个版本的区别基本上就是以上几点,下面我们来看看Security Application Block2.0的基本使用方法:</p>
<p>1、为已授权的用户分配一个Token(记号):
Security Application Block将授权用户的身份信息放进缓存,并返回一个Token,所以我们在使用此方之前需要先配置一个Security Cache,具体方法我就不多说了,可以参考官方教程,下面贴出配置后的App.Config中的代码:
</p>
<div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; font-size: 13px; width: 98%; background-color: rgb(238, 238, 238);">
<img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif"><span style="color: rgb(0, 0, 255);"><</span><span style="color: rgb(128, 0, 0);">securityCacheProviders</span><span style="color: rgb(0, 0, 255);">></span><span style="color: rgb(0, 0, 0);">
<img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif"></span><span style="color: rgb(0, 0, 255);"><</span><span style="color: rgb(128, 0, 0);">add</span><span style="color: rgb(255, 0, 0);">cacheManagerInstanceName</span><span style="color: rgb(0, 0, 255);">="CacheManager"</span><span style="color: rgb(255, 0, 0);">defaultSlidingSessionExpirationInMinutes</span><span style="color: rgb(0, 0, 255);">="10"</span><span style="color: rgb(255, 0, 0);">
<img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif">defaultAbsoluteSessionExpirationInMinutes</span><span style="color: rgb(0, 0, 255);">="60"</span><span style="color: rgb(255, 0, 0);">type</span><span style="color: rgb(0, 0, 255);">="Microsoft.Practices.EnterpriseLibrary.Security.Cache.CachingStore.CachingStoreProvider,Microsoft.Practices.EnterpriseLibrary.Security.Cache.CachingStore,Version=2.0.0.0,Culture=neutral,PublicKeyToken=null"</span><span style="color: rgb(255, 0, 0);">
<img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif">name</span><span style="color: rgb(0, 0, 255);">="CachingStoreProvider"</span><span style="color: rgb(255, 0, 0);"></span><span style="color: rgb(0, 0, 255);">/></span><span style="color: rgb(0, 0, 0);">
<img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif"></span><span style="color: rgb(0, 0, 255);"></</span><span style="color: rgb(128, 0, 0);">securityCacheProviders</span><span style="color: rgb(0, 0, 255);">></span>
</div>
这段代码是在securityConfiguration节中的,其中cacheManagerInstanceName是我们在Caching Application Block中配置的实例。
<div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; font-size: 13px; width: 98%; background-color: rgb(238, 238, 238);">
<img align="top" alt="" id="Codehighlighter1_8_11_Open_Image" src="http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif"><img align="top" alt="" id="Codehighlighter1_8_11_Closed_Image" style="display: none;" src="http://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif"><span style="color: rgb(0, 0, 0);"></span><span id="Codehighlighter1_8_11_Closed_Text" style="border: 1px solid rgb(128, 128, 128); display: none; background-color: rgb(255, 255, 255);">/**/</span><span id="Codehighlighter1_8_11_Open_Text"><span style="color: rgb(0, 128, 0);">/**/</span></span><span id="Codehighlighter1_12_161_Closed_Text" style="border: 1px solid rgb(128, 128, 128); display: none; background-color: rgb(255, 255, 255);">/**/</span><span id="Codehighlighter1_12_161_Open_Text"><span style="color: rgb(128, 128, 128);">///</span><span style="color: rgb(0, 128, 0);"></span><span style="color: rgb(128, 128, 128);"><summary></span><span style="color: rgb(0, 128, 0);">
<img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif"></span><span style="color: rgb(128, 128, 128);">///</span><span style="color: rgb(0, 128, 0);">使用SaveIdentity方法,保存用户身份,并返回一个Token
<img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif"></span><span style="color: rgb(128, 128, 128);">///</span><span style="color: rgb(0, 128, 0);"></span><span style="color: rgb(0, 128, 0); text-decoration: underline;">http://pw.cnblogs.com</span><span style="color: rgb(0, 128, 0);">
<img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif"></span><span style="color: rgb(128, 128, 128);">///</span><span style="color: rgb(0, 128, 0);"></span><span style="color: rgb(128, 128, 128);"></summary></span><span style="color: rgb(0, 128, 0);">
<img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif"></span><span style="color: rgb(128, 128, 128);">///</span><span style="color: rgb(0, 128, 0);"></span><span style="color: rgb(128, 128, 128);"><returns></returns></span><span style="color: rgb(128, 128, 128);"></span></span>
<img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif"><span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);">ITokenSaveUserInfo()
<img align="top" alt="" id="Codehighlighter1_207_430_Open_Image" src="http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif"><img align="top" alt="" id="Codehighlighter1_207_430_Closed_Image" style="display: none;" src="http://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif"></span><span id="Codehighlighter1_207_430_Closed_Text" style="border: 1px solid rgb(128, 128, 128); display: none; background-color: rgb(255, 255, 255);"><img alt="" src="http://www.cnblogs.com/Images/dot.gif"></span><span id="Codehighlighter1_207_430_Open_Text"><span style="color: rgb(0, 0, 0);">{
<img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif">ISecurityCacheProvidersc</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">SecurityCacheFactory.GetSecurityCacheProvider(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">CacheManager</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">);
<img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif"><img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif"></span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">将用户身份装入缓存,并返回一个IToken类型的记号</span><span style="color: rgb(0, 128, 0);">
<img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif"></span><span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);">sc.SaveIdentity(</span><span style="color: rgb(0, 0, 255);">new</span><span style="color: rgb(0, 0, 0);">GenericIdentity(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">SHY520</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">));
<img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif">}</span></span>
</div>
2、使用Token来验证用户是否已被授权
<div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; font-size: 13px; width: 98%; background-color: rgb(238, 238, 238);">
<img align="top" alt="" id="Codehighlighter1_8_11_Open_Image" src="http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif"><img align="top" alt="" id="Codehighlighter1_8_11_Closed_Image" style="display: none;" src="http://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif"><span style="color: rgb(0, 0, 0);"></span><span id="Codehighlighter1_8_11_Closed_Text" style="border: 1px solid rgb(128, 128, 128); display: none; background-color: rgb(255, 255, 255);">/**/</span><span id="Codehighlighter1_8_11_Open_Text"><span style="color: rgb(0, 128, 0);">/**/</span></span><span id="Codehighlighter1_12_186_Closed_Text" style="border: 1px solid rgb(128, 128, 128); display: none; background-color: rgb(255, 255, 255);">/**/</span><span id="Codehighlighter1_12_186_Open_Text"><span style="color: rgb(128, 128, 128);">///</span><span style="color: rgb(0, 128, 0);"></span><span style="color: rgb(128, 128, 128);"><summary></span><span style="color: rgb(0, 128, 0);">
<img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif"></span><span style="color: rgb(128, 128, 128);">///</span><span style="color: rgb(0, 128, 0);">根据Token来判断用户是否已被授权
<img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif"></span><span style="color: rgb(128, 128, 128);">///</span><span style="color: rgb(0, 128, 0);"></span><span style="color: rgb(0, 128, 0); text-decoration: underline;">http://pw.cnblogs.com</span><span style="color: rgb(0, 128, 0);">
<img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif"></span><span style="color: rgb(128, 128, 128);">///</span><span style="color: rgb(0, 128, 0);"></span><span style="color: rgb(128, 128, 128);"></summary></span><span style="color: rgb(0, 128, 0);">
<img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif"></span><span style="color: rgb(128, 128, 128);">///</span><span style="color: rgb(0, 128, 0);"></span><span style="color: rgb(128, 128, 128);"><paramname="token"></param></span><span style="color: rgb(0, 128, 0);">
<img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif"></span><span style="color: rgb(128, 128, 128);">///</span><span style="color: rgb(0, 128, 0);"></span><span style="color: rgb(128, 128, 128);"><returns></returns></span><span style="color: rgb(128, 128, 128);"></span></span>
<img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif"><span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 0, 255);">bool</span><span style="color: rgb(0, 0, 0);">CheckUserByToken(ITokentoken)
<img align="top" alt="" id="Codehighlighter1_246_547_Open_Image" src="http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif"><img align="top" alt="" id="Codehighlighter1_246_547_Closed_Image" style="display: none;" src="http://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif"></span><span id="Codehighlighter1_246_547_Closed_Text" style="border: 1px solid rgb(128, 128, 128); display: none; background-color: rgb(255, 255, 255);"><img alt="" src="http://www.cnblogs.com/Images/dot.gif"></span><span id="Codehighlighter1_246_547_Open_Text"><span style="color: rgb(0, 0, 0);">{
<img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif">ISecurityCacheProvidersc</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">SecurityCacheFactory.GetSecurityCacheProvider(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">CacheManager</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">);
<img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif"><img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif"></span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">得到Token对应的身份IIdentity</span><span style="color: rgb(0, 128, 0);">
<img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif"></span><span style="color: rgb(0, 0, 0);">IIdentitysavedIdentity</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">sc.GetIdentity(token);
<img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif"></span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">返回该身份是否已被授权</span><span style="color: rgb(0, 128, 0);">
<img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif"></span><span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);">savedIdentity.IsAuthenticated;
<img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif"><img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif">}</span></span><span style="color: rgb(0, 0, 0);"></span>
</div>
3、结束User Session(即使Token过期)
<div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; font-size: 13px; width: 98%; background-color: rgb(238, 238, 238);">
<img align="top" alt="" id="Codehighlighter1_8_11_Open_Image" src="http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif"><img align="top" alt="" id="Codehighlighter1_8_11_Closed_Image" style="display: none;" src="http://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif"><span style="color: rgb(0, 0, 0);"></span><span id="Codehighlighter1_8_11_Closed_Text" style="border: 1px solid rgb(128, 128, 128); display: none; background-color: rgb(255, 255, 255);">/**/</span><span id="Codehighlighter1_8_11_Open_Text"><span style="color: rgb(0, 128, 0);">/**/</span></span><span id="Codehighlighter1_12_105_Closed_Text" style="border: 1px solid rgb(128, 128, 128); display: none; background-color: rgb(255, 255, 255);">/**/</span><span id="Codehighlighter1_12_105_Open_Text"><span style="color: rgb(128, 128, 128);">///</span><span style="color: rgb(0, 128, 0);"></span><span style="color: rgb(128, 128, 128);"><summary></span><span style="color: rgb(0, 128, 0);">
<img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif"></span><span style="color: rgb(128, 128, 128);">///</span><span style="color: rgb(0, 128, 0);">使用户Token过期
<img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif"></span><span style="color: rgb(128, 128, 128);">///</span><span style="color: rgb(0, 128, 0);"></span><span style="color: rgb(0, 128, 0); text-decoration: underline;">http://pw.cnblogs.com</span><span style="color: rgb(0, 128, 0);">
<img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif"></span><span style="color: rgb(128, 128, 128);">///</span><span style="color: rgb(0, 128, 0);"></span><span style="color: rgb(128, 128, 128);"></summary></span><span style="color: rgb(128, 128, 128);"></span></span>
<img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif"><span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);">CloseUserSession()
<img align="top" alt="" id="Codehighlighter1_153_419_Open_Image" src="http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif"><img align="top" alt="" id="Codehighlighter1_153_419_Closed_Image" style="display: none;" src="http://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif"></span><span id="Codehighlighter1_153_419_Closed_Text" style="border: 1px solid rgb(128, 128, 128); display: none; background-color: rgb(255, 255, 255);"><img alt="" src="http://www.cnblogs.com/Images/dot.gif"></span><span id="Codehighlighter1_153_419_Open_Text"><span style="color: rgb(0, 0, 0);">{
<img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif">ISecurityCacheProvidersc</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">SecurityCacheFactory.GetSecurityCacheProvider(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">CacheManager</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">);
<img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif"></span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">保存身份</span><span style="color: rgb(0, 128, 0);">
<img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif"></span><span style="color: rgb(0, 0, 0);">ITokentoken</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">sc.SaveIdentity(</span><span style="color: rgb(0, 0, 255);">new</span><span style="color: rgb(0, 0, 0);">GenericIdentity(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">SHY520</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">));
<img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif"></span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">使身份过期</span><span style="color: rgb(0, 128, 0);">
<img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif"></span><span style="color: rgb(0, 0, 0);">sc.ExpireIdentity(token);
<img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif">}</span></span>
</div>
4、判断用户是否满足一定的规则
首先需要导入下面的命名空间:
using System.Security;
using System.Security.Principal;
using Microsoft.Practices.EnterpriseLibrary.Security;
<div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; font-size: 13px; width: 98%; background-color: rgb(238, 238, 238);">
<img align="top" alt="" id="Codehighlighter1_4_7_Open_Image" src="http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif"><img align="top" alt="" id="Codehighlighter1_4_7_Closed_Image" style="display: none;" src="http://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif"><span style="color: rgb(0, 0, 0);"></span><span id="Codehighlighter1_4_7_Closed_Text" style="border: 1px solid rgb(128, 128, 128); display: none; background-color: rgb(255, 255, 255);">/**/</span><span id="Codehighlighter1_4_7_Open_Text"><span style="color: rgb(0, 128, 0);">/**/</span></span><span id="Codehighlighter1_8_272_Closed_Text" style="border: 1px solid rgb(128, 128, 128); display: none; background-color: rgb(255, 255, 255);">/**/</span><span id="Codehighlighter1_8_272_Open_Text"><span style="color: rgb(128, 128, 128);">///</span><span style="color: rgb(0, 128, 0);"></span><span style="color: rgb(128, 128, 128);"><summary></span><span style="color: rgb(0, 128, 0);">
<img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif"></span><span style="color: rgb(128, 128, 128);">///</span><span style="color: rgb(0, 128, 0);">判断用户是否满足一定的规则
<img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif"></span><span style="color: rgb(128, 128, 128);">///</span><span style="color: rgb(0, 128, 0);"></span><span style="color: rgb(0, 128, 0); text-decoration: underline;">http://pw.cnblogs.com</span><span style="color: rgb(0, 128, 0);">
<img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif"></span><span style="color: rgb(128, 128, 128);">///</span><span style="color: rgb(0, 128, 0);"></span><span style="color: rgb(128, 128, 128);"></summary></span><span style="color: rgb(0, 128, 0);">
<img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif"></span><span style="color: rgb(128, 128, 128);">///</span><span style="color: rgb(0, 128, 0);"></span><span style="color: rgb(128, 128, 128);"><paramname="username"></span><span style="color: rgb(0, 128, 0);">用户名称</span><span style="color: rgb(128, 128, 128);"></param></span><span style="color: rgb(0, 128, 0);">
<img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif"></span><span style="color: rgb(128, 128, 128);">///</span><span style="color: rgb(0, 128, 0);"></span><span style="color: rgb(128, 128, 128);"><paramname="role"></span><span style="color: rgb(0, 128, 0);">角色名称</span><span style="color: rgb(128, 128, 128);"></param></span><span style="color: rgb(0, 128, 0);">
<img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif"></span><span style="color: rgb(128, 128, 128);">///</span><span style="color: rgb(0, 128, 0);"></span><span style="color: rgb(128, 128, 128);"><paramname="rule"></span><span style="color: rgb(0, 128, 0);">规则名称</span><span style="color: rgb(128, 128, 128);"></param></span><span style="color: rgb(0, 128, 0);">
<img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif"></span><span style="color: rgb(128, 128, 128);">///</span><span style="color: rgb(0, 128, 0);"></span><span style="color: rgb(128, 128, 128);"><returns></returns></span><span style="color: rgb(128, 128, 128);"></span></span>
<img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif"><span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 0, 255);">bool</span><span style="color: rgb(0, 0, 0);">CheckUserRoleByRule(</span><span style="color: rgb(0, 0, 255);">string</span><span style="color: rgb(0, 0, 0);">username,</span><span style="color: rgb(0, 0, 255);">string</span><span style="color: rgb(0, 0, 0);">role,</span><span style="color: rgb(0, 0, 255);">string</span><span style="color: rgb(0, 0, 0);">rule)
<img align="top" alt="" id="Codehighlighter1_364_706_Open_Image" src="http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif"><img align="top" alt="" id="Codehighlighter1_364_706_Closed_Image" style="display: none;" src="http://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif"></span><span id="Codehighlighter1_364_706_Closed_Text" style="border: 1px solid rgb(128, 128, 128); display: none; background-color: rgb(255, 255, 255);"><img alt="" src="http://www.cnblogs.com/Images/dot.gif"></span><span id="Codehighlighter1_364_706_Open_Text"><span style="color: rgb(0, 0, 0);">{
<img align="top" alt="" id="Codehighlighter1_466_473_Open_Image" src="http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif"><img align="top" alt="" id="Codehighlighter1_466_473_Closed_Image" style="display: none;" src="http://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif">IPrincipalprincipal</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 0, 255);">new</span><span style="color: rgb(0, 0, 0);">GenericPrincipal(</span><span style="color: rgb(0, 0, 255);">new</span><span style="color: rgb(0, 0, 0);">GenericIdentity(username),</span><span style="color: rgb(0, 0, 255);">new</span><span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 0, 255);">string</span><span style="color: rgb(0, 0, 0);">[]</span><span id="Codehighlighter1_466_473_Closed_Text" style="border: 1px solid rgb(128, 128, 128); display: none; background-color: rgb(255, 255, 255);"><img alt="" src="http://www.cnblogs.com/Images/dot.gif"></span><span id="Codehighlighter1_466_473_Open_Text"><span style="color: rgb(0, 0, 0);">{role}</span></span><span style="color: rgb(0, 0, 0);">);
<img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif"><img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif"></span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">创建一个默认的Authorization实例</span><span style="color: rgb(0, 128, 0);">
<img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif"></span><span style="color: rgb(0, 0, 0);">IAuthorizationProviderautoprovider</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">AuthorizationFactory.GetAuthorizationProvider();
<img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif"><img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif"></span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">返回验证是否通过</span><span style="color: rgb(0, 128, 0);">
<img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif"></span><span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);">autoprovider.Authorize(principal,rule);
<img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif">}</span></span>
</div>
通过上面的方法可以很简单的实现对用户权限的鉴定,关于Security Application Block的简单应用就说到这里,下面我们来看一下如何实现自定义的authorization provider。
1、首先我们建立自己的Provider类,该类需继承AuthorizationProvider;
2、并且要加上[ConfigurationElementType(typeof(CustomAuthorizationProviderData))]的Attribute;
3、添加构造函数,参数为NameValueCollection类型;
4、覆盖基类中的Authorize方法,在其中添加自己的验证是否授权的逻辑
这样就完成了一个自定义的authorization provider。
完整的类定义如下:
<div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; font-size: 13px; width: 98%; background-color: rgb(238, 238, 238);">
<img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif"><span style="color: rgb(0, 0, 255);">using</span><span style="color: rgb(0, 0, 0);">System;
<img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif"></span><span style="color: rgb(0, 0, 255);">using</span><span style="color: rgb(0, 0, 0);">System.Collections.Generic;
<img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif"></span><span style="color: rgb(0, 0, 255);">using</span><span style="color: rgb(0, 0, 0);">System.Text;
<img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif"></span><span style="color: rgb(0, 0, 255);">using</span><span style="color: rgb(0, 0, 0);">Microsoft.Practices.EnterpriseLibrary.Security;
<img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif"></span><span style="color: rgb(0, 0, 255);">using</span><span style="color: rgb(0, 0, 0);">System.Collections.Specialized;
<img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif"></span><span style="color: rgb(0, 0, 255);">using</span><span style="color: rgb(0, 0, 0);">System.Security.Principal;
<img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif"></span><span style="color: rgb(0, 0, 255);">using</span><span style="color: rgb(0, 0, 0);">Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
<img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif"></span><span style="color: rgb(0, 0, 255);">using</span><span style="color: rgb(0, 0, 0);">Microsoft.Practices.EnterpriseLibrary.Security.Configuration;
<img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif"><img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif"></span><span style="color: rgb(0, 0, 255);">namespace</span><span style="color: rgb(0, 0, 0);">Enterprise_Library_2
<img align="top" alt="" id="Codehighlighter1_358_767_Open_Image" src="http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif"><img align="top" alt="" id="Codehighlighter1_358_767_Closed_Image" style="display: none;" src="http://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif"></span><span id="Codehighlighter1_358_767_Closed_Text" style="border: 1px solid rgb(128, 128, 128); display: none; background-color: rgb(255, 255, 255);"><img alt="" src="http://www.cnblogs.com/Images/dot.gif"></span><span id="Codehighlighter1_358_767_Open_Text"><span style="color: rgb(0, 0, 0);">{
<img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif">[ConfigurationElementType(</span><span style="color: rgb(0, 0, 255);">typeof</span><span style="color: rgb(0, 0, 0);">(CustomAuthorizationProviderData))]
<img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif"></span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 0, 255);">class</span><span style="color: rgb(0, 0, 0);">CustomProvider:AuthorizationProvider
<img align="top" alt="" id="Codehighlighter1_492_765_Open_Image" src="http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif"><img align="top" alt="" id="Codehighlighter1_492_765_Closed_Image" style="display: none;" src="http://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif"></span><span id="Codehighlighter1_492_765_Closed_Text" style="border: 1px solid rgb(128, 128, 128); display: none; background-color: rgb(255, 255, 255);"><img alt="" src="http://www.cnblogs.com/Images/dot.gif"></span><span id="Codehighlighter1_492_765_Open_Text"><span style="color: rgb(0, 0, 0);">{
<img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif"></span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);">CustomProvider(NameValueCollectionconfigurationItems)
<img align="top" alt="" id="Codehighlighter1_572_595_Open_Image" src="http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif"><img align="top" alt="" id="Codehighlighter1_572_595_Closed_Image" style="display: none;" src="http://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif"></span><span id="Codehighlighter1_572_595_Closed_Text" style="border: 1px solid rgb(128, 128, 128); display: none; background-color: rgb(255, 255, 255);"><img alt="" src="http://www.cnblogs.com/Images/dot.gif"></span><span id="Codehighlighter1_572_595_Open_Text"><span style="color: rgb(0, 0, 0);">{
<img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif"><img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif">}</span></span><span style="color: rgb(0, 0, 0);">
<img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif"><img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif"></span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 0, 255);">override</span><span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 0, 255);">bool</span><span style="color: rgb(0, 0, 0);">Authorize(IPrincipalprincipal,</span><span style="color: rgb(0, 0, 255);">string</span><span style="color: rgb(0, 0, 0);">context)
<img align="top" alt="" id="Codehighlighter1_683_758_Open_Image" src="http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif"><img align="top" alt="" id="Codehighlighter1_683_758_Closed_Image" style="display: none;" src="http://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif"></span><span id="Codehighlighter1_683_758_Closed_Text" style="border: 1px solid rgb(128, 128, 128); display: none; background-color: rgb(255, 255, 255);"><img alt="" src="http://www.cnblogs.com/Images/dot.gif"></span><span id="Codehighlighter1_683_758_Open_Text"><span style="color: rgb(0, 0, 0);">{
<img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif"></span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">TODO:在此处添加自己需要的验证授权的逻辑既可</span><span style="color: rgb(0, 128, 0);">
<img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif"></span><span style="color: rgb(0, 0, 0);">
<img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif"></span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 0, 255);">true</span><span style="color: rgb(0, 0, 0);">;
<img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif">}</span></span><span style="color: rgb(0, 0, 0);">
<img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif"><img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif">}</span></span><span style="color: rgb(0, 0, 0);">
<img align="top" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif">}</span></span>
</div>
<!--Beging Temp Save--> <script type="text/javascript">
function pageLoad()
{
Sys.WebForms.PageRequestManager.getInstance().add_initializeRequest(handleInitializeRequest);
//Sys.WebForms.PageRequestManager.getInstance().add_endRequest(handleEndRequest);
}
function handleInitializeRequest(sender, args)
{
var prm = Sys.WebForms.PageRequestManager.getInstance();
var eid = args.get_postBackElement().id;
if (eid.indexOf("DeleteLink")>0)
{
args.get_postBackElement().innerHTML = "<font color='red'>正在删除...</font>";
}
else if (eid.indexOf("btnSubmit")>0)
{
document.getElementById("AjaxHolder_PostComment_ltSubmitMsg").innerHTML="正在提交...";
document.getElementById("AjaxHolder_PostComment_btnSubmit").disabled = true;
}
else if(eid.indexOf("refreshList")>0)
{
document.getElementById("AjaxHolder_PostComment_refreshList").innerHTML="<font color='red'>正在刷新...</font>";
}
}
function TempSave(ElementID)
{
try
{
CommentsPersistDiv.setAttribute("CommentContent",document.getElementById(ElementID).value);
CommentsPersistDiv.save("CommentXMLStore");
}
catch(ex)
{
}
}
function Restore(ElementID)
{
CommentsPersistDiv.load("CommentXMLStore");
document.getElementById(ElementID).value=CommentsPersistDiv.getAttribute("CommentContent");
}
</script><!--Ene TempSave-->
相关推荐
Enterprise Library --Data Access Application Block整理(入门,示例)
Enterprise Library 2.0系列文章(中文) 所有文章来自:http://www.cnblogs.com/Terrylee/ (我对2.0的资料打包放在一起) 很感谢TerryLee给我们讲解和翻译了这么好的资料 包括: 1.Enterprise Library 2.0 学习 2....
Enterprise Library2.0.chm
Enterprise Library for .NET Framework 2.0 - January 2006 下载地址: http://www.microsoft.com/downloads/details.aspx?FamilyId=5A14E870-406B-4F2A-B723-97BA84AE80B5&displaylang=en<br>
企业库Enterprise Library 4.0 - May 2008.part3.rar 安装Enterprise支持文件 企业库Enterprise Library 4.0 - May 2008.msi 压缩分三卷输“企业库Enterprise Library 4.0 - May 2008.part3.rar”查到对应压缩卷3
Enterprise Library2.0.chm 微软企业库2.0的使用文档。
Enterprise Library 2.0 Core Architecture 这个东西蛮有用的
企业库Enterprise Library 4.0 - May 2008.part1.rar 安装Enterprise支持文件 企业库Enterprise Library 4.0 - May 2008.msi
牛人写的Enterprise Library2.0最新整理教程
微软模式&实践团队的开源作品 希望学习wcsf的朋友必不可少的类库哦
企业库Enterprise Library 4.0 - May 2008.part1.rar 安装Enterprise支持文件 企业库Enterprise Library 4.0 - May 2008.msi
Red_Hat_Enterprise_Linux-6-Security-Enhanced_Linux-en-US
Oracle数据库安装在RedHat Linux Enterprise操作系统下setarch-2.0-1.1.i386.rpm
企业库Enterprise Library 4.0 - May 2008.msi 完整版,直接解压安装使用
Enterprise Library 2.0 Hands On Lab 翻译(1):数据访问程序块(一)
Enterprise Library – Data Access Application Block 学习手册(最新版) - Part 1 Enterprise Library for .Net Framework 3.5 – EntLib v4.1 是 patterns & practices 小组为.NET Framework 3.5 开发一套企业库...
Microsoft Enterprise Library is a collection of reusable application blocks designed to assist software developers with common enterprise development challenges. This release includes: Data Access ...
security-enterprise-FrontBackSplit_