I、身份(Identity)与安全主体(SecurityPrincipal)
从两个重要的概念谈起:Identity与Principal[上篇]
从两个重要的概念谈起:Identity与Principal[下篇]
WCF的三种授权模式
基于Windows用户组的授权方式[上篇]
基于Windows用户组的授权方式[下篇]
模拟(Impersonation)与委托(Delegation)
模拟在WCF中的应用
ASP.NET Roles授权[上篇]
ASP.NET Roles授权[下篇]
通过扩展自行实现服务授权[提供源码下载]
利用WCF自定义授权模式提供当前Principal[原理篇]
利用WCF自定义授权模式提供当前Principal[实例篇]
WCF自定义授权体系详解[原理篇]
WCF自定义授权体系详解[实例篇]
为了让读者对基于Windows用户组的授权具有深刻的认识,接下来我们通过一个简单的事例来讲解在真正的应用中该授权模式如何使用。对于接下来演示的事例,我们将采用Windows认证和授权。至于授权的最终实现,我们采用的是在服务方法上面应用PrincipalPermissionAttribute特性方式的声明式授权。
目录:
步骤一、创建测试帐号
步骤二、创建服务契约和服务
步骤三、寄宿服务
步骤四、创建客户端程序
在创建事例解决方案之前我们先完成相应的准备工作,创建两个测试用的Windows帐号。假设两个帐号的名称分别为Foo和Bar,密码为Password。然后将帐号Foo添加到管理员(wcflogger)用户组中。
在svc 服务方法中添加属性: [PrincipalPermission(SecurityAction.Demand, Role ="wcfgroup")]
其中:wcflogger为之前创建好的Windows用户组;
///<summary>
/// PrincipalPermission权限控制
///</summary>
///<returns></returns>
[PrincipalPermission(SecurityAction.Demand, Role = "wcflogger")]
publicstringgetrolegroup()
{
return"wcflogger";
}
步骤三、寄宿服务
现在通过Hosting这个控制台程序对上面创建的服务进行寄宿。下面给出的是整个寄宿程序的配置。从该配置我们可以看到,服务唯一的终结点采用的绑定类型为WS2007HttpBinding。而在默认的情况下,WS2007HttpBinding采用Message安全模式和Windows认证方式。此外,基于UseWindowsGroups安全主体权限模式的ServiceAuthorization服务行为被应用到了该服务上。
服务端web.config 如下节点,添加相关配置:
system.serviceModel:
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior>
<!--为避免泄漏元数据信息,请在部署前将以下值设置为 false-->
<serviceMetadatahttpGetEnabled="true"httpsGetEnabled="true"/>
<!--要接收故障异常详细信息以进行调试,请将以下值设置为 true。在部署前设置为 false 以避免泄漏异常信息-->
<serviceDebugincludeExceptionDetailInFaults="false"/>
</behavior>
<behavior name="useWindowsGroupsAuthorization">
<serviceAuthorizationprincipalPermissionMode="UseWindowsGroups"/>
<serviceMetadatahttpGetEnabled="true" />
</behavior>
</serviceBehaviors>
</behaviors>
<protocolMapping>
<addbinding="ws2007HttpBinding"scheme="https"/>
</protocolMapping>
<serviceHostingEnvironmentaspNetCompatibilityEnabled="true"multipleSiteBindingsEnabled="true"/>
<services>
<servicename="Logger.Service.HlianHome.HlianHomeLog"behaviorConfiguration="useWindowsGroupsAuthorization">
<endpointaddress=""binding="ws2007HttpBinding"contract="Logger.Service.HlianHome.IHlianHomeLog"/>
</service>
</services>
</system.serviceModel>
注意: 上面name,contract对应的命名空间要与服务的命名空间一致,address如果是IIS寄宿保持为空"" ;
步骤四、创建客户端程序
1、客户端web.config文件找到endpoint配置: WS2007HttpBinding_IHlianHomeLog
<system.serviceModel>
<bindings>
<ws2007HttpBinding>
<binding name="WS2007HttpBinding_IHlianHomeLog" maxBufferPoolSize="524288"
maxReceivedMessageSize="2147483647">
<security mode="Message">
<transport clientCredentialType="Windows" />
<message clientCredentialType="Windows" />
</security>
</binding>
</ws2007HttpBinding>
</bindings>
<client>
<endpoint address="http://lxxxxxxxcom/Service/HlianHxxxmeLog.svc"
binding="ws2007HttpBinding" bindingConfiguration="WS2007HttpBinding_IHlianHomeLog"
contract="HlianHomeLog.IHlianHomeLog" name="WS2007HttpBinding_IHlianHomeLog" />
</client>
2、代码示例:
publicActionResultIndex()
{
ChannelFactory<IHlianHomeLog> channelFactory =newChannelFactory<IHlianHomeLog>("BasicHttpBinding_IHlianHomeLog");
NetworkCredential credential =channelFactory.Credentials.Windows.ClientCredential;
credential.UserName ="foo"; //windows用户
credential.Password ="password";//windows用户对应密码
IHlianHomeLog logger = channelFactory.CreateChannel(); //创建服务实例;
logger.InfoLog("","aaaaaaaaa");//完成服务调用
return View();
}