c# - 使用 Azure 存储模拟器测试开发

标签 c# azure asp.net-core azure-storage azure-emulator

我正在使用 Azure 存储模拟器版本在 Windows 10-x64 和 WindowsAzure.Storage 9.1.1 上测试 asp.net core 2.1-rc1 Web 应用程序。

我已按照本指南设置 Azure 存储模拟器。

https://learn.microsoft.com/en-us/azure/storage/common/storage-use-emulator

当我尝试创建 blob 容器时,出现此异常:

Microsoft.WindowsAzure.Storage.StorageException: Server failed to authenticate the request. Make sure the value of Authorization header is formed correctly including the signature.
at Microsoft.WindowsAzure.Storage.Core.Executor.Executor.ExecuteAsyncInternal[T](RESTCommand`1 cmd, IRetryPolicy policy, OperationContext operationContext, CancellationToken token) in C:\Program Files (x86)\Jenkins\workspace\release_dotnet_master\Lib\WindowsRuntime\Core\Executor\Executor.cs:line 316
at Microsoft.WindowsAzure.Storage.Blob.CloudBlobContainer.CreateIfNotExistsAsync(BlobContainerPublicAccessType accessType, BlobRequestOptions options, OperationContext operationContext, CancellationToken cancellationToken) in C:\Program Files (x86)\Jenkins\workspace\release_dotnet_master\Lib\WindowsRuntime\Blob\CloudBlobContainer.cs:line 165

Request Information
RequestID:c4a44dcc-301e-002e-5ad0-f2637a000000
RequestDate:Wed, 23 May 2018 22:02:36 GMT
StatusMessage:Server failed to authenticate the request. Make sure the value of Authorization header is formed correctly including the signature.
ErrorCode:AuthenticationFailed
ErrorMessage:Server failed to authenticate the request. Make sure the value of Authorization header is formed correctly including the signature.
RequestId:c4a44dcc-301e-002e-5ad0-f2637a000000
Time:2018-05-23T20:02:36.9987818Z

我在异常附加信息中发现了这一点:-

"The MAC signature found in the HTTP request 'NeZGAEspShTRdpc/zFH++pS9YChlOczzEg0vcVGXF10=' is not the same as any computed signature. Server used following string to sign: 'PUT\n\n\n\n\n\n\n\n\n\n\n\nx-ms-client-request-id:e4b5b43c-ba27-45b1-8545-19db1c16a160\nx-ms-date:Wed, 23 May 2018 19:10:31 GMT\nx-ms-version:2017-07-29\n/devstoreaccount1/admins\nrestype:container'."

这是我的代码:-

public static async Task InitializeContainersAsync()
{
    try
    {
        //Use the emulator default credenitals
        var credentials = new StorageCredentials("devstoreaccount1", "Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw==");
        CloudStorageAccount storageAccount = new CloudStorageAccount(credentials, false);

        CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();

        CloudBlobContainer container = blobClient.GetContainerReference("testing_container");

        await container.CreateIfNotExistsAsync();
    }
    catch (StorageException ex)
    {
        Console.WriteLine(ex);
    }
}

最佳答案

如果您想使用存储模拟器,实际上有两个选择。

  1. 最简单的一种是使用连接字符串 UseDevelopmentStorage=true

The easiest way to connect to the storage emulator from your application is to configure a connection string in your application's configuration file that references the shortcut UseDevelopmentStorage=true. Here's an example of a connection string to the storage emulator in an app.config file:

<appSettings>
  <add key="StorageConnectionString" value="UseDevelopmentStorage=true" />
</appSettings>
  • 如果您想自己构建连接字符串,您还需要指定要连接的服务的端点。摘自您链接到的文章:
  • To create a connection string that references the emulator account name and key, you must specify the endpoints for each of the services you wish to use from the emulator in the connection string. This is necessary so that the connection string will reference the emulator endpoints, which are different than those for a production storage account. For example, the value of your connection string will look like this:

    DefaultEndpointsProtocol=http;AccountName=devstoreaccount1;
    AccountKey=Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw==;
    BlobEndpoint=http://127.0.0.1:10000/devstoreaccount1;
    TableEndpoint=http://127.0.0.1:10002/devstoreaccount1;
    QueueEndpoint=http://127.0.0.1:10001/devstoreaccount1;
    

    关于c# - 使用 Azure 存储模拟器测试开发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50496798/

    相关文章:

    azure - 无法使用 Azure APIM 交互式门户向后端 API 发送测试请求

    c# - services.AddSingleton<IConfiguration> 在 .NET Core 中的用途

    asp.net-core - Asp.net 核心身份更改用户名/电子邮件

    C# POST 请求到 webapi 使用控制台程序

    c# - 如何返回能够在服务器上执行的对象?

    azure - 以特定方式自定义 Azure 搜索评分

    c# - 抽象类中的依赖注入(inject),ASP.NET Core

    c# - 团队环境 Entity Framework 代码优先 - 仅删除 _MigrationHistory 表以将模型项目重新同步到数据库是否合适?

    c# - 如何重写 SHA1 生成的密码哈希(在 ASP.NET Identity 中)?

    azure - 是否可以使用 @azure/cosmos sdk 创建新的 CosmosDB 帐户?