2012-03-08 49 views
2

我正在关注Pluralsight video on AuthenticationWCF中的PrincipalPermission身份验证错误

我想简单PrinciplePermission认证添加到我的web服务:

[PrincipalPermission(SecurityAction.Demand, Role = "Computer\\Group")] 
    public String testDBConnection() 
    { 
     return "success"; 
    } 

在我的WCF客户端我送:

client.ClientCredentials.UserName.UserName = "Alice"; 
    client.ClientCredentials.UserName.Password = "alice"; 

我已经创建的组,加入爱丽丝它作为每个视频,但现在....

我不断收到错误:

“请求主体许可失败。”

任何想法有什么不对?

+0

当使用'ClientCredentials'发送用户名和密码时,我最近注意到信息在服务器端的'Thread.CurrentPrinciple'中不可用。 'PrinciplePermission'使用'Thread.CurrentPrinciple'来验证用户。我不知道你是否可以配置这个。 – Steven 2012-03-08 08:46:20

+0

@Steven谢谢你的回复,对不起,我不知道Thread.CurrentPriciple是什么,你能提供更多细节吗? – iKode 2012-03-08 08:57:23

+0

'Thread.CurrentThread.CurrentPrincipel'返回'IPrincipal'实例,通常是'WindowsPrincipel'。这包含正在运行线程的当前用户的信息。 – Steven 2012-03-08 10:11:25

回答

1

原理对象是一个只读对象,它在加载时被设置为应用程序的安全设置的一部分。我能够解决这个问题的方式是创建一个继承IPrincipal的类。您需要在WCF应用程序中执行此操作。

3

当请求到达时,您需要一些代码在服务器上创建IPrincipal

要做到这一点,最简单的方法可能是使用ASP.NET RoleProvider,在您需要配置的行为,例如:如果要配置

<system.web> 
    ... 
    <roleManager enabled="true" defaultProvider="MyRoleProvider"> 
    <providers> 
     <clear/> 
     <add name="MyRoleProvider" 
     ... 

<system.serviceModel> 
    ... 
    <behaviors> 
    <serviceBehaviors> 
     <behavior name=...> 
      ... 
      <serviceAuthorization principalPermissionMode="UseAspNetRoles" 
         roleProviderName="MyRoleProvider" /> 
     </behavior> 
    </serviceBehaviors> 
    ... 

和一个RoleProvider正确以这种方式,然后Thread.CurrentPrincipal将被自动设置,它应该工作。