2016-11-15 46 views
0

Service Fabric无状态服务支持基于声明的授权吗?如何将声明/ JWT传递给Service Fabric无状态服务?

比方说,我有一个网页api接收标题中的智威汤逊。我可以将JWT或索赔转交给服务架构无状态服务,以便在执行敏感操作之前可以对索赔进行一些检查吗?

我可以看到,我们可以通过使用ClaimsCredentials要求传递给服务:

var serviceProxyFactory = new ServiceProxyFactory(
    (callbackClient) => new FabricTransportServiceRemotingClientFactory(
     new FabricTransportSettings 
     { 
      SecurityCredentials = new ClaimsCredentials 
      { 
       LocalClaims = "[JWT HERE? or just Claims JSON?]" 
      } 
     })); 

IMyService service = serviceProxyFactory.CreateServiceProxy<IMyService>(new Uri("fabric:/MyThing/MyService")); 

https://msdn.microsoft.com/en-us/library/azure/system.fabric.claimscredentials.localclaims.aspx说LocalClaims是“索赔的字符串表示令牌从STS(安全令牌服务)获得的。”

另外:

  • 是ClaimsCredentials实际编码智威汤逊中的Base64,或者只是要求关键的JSON有效载荷:值属性?

  • 是否需要在无状态服务中完成特定的配置或代码?

  • 您如何获得无状态服务的索赔?

此刻,当我打电话的服务,我得到以下错误,不管什么样的价值没有我设置LocalClaims到:

System.Fabric.FabricCannotConnectException: Error in Connection during ServiceCommunication 
---> System.Runtime.InteropServices.COMException: Exception from HRESULT: 0x80071C4C\r\n 
at Microsoft.ServiceFabric.Services.Communication.FabricTransport.Common.NativeServiceCommunication.IFabricServiceCommunicationClient2.EndRequest(IFabricAsyncOperationContext context)\r\n 
at Microsoft.ServiceFabric.Services.Communication.FabricTransport.Client.NativeServiceCommunicationClient.EndRequest(IFabricAsyncOperationContext context)\r\n at System.Fabric.Interop.AsyncCallOutAdapter2`1.Finish(IFabricAsyncOperationContext context, Boolean expectedCompletedSynchronously)\r\n --- End of inner exception stack trace ---\r\n 
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n 
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n 
at Microsoft.ServiceFabric.Services.Communication.FabricTransport.Client.NativeServiceCommunicationClient.<RequestResponseAsync>d__8.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at Microsoft.ServiceFabric.Services.Communication.Client.ServicePartitionClient`1.<InvokeWithRetryAsync>d__7`1.MoveNext() 

谢谢!

回答

1

我不认为ClaimsCredentials类旨在携带JWT令牌,而是由服务架构自身生成的安全令牌(基于某些证书)。如果您检查课程ClaimsCredentials,您会发现它与携带声明信息的本地课程紧密相关。更深的挖掘,揭示的是用于传递信息的结构:

[StructLayout(LayoutKind.Sequential, Pack = 8)] 
internal struct FABRIC_CLAIMS_CREDENTIALS 
{ 
    public uint ServerCommonNameCount; 
    public IntPtr ServerCommonNames; 
    public uint IssuerThumbprintCount; 
    public IntPtr IssuerThumbprints; 
    public IntPtr LocalClaims; 
    public NativeTypes.FABRIC_PROTECTION_LEVEL ProtectionLevel; 
    public IntPtr Reserved; 
} 

[StructLayout(LayoutKind.Sequential, Pack = 8)] 
internal struct FABRIC_CLAIMS_CREDENTIALS_EX1 
{ 
    public uint ServerThumbprintCount; 
    public IntPtr ServerThumbprints; 
    public IntPtr Reserved; 
} 

有很多关于这个类的尖叫内部并没有什么表示任何关系,或者,JWTs理解。

如果你想在你的HTTP端点添加某种形式的安全,那么你应该可以使用System.IdentityModel.Tokens.JwtSecurityTokenHandler,看看这个SO回答开始:Decoding and verifying JWT token using System.IdentityModel.Tokens.Jwt

如果要将基于JWT的安全性添加到Fabric Transport端点,那么您可能需要使用ServiceRemotingMessageHeaders将JWT作为自定义标头添加到客户端,然后解析并验证侦听器上的该标头。使用提供和记录的X509证书进行服务远程处理的安全模型可能是此运输思想的更好选择。