2017-08-15 90 views
0

我有一個ASP.NET Core應用程序託管在Azure虛擬機中的IIS中。我調用擴展方法來將它們保存到Azure中,但沒有任何內容存儲在那裏,應用程序似乎仍然使用本地密鑰。我按照這裏的例子:http://intellitect.com/staying-logged-across-azure-app-service-swap/。事實上,當我在控制檯應用程序中測試代碼時,持久性工作正常。但是,當我使用此代碼運行ASP.NET Core應用程序時,沒有任何事情會持續到Azure。任何想法爲什麼?不保留給Azure的數據保護密鑰

回答

1

我將上面的回答標記爲答案,因爲它是正確的並且是重要的一點。但是,對於這個問題的真正答案是關鍵數據不會因爲應用程序啓動並且您調用PersistKeysToAzureBlobStorage(或任何其他PersistToXXX方法)而持久。這只是爲了配置數據保護。你可以說這是「懶惰」。關鍵數據將與您的代碼一起生成或框架首先調用Protect/Unprotect,如下所示:

var protector = provider.CreateProtector("Some Purpose"); 
var enc = protector.Protect("Hello World"); 
... 
protector.Unprotect(enc); 
2

請檢查註冊MVC服務和DataProtection服務的順序。註冊DataProtection服務必須在註冊MVC服務之前。以下代碼供您參考。

// Add DataProtection Service 
if (storageUrl != null && sasToken != null && containerName != null && applicationName != null && blobName != null) 
{ 
    // Create the new Storage URI 
    Uri storageUri = new Uri($"{storageUrl}{sasToken}"); 

    //Create the blob client object. 
    CloudBlobClient blobClient = new CloudBlobClient(storageUri); 

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

    services.AddDataProtection() 
     .SetApplicationName(applicationName) 
     .PersistKeysToAzureBlobStorage(container, blobName); 
} 

// Add framework services. 
services.AddMvc();