簡體   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