数据保护密钥不持久到Azure

She*_*per 2 c# asp.net azure asp.net-core

我在Azure VM的IIS中托管了一个ASP.NET Core应用程序。我将扩展方法称为持久化方法,以保留它们对Azure的密钥,但该处没有存储任何内容,并且该应用程序似乎仍在使用本地密钥。我在这里按照以下示例操作:http : //intellitect.com/staying-logged-across-azure-app-service-swap/。事实上,当我在控制台应用程序中测试代码时,持久性可以正常工作。但是,当我使用此代码运行ASP.NET Core应用程序时,Azure无法持久存在。有什么想法吗?

Amo*_*mor 5

请检查注册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();
Run Code Online (Sandbox Code Playgroud)