2016-10-04 195 views
2

我有兩個身份認證服務器和一個Web API。 我試圖做的是讓API使用IdentityServers中的一個或兩個進行身份驗證,並且在出現故障時能夠切換。如果可能,我還想在運行時添加一個新的IdentityServer。多個IdentityServer4服務的最佳實踐

這裏有什麼最佳做法嗎?

截至目前看起來像這樣。

 app.UseIdentityServerAuthentication(new IdentityServerAuthenticationOptions 
     { 
      Authority = $"http://localhost:5000", 
      ScopeName = "my.scope", 
      RequireHttpsMetadata = false, 
      ScopeSecret = "secret", 
     }); 

如果我在端口5000關閉IdentityServer,我不能再使用該API。這是可以預料的。

+0

您應該在負載均衡器後面使用冗餘身份認證服務器。那麼網絡會照顧到這一點。 – leastprivilege

+0

我做了一個非常簡單的中間件,將localhost:5000上的呼叫重新路由到正確的身份服務器。這工作正常..一次(?)。之後,他直接進入了身份服務器,並忽略了我設置權限的任何內容。他是否在運行時更新了無故障狀態? – Evelie

+0

如果其中一個電話超時,我能否以某種方式告訴他重試管理局?即使我現在實現了一個正確的負載均衡器,它仍然會有相同的行爲,因爲IdentityServer會告訴使用哪個地址..所以在初始之後的所有調用都將繞過負載均衡器。我不介意這一點。但如果可能的話,我想在IdentityServer發生故障時重試。到目前爲止,即使使用負載平衡器,API也將與IdentityServer一起消亡。 – Evelie

回答

0

我不確定這是否是解決問題的好方法。但它是一種方式。 我問我的路由服務的「第一身份服務」設置選項中的Authroity。 然後我添加自定義IntrospectionBackChannelHandler

 app.UseIdentityServerAuthentication(new IdentityServerAuthenticationOptions 
     { 
      Authority = $"http://{v.Address}:{v.Port}", 
      IntrospectionBackChannelHandler = new CustomIntrospectionBackChannelHandler(consulService) 

因爲我所有的身份服務器看起來是一樣的,但在不同的地址,我真的不費心去再做管理局事情。

定製反思裏面....我檢查每個反思,並將其發送到「正確」 identityserver。如果它不工作,我嘗試另一個身份服務器。

var qs = await request.Content.ReadAsStringAsync(); var queryDic = QueryHelpers.ParseQuery(await request.Content.ReadAsStringAsync());

 var token = queryDic["token"]; 
     var client_id = queryDic["client_id"]; 
     var client_secret = queryDic["client_secret"]; 
     var iRequest = new IntrospectionRequest 
     { 
      ClientId = client_id, 
      ClientSecret = client_secret, 
      TokenTypeHint = "access_token", 
      Token = token 
     }; 

     IntrospectionResponse result = null; 

     var svc = await _Consul.GetService(OrbitServices.IdentityServer); 
     result = await TrySendAsync(iRequest, svc); 
     if (!result.IsActive && result.IsError) 
     { 
      svc = await _Consul.GetService(OrbitServices.IdentityServer, true); 
      result = await TrySendAsync(iRequest, svc); 
     } 

     var message = new HttpResponseMessage(HttpStatusCode.OK) 
     { 
      Content = new StringContent(result.Raw, Encoding.UTF8, "application/json") 
     }; 

     return message;