2017-08-15 88 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();