2016-11-15 42 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證書進行服務遠程處理的安全模型可能是此運輸思想的更好選擇。