2017-03-06 46 views
1

我有一個.NET Core應用程序,Angular2 UI運行在使用OpenIddict保護的服務結構集羣中。我跟着這個例子:https://github.com/openiddict/openiddict-samples/tree/master/samples/RefreshFlowOpenIddict:401錯誤,當兩個或更多的服務實例數

它的工程很棒,當我只有一個無狀態的.NET核心應用程序的實例。當我將實例數增加到兩個時,身份驗證失敗,並且出現一堆401錯誤。看來我收到的令牌只對那個特定的實例有用,並且在另一個實例上被拒絕。 我想我明白爲什麼會發生這種情況,但我不知道如何解決它。

任何幫助將不勝感激。

+0

你解決了這個問題嗎?怎麼樣?我有同樣的問題。 – Makla

+0

抱歉,還沒有。我即將重新審視這一點,並將遵循下面列出的建議。 – Per

+0

我添加了一個適用於我的解決方案。 – Per

回答

1

OpenIddict依靠ASP.NET Core Data Protection stack生成並保護其授權碼,刷新令牌和訪問令牌(除非您明確選擇JWT作爲訪問令牌格式)。

要確保可以跨實例讀取這些標記,必須將ASP.NET Core Data Protection配置爲共享相同的密鑰環。如果您需要有關此過程的更多信息,我建議您閱讀the related documentation on Microsoft's website

+0

我認爲這將與任何需要在不同實例上解密數據的系統相關嗎?另外,從來沒有聽說過openiddict,目前使用idsrv4,必須看看。偉大的工作:) – Mardoxx

+1

@Mardoxx是的,它適用於所有使用ASP.NET核心數據保護的產品,其中包括身份驗證Cookie或使用'IDataProtector'加密自己的數據。 – Pinpoint

+0

我正在將此標記爲答案,因爲它指向了正確的方向。我在下面添加了一些關於服務結構集羣的說明。 – Per

0

我解決了這個看這個:https://docs.microsoft.com/en-us/aspnet/core/security/data-protection/implementation/key-storage-providers#azure-and-redis

以下是基本步驟我把:

  1. 創建一個用於存儲共享密鑰一個Blob存儲賬戶。

  2. 下面的代碼添加到您的Startup.cs:

var storageAccount = CloudStorageAccount.Parse("blob storage connection string"); 

//Create the blob client object. 
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient(); 

//Get a reference to a container to use for the sample code, and create it if it does not exist. 
CloudBlobContainer container = blobClient.GetContainerReference("mycontainer"); 
container.CreateIfNotExists(); 

services.AddDataProtection() 
    .SetApplicationName("MyApplication") 
    .PersistKeysToAzureBlobStorage(container, "myblobname"); 

就是這樣。