在Azure Cloud中为ASP.net站点Web.config使用App Settings

时间:2016-03-09 01:48:50

标签: c# asp.net asp.net-mvc azure oauth-2.0

我正在遵循有关如何开发ASP.NET MVC应用程序的一些说明,我正在添加使用Facebook或Google登录的功能。

我听说在源代码中存储敏感信息是不好的做法。我修改了我的Web.config文件:

<appSettings>    
    <add key="GoogClientID" value="X" />
    <add key="GoogClientSecret" value="X" />
    <add key="FbAppID" value="X" />
    <add key="FbAppSecret" value="X" />
    ...etc...

这使我无法在源代码中存储敏感信息,我打算通过在Azure门户中配置应用程序来覆盖它们。

我从Startup.Auth.cs访问这些设置,根据我的理解,这种类型的应用程序是典型的。

        //load from Web.config, to get app secrets.
        System.Configuration.Configuration WebConfigForSecrets = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("~/");
        if (WebConfigForSecrets.AppSettings.Settings.Count > 0)
        {

            //google
            System.Configuration.KeyValueConfigurationElement GoogClientID = WebConfigForSecrets.AppSettings.Settings["GoogClientID"];
            if (GoogClientID != null)
            {
                System.Configuration.KeyValueConfigurationElement GoogClientSecret = WebConfigForSecrets.AppSettings.Settings["GoogClientSecret"];
                if (GoogClientSecret != null)
                {
                    //never reached when hosted on azure               
                    app.UseGoogleAuthentication(
                        clientId: GoogClientID.Value.ToString(), //added .Value 3/9/16
                        clientSecret: GoogClientSecret.Value.ToString()); //added .Value 3/9/16
                }
            }

            //facebook
            System.Configuration.KeyValueConfigurationElement FbAppID = WebConfigForSecrets.AppSettings.Settings["FbAppID"];
            if (FbAppID != null)
            {
                System.Configuration.KeyValueConfigurationElement FbAppSecret = WebConfigForSecrets.AppSettings.Settings["FbAppSecret"];
                if (FbAppSecret != null)
                {
                    //never reached when hosted on azure     
                    app.UseFacebookAuthentication(
                       appId: FbAppID.Value.ToString(), //added .Value 3/9/16
                       appSecret: FbAppSecret.Value.ToString()); //added .Value 3/9/16
                }
            }

        }

我尝试将相同的键/值对添加到&#34;应用程序设置&#34; &GT; &#34;应用设置&#34;用于我的Web应用程序的azure门户中的菜单。它仍然表现得好像没有找到它们。

我也试过去Project - &gt;从visual studio发布Web.config。它说这是成功的,但它并没有解决这个问题。

我也尝试过为AppSettingsSecrets.config文件使用绝对路径,但我没有尝试将其添加到项目中,因为这似乎可能会破坏文章中建议的目的。

有人知道如何做到这一点/最佳做法是什么?

0 个答案:

没有答案