简体   繁体   English

无法读取存储帐户和密钥-Azure

[英]Unable to read Storage account and key - Azure

I'm trying to upload image to Blob. 我正在尝试将图像上传到Blob。 Hence in the Web.Config I have settings like this: 因此,在Web.Config中,我具有如下设置:

            <appSettings>
                <add key="webpages:Enabled" value="false" />
                <add key="StorageAccountName" value="storageaccount1"/> 
                <add key="StorageAccountKey" value="storageaccountkey1"/>
              </appSettings>

Here is how it looks like in the portal: 这是门户中的外观:

在此处输入图片说明

My Dot Net code is as below: 我的点网代码如下:

        public class ConnectionString
            {
                static string account = CloudConfigurationManager.GetSetting("StorageAccountName");
                static string key = CloudConfigurationManager.GetSetting("StorageAccountKey");
                public static CloudStorageAccount GetConnectionString()
                {
                    string connectionString = string.Format("DefaultEndpointsProtocol=https;AccountName={0};AccountKey={1}", account, key);
                    return CloudStorageAccount.Parse(connectionString);
                }
            }

However, my "account" and "key" variable are returning NULL. 但是,我的“ account”和“ key”变量返回NULL。

Could any one please suggest, what I'm doing wrong? 有人可以建议,我做错了吗? Is there anything that I'm missing? 有什么我想念的吗?

Note: I'm able to upload image via "Azure Storage Explorer". 注意:我可以通过“ Azure Storage Explorer”上传图像。

CloudConfigurationManager.GetSetting("StorageAccountName"); should work If you config it in the Web.config file. 如果您在Web.config文件中对其进行配置,则应该可以使用。 I test it with Asp.net MVC file. 我用Asp.net MVC文件对其进行测试。 It works correctly on my side. 它在我这边正常工作。

在此处输入图片说明

Test Result: 测试结果:

在此处输入图片说明

CloudConfigurationManager.GetSetting("MySetting")

Which will read values from all the configuration files ie, app.config, web.config and ServiceConfiguration.cscfg. 它将从所有配置文件(即app.config,web.config和ServiceConfiguration.cscfg)读取值。

Note: If we config the appsetting in the Web.Debug.config not web.config file,it will get null value. 注意:如果我们的配置在Web.Debug.config的appsetting没有web.config文件,它会得到空值。

To read web.config file, please use ConfigurationManager.AppSettings . 要读取web.config文件,请使用ConfigurationManager.AppSettings CloudConfigurationManager.GetSetting() is used to read .cscfg file actually. CloudConfigurationManager.GetSetting()用于实际读取.cscfg文件。

Issue is resolved. 问题已解决。 I was using web.config that is under view (which is wrong). 我正在使用正在查看的web.config(这是错误的)。 No .Net code changed. .NET代码未更改。 By using correct web.config solved the problem. 通过使用正确的web.config解决了该问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM