繁体   English   中英

如何在ASP.NET和Azure的web.config之外安全地使用凭据

[英]How to securely use credentials outside web.config for ASP.NET & Azure

目标:将Azure Web应用程序托管到Azure,并将OAuth2用于Google,Twilio和SendGrid,并提供用于用户信息的数据库。

问题:当我利用我的Web.config文件中的“appSettings”引用的外部配置文件时,我在发布时收到错误。 在Azure中,我还输入了要为Google OAuth2安全存储的凭据,这将覆盖我的研究和理解中发布的Web.config设置。 如何在我的代码敏感凭据中正确安全地使用和引用Azure?

研究:我一直在逐步关注这个链接 -

https://azure.microsoft.com/en-us/documentation/articles/web-sites-dotnet-deploy-aspnet-mvc-app-membership-oauth-sql-database/

此链接还会在下面显示另一个Google OAuth2实施链接 -

www.asp.net/mvc/overview/security/create-an-aspnet-mvc-5-app-with-facebook-and-google-oauth2-and-openid-sign-on#goog

但是,这会不安全地将敏感信息放在web.config文件中,该文件由安全说明指出,该文件引出了将敏感ASP.NET信息部署到Azure的安全/最佳实践 -

www.asp.net/identity/overview/features-api/best-practices-for-deploying-passwords-and-other-sensitive-data-to-aspnet-and-azure

我的理解是,从web.config文件中引用包含敏感数据/凭证的外部文件是最佳做法。 我注意到IIS不提供* .config,并且由于下面引用的配置文件位置,即使“git add *”也不会将敏感凭据添加到存储库。

Web.config - (注意第2行的appSettings)

    </connectionStrings>
   <appSettings file="..\..\AppSettingsSecrets.config">      
      <add key="webpages:Version" value="3.0.0.0" />
      <add key="webpages:Enabled" value="false" />
      <add key="ClientValidationEnabled" value="true" />
      <add key="UnobtrusiveJavaScriptEnabled" value="true" />      
   </appSettings>
  <system.web>

AppSettingsSecrets.config

<appSettings>   
   <!-- SendGrid-->
   <add key="mailAccount" value="My mail account." />
   <add key="mailPassword" value="My mail password." />
   <!-- Twilio-->
   <add key="TwilioSid" value="My Twilio SID." />
   <add key="TwilioToken" value="My Twilio Token." />
   <add key="TwilioFromPhone" value="+12065551234" />

   <add key="GoogClientID" value="1.apps.googleusercontent.com" />
   <add key="GoogClientSecret" value="My Google client secret." />
</appSettings>

如何从步骤7中列出的代码中正确/安全地从AppSettingsSecrets.config中引用我的ID和机密?

www.asp.net/mvc/overview/security/create-an-aspnet-mvc-5-app-with-facebook-and-google-oauth2-and-openid-sign-on#goog

下面列出了相关代码(请注意用于Google身份验证的底部):

public void ConfigureAuth(IAppBuilder app)
{
    // Configure the db context and user manager to use a single instance per request
    app.CreatePerOwinContext(ApplicationDbContext.Create);
    app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);

    // Enable the application to use a cookie to store information for the signed in user
    // and to use a cookie to temporarily store information about a user logging in with a third party login provider
    // Configure the sign in cookie
    app.UseCookieAuthentication(new CookieAuthenticationOptions
    {
        AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
        LoginPath = new PathString("/Account/Login"),
        Provider = new CookieAuthenticationProvider
        {
            OnValidateIdentity = SecurityStampValidator.OnValidateIdentity<ApplicationUserManager, ApplicationUser>(
                validateInterval: TimeSpan.FromMinutes(30),
                regenerateIdentity: (manager, user) => user.GenerateUserIdentityAsync(manager))
        }
    });

    app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);

    // Uncomment the following lines to enable logging in with third party login providers
    //app.UseMicrosoftAccountAuthentication(
    //    clientId: "",
    //    clientSecret: "");

    //app.UseTwitterAuthentication(
    //   consumerKey: "",
    //   consumerSecret: "");

    //app.UseFacebookAuthentication(
    //   appId: "",
    //   appSecret: "");

    app.UseGoogleAuthentication(
         clientId: "000-000.apps.googleusercontent.com",
         clientSecret: "00000000000");
}

此外,在向Azure部署机密的教程中,列出了以下信息:

When you deploy your web app to Azure, the AppSettingsSecrets.config  file won't be deployed (that's what you want). You could go to the Azure Management Portal and set them manually, to do that:
1.  Go to http://portal.azure.com, and sign in with your Azure credentials.
2.  Click Browse > Web Apps, then click the name of your web app.
3.  Click All settings > Application settings.
The app settings and connection string values override the same settings in the web.config file. In our example, we did not deploy these settings to Azure, but if these keys were in the web.config file, the settings shown on the portal would take precedence.

这告诉我,我可以通过门户手动将敏感信息输入到Azure中(我假设)这是一种安全的方法,可以将敏感凭据保密,同时允许我的Web应用程序访问和利用这些信息。 (如果我错了,请纠正我!)但是,当我手动输入此信息时,我的Web应用程序现在抛出运行时错误,如下所示为图像链接:

服务器运行时错误

任何建议,或其他链接或指针/提示将不胜感激! 提前致谢!

编辑:关闭web.config文件中的customErrors并刷新Azure部署后,这是网站现在产生的错误 - 基本上我的代码并没有提取我存储在Azure中的存储的Google OAuth2凭据。 如何获取我的代码以获取存储在Azure for Google OAuth2中的凭据? NewSiteError

首先,我将关闭customErrors,以便找到真正的问题,但我的猜测是你没有在解决方案中包含AppSettingsSecrets.config。 一旦部署,这将导致问题,因为文件不存在 - 因此您应该使用web.config转换从配置中删除configSource或文件属性。

所以在Web.Release.config中你可以在里面添加以下内容:

<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
...
    <connectionStrings xdt:Transform="RemoveAttributes(configSource)"/>
    <appSettings xdt:Transform="RemoveAttributes(file)"/>

当您发布发布版本时,这将从配置中删除这些文件路径,以便在部署时不会在启动时失败。

更新

现在,您需要将AppSettingsSecrets.config文件中的所有appSettings添加到门户网站中的appSettings。 这将仅在Azure中保留您发布的站点凭据。

所有Web.config中的appSettings和任何其他文件都被合并到同一个列表中(这意味着您的代码不需要知道appSetting来自web.config,AppSettingsSecrets.config或者是从azure门户配置的。是一篇关于appSettings的好文章: https//buildazure.com/2015/11/30/azure-web-app-application-settings/

您的设置的好处是:

  1. AppSettingsSecrets.config只有开发人员需要的秘密,不包含在源代码管理中或已发布
  2. 发布的站点凭据仅在Azure中,仅对有权访问Azure帐户的用户可用。

暂无
暂无

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

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