简体   繁体   English

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

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

Goal: Host an ASP.NET Web App to Azure and utilize OAuth2 for Google, Twilio, and SendGrid, with a database for user information. 目标:将Azure Web应用程序托管到Azure,并将OAuth2用于Google,Twilio和SendGrid,并提供用于用户信息的数据库。

Issue: I receive errors upon publishing when I utilize an external config file that my "appSettings" within my Web.config file references. 问题:当我利用我的Web.config文件中的“appSettings”引用的外部配置文件时,我在发布时收到错误。 Within Azure I've also entered credentials to be securely stored for Google OAuth2, which overrides the published Web.config settings from my research and understanding. 在Azure中,我还输入了要为Google OAuth2安全存储的凭据,这将覆盖我的研究和理解中发布的Web.config设置。 How do I properly and securely use and reference within my code sensitive credentials for Azure? 如何在我的代码敏感凭据中正确安全地使用和引用Azure?

Research: I've been following this link step by step - 研究:我一直在逐步关注这个链接 -

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

This link also leads to another link for Google OAuth2 implementation below - 此链接还会在下面显示另一个Google OAuth2实施链接 -

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

However this insecurely puts the sensitive information within the web.config file which is noted by a Security note that leads here for secure/best practices for deploying sensitive ASP.NET information to Azure - 但是,这会不安全地将敏感信息放在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

My understanding is that referencing an external file that houses the sensitive data/credentials from within the web.config file is a best practice. 我的理解是,从web.config文件中引用包含敏感数据/凭证的外部文件是最佳做法。 I note that IIS does not serve *.config and because of the referenced configuration file location below even "git add *" will not add the sensitive credentials to the repository. 我注意到IIS不提供* .config,并且由于下面引用的配置文件位置,即使“git add *”也不会将敏感凭据添加到存储库。

Web.config - (Note appSettings on line 2) 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 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>

How do I properly/securely reference my ID and Secret from the AppSettingsSecrets.config from within the code that is listed in step 7? 如何从步骤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

Relevant code is listed below (note the bottom used for Google Authentication): 下面列出了相关代码(请注意用于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");
}

Additionally in the tutorial for deploying secrets to Azure this information is listed: 此外,在向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.

This tells me that I can manually enter the sensitive information into Azure through the portal and (I'm assuming) this is a secure method of keeping the sensitive credentials private while allowing my web application to access and utilize the information. 这告诉我,我可以通过门户手动将敏感信息输入到Azure中(我假设)这是一种安全的方法,可以将敏感凭据保密,同时允许我的Web应用程序访问和利用这些信息。 (Please correct me if I'm wrong!) However, when I manually entered this information, my web application is now throwing a runtime error as shown below as an image link: (如果我错了,请纠正我!)但是,当我手动输入此信息时,我的Web应用程序现在抛出运行时错误,如下所示为图像链接:

Server Runtime Error 服务器运行时错误

Any suggestions, or other links or pointers/tips would be greatly appreciated! 任何建议,或其他链接或指针/提示将不胜感激! Thanks in advance! 提前致谢!

EDIT: After turning off the customErrors in the web.config file, and refreshing the Azure deployment this is the error the site now yields - Essentially my code is not pulling the stored Google OAuth2 credentials I've stored in Azure. 编辑:关闭web.config文件中的customErrors并刷新Azure部署后,这是网站现在产生的错误 - 基本上我的代码并没有提取我存储在Azure中的存储的Google OAuth2凭据。 How do I get my code to pull the credentials stored in Azure for Google OAuth2? 如何获取我的代码以获取存储在Azure for Google OAuth2中的凭据? NewSiteError NewSiteError

First I would turn the customErrors off so you can find the real problem, but my guess is that you aren't including the AppSettingsSecrets.config in your solution. 首先,我将关闭customErrors,以便找到真正的问题,但我的猜测是你没有在解决方案中包含AppSettingsSecrets.config。 This will cause a problem once deployed because the file isn't there - so you should remove the configSource or file attributes from the config using a web.config transform. 一旦部署,这将导致问题,因为文件不存在 - 因此您应该使用web.config转换从配置中删除configSource或文件属性。

So in the Web.Release.config you can add the following inside the: 所以在Web.Release.config中你可以在里面添加以下内容:

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

When you do a publish of the release build, this will remove those file paths from the config so it won't fail at startup once deployed. 当您发布发布版本时,这将从配置中删除这些文件路径,以便在部署时不会在启动时失败。

Update 更新

Now you'll need to add all the appSettings that were in the AppSettingsSecrets.config file to the appSettings in the portal. 现在,您需要将AppSettingsSecrets.config文件中的所有appSettings添加到门户网站中的appSettings。 This will keep your published site credentials only in Azure. 这将仅在Azure中保留您发布的站点凭据。

All The appSettings in the Web.config and any other files get merged into the same listing (meaning your code doesn't need to know the appSetting is coming from the web.config, AppSettingsSecrets.config or being configured from the azure portal. Here is a good article on appSettings: https://buildazure.com/2015/11/30/azure-web-app-application-settings/ 所有Web.config中的appSettings和任何其他文件都被合并到同一个列表中(这意味着您的代码不需要知道appSetting来自web.config,AppSettingsSecrets.config或者是从azure门户配置的。是一篇关于appSettings的好文章: https//buildazure.com/2015/11/30/azure-web-app-application-settings/

The good things about your setup is: 您的设置的好处是:

  1. the AppSettingsSecrets.config has the secrets needed only for developers and is not included in source control or published AppSettingsSecrets.config只有开发人员需要的秘密,不包含在源代码管理中或已发布
  2. the published site credentials are only in Azure and available to only those with access to the Azure account. 发布的站点凭据仅在Azure中,仅对有权访问Azure帐户的用户可用。

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

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