`
yzd
  • 浏览: 1813490 次
  • 性别: Icon_minigender_2
  • 来自: 北京
文章分类
社区版块
存档分类
最新评论

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);">&lt;</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);">/&gt;</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);">&lt;</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);">&gt;</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);">&lt;</span><span style="color: rgb(128, 0, 0);">authorizationProviders</span><span style="color: rgb(0, 0, 255);">&gt;</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);">&lt;</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);">&gt;</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);">&lt;</span><span style="color: rgb(128, 0, 0);">rules</span><span style="color: rgb(0, 0, 255);">&gt;</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);">&lt;</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);">/&gt;</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);">&lt;/</span><span style="color: rgb(128, 0, 0);">rules</span><span style="color: rgb(0, 0, 255);">&gt;</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);">&lt;/</span><span style="color: rgb(128, 0, 0);">add</span><span style="color: rgb(0, 0, 255);">&gt;</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);">&lt;/</span><span style="color: rgb(128, 0, 0);">authorizationProviders</span><span style="color: rgb(0, 0, 255);">&gt;</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);">&lt;/</span><span style="color: rgb(128, 0, 0);">securityConfiguration</span><span style="color: rgb(0, 0, 255);">&gt;</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);">&lt;summary&gt;</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);">&lt;/summary&gt;</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);">&lt;paramname="username"&gt;</span><span style="color: rgb(0, 128, 0);">用户名</span><span style="color: rgb(128, 128, 128);">&lt;/param&gt;</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);">&lt;paramname="password"&gt;</span><span style="color: rgb(0, 128, 0);">密码</span><span style="color: rgb(128, 128, 128);">&lt;/param&gt;</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);">&lt;returns&gt;&lt;/returns&gt;</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);">&lt;summary&gt;</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);">&lt;/summary&gt;</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);">&lt;summary&gt;</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);">&lt;/summary&gt;</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);">&lt;returns&gt;</span><span style="color: rgb(0, 128, 0);">角色列表</span><span style="color: rgb(128, 128, 128);">&lt;/returns&gt;</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);">&lt;</span><span style="color: rgb(128, 0, 0);">securityCacheProviders</span><span style="color: rgb(0, 0, 255);">&gt;</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);">&lt;</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);">/&gt;</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);">&lt;/</span><span style="color: rgb(128, 0, 0);">securityCacheProviders</span><span style="color: rgb(0, 0, 255);">&gt;</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);">&lt;summary&gt;</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);">&lt;/summary&gt;</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);">&lt;returns&gt;&lt;/returns&gt;</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);">&lt;summary&gt;</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);">&lt;/summary&gt;</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);">&lt;paramname="token"&gt;&lt;/param&gt;</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);">&lt;returns&gt;&lt;/returns&gt;</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);">&lt;summary&gt;</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);">&lt;/summary&gt;</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);">&lt;summary&gt;</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);">&lt;/summary&gt;</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);">&lt;paramname="username"&gt;</span><span style="color: rgb(0, 128, 0);">用户名称</span><span style="color: rgb(128, 128, 128);">&lt;/param&gt;</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);">&lt;paramname="role"&gt;</span><span style="color: rgb(0, 128, 0);">角色名称</span><span style="color: rgb(128, 128, 128);">&lt;/param&gt;</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);">&lt;paramname="rule"&gt;</span><span style="color: rgb(0, 128, 0);">规则名称</span><span style="color: rgb(128, 128, 128);">&lt;/param&gt;</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);">&lt;returns&gt;&lt;/returns&gt;</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>

               &lt;!--Beging Temp Save--&gt; &lt;script type="text/javascript"&gt;
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")&gt;0)
    {
       args.get_postBackElement().innerHTML = "&lt;font color='red'&gt;正在删除...&lt;/font&gt;";        
    }   
    else if (eid.indexOf("btnSubmit")&gt;0)
    {
       document.getElementById("AjaxHolder_PostComment_ltSubmitMsg").innerHTML="正在提交...";
       document.getElementById("AjaxHolder_PostComment_btnSubmit").disabled = true;      
    }  
    else if(eid.indexOf("refreshList")&gt;0)
    {
        document.getElementById("AjaxHolder_PostComment_refreshList").innerHTML="&lt;font color='red'&gt;正在刷新...&lt;/font&gt;";
    }
  
}

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");
}


&lt;/script&gt;&lt;!--Ene TempSave--&gt;
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics