Din*_*uka 2 asp.net web-services web-applications azure azure-storage
我知道您可以使用以下方法获取连接字符串:
<add key="StorageConnectionString" value="DefaultEndpointsProtocol=https;AccountName=mystorage;AccountKey=MY_ACCOUNT_KEY" />
Run Code Online (Sandbox Code Playgroud)
然后使用以下方法检索它:
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("StorageConnectionString"));
Run Code Online (Sandbox Code Playgroud)
而且我知道如何使用SqlConnectionStringBuilder建立连接字符串:
System.Data.SqlClient.SqlConnectionStringBuilder builder = new System.Data.SqlClient.SqlConnectionStringBuilder();
builder["DefaultEndpointsProtocol"] = "https";
builder["AccountName"] = "mystorage";
builder["AccountKey"] = "MY_ACCOUNT_KEY";
string conString = builder.ConnectionString;
Run Code Online (Sandbox Code Playgroud)
但是,这显然不适用于存储连接字符串。它说SqlConnectionStringBuilder不支持DefaultEndpointsProtocol或我指定的3个键中的任何一个。如何使用这些键制作字符串?
使用带有对象的CloudStorageAccount构造StorageCredentials函数。然后使用ToString(boolean)重载获取连接字符串。
var accountName = "myAccount";
var keyValue = "c3RyaW5nIGxlbmd0aCB2YWxpZA==";
var useHttps = true;
var exportSecrets = true;
var storageCredentials = new StorageCredentials(accountName, keyValue);
var storageAccount = new CloudStorageAccount(storageCredentials, useHttps);
var connString = storageAccount.ToString(exportSecrets);
Run Code Online (Sandbox Code Playgroud)
StorageCredentials构造函数(字符串,字符串)。使用StorageCredentials指定的帐户名和键值初始化该类的新实例。
CloudStorageAccount构造函数(StorageCredentials,布尔值)。使用指定的凭据初始化CloudStorageAccount类的新实例,并指定使用HTTP还是HTTPS连接到存储服务。
CloudStorageAccount.ToString方法(布尔)。返回存储帐户的连接字符串,可以选择包含敏感数据。
不确定为什么不使用您正在使用的第一个选项(也许您想将它们在配置文件中分开),但在任何情况下您都可以将它们连接在一起来构建字符串:
var storageConnectionString = String.format("DefaultEndpointsProtocol={0};AccountName={1};AccountKey={2}", "https", "MyAccountName","MyAccountKey");
CloudStorageAccount.Parse(storageConnectionString);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3466 次 |
| 最近记录: |