【问题标题】:Log Azure AD e-mail into ASP.NET Identity tables将 Azure AD 电子邮件记录到 ASP.NET 身份表中
【发布时间】:2019-09-24 16:56:41
【问题描述】:

我正在制作一个报告系统,允许用户使用 Azure 凭据登录,然后他们将订阅/取消订阅报告。

它目前使用的 ASP.NET Identity 运行良好,但需要更多 IT 支持,因为人们忘记了他们的凭据等。

如何将 Azure 凭据保存到 ASP.NET Identity 中?就像它与 Facebook 等其他外部提供商一样,或者您是否会避免一起使用 ASP.NET Identity 并将电子邮件地址保存到表中并引用该表而不是 ASP.NET Identity 版本?

Startup.cs

            services.AddAuthentication(AzureADDefaults.AuthenticationScheme)
            .AddAzureAD(options => Configuration.Bind("AzureAd", options));

        services.Configure<OpenIdConnectOptions>(AzureADDefaults.OpenIdScheme, options =>
        {
            options.Authority = options.Authority + "/v2.0/";

            // Per the code below, this application signs in users in any Work and School
            // accounts and any Microsoft Personal Accounts.
            // If you want to direct Azure AD to restrict the users that can sign-in, change 
            // the tenant value of the appsettings.json file in the following way:
            // - only Work and School accounts => 'organizations'
            // - only Microsoft Personal accounts => 'consumers'
            // - Work and School and Personal accounts => 'common'

            // If you want to restrict the users that can sign-in to only one tenant
            // set the tenant value in the appsettings.json file to the tenant ID of this
            // organization, and set ValidateIssuer below to true.

            // If you want to restrict the users that can sign-in to several organizations
            // Set the tenant value in the appsettings.json file to 'organizations', set
            // ValidateIssuer, above to 'true', and add the issuers you want to accept to the
            // options.TokenValidationParameters.ValidIssuers collection
            options.TokenValidationParameters.ValidateIssuer = false;

            // Custom
            options.Scope.Add("email");
            //options.Scope.Add("profile");
            options.SaveTokens = true;
            options.GetClaimsFromUserInfoEndpoint = true;

            options.TokenValidationParameters = new TokenValidationParameters
            {
                NameClaimType = "name"
            };
        });

感谢阅读。

编辑: 现在,我只是使用 TokenValidated 事件,然后使用带有 CreateAsync 的 UserManager 创建用户。

【问题讨论】:

    标签: asp.net-core azure-active-directory asp.net-identity


    【解决方案1】:

    如果将 asp.net 核心身份与 Azure AD 作为外部身份提供者一起使用。使用 AAD 登录后,asp.net 身份将帮助创建本地用户,用户可以输入他的 AAD 电子邮件,以便在数据库中您可以知道哪个本地用户与特定的 Azure AD 用户相关联。然后您可以在本地数据库中实现自定义逻辑,例如授权。但是本地用户不会影响 AAD 用户,更改本地帐户的凭据不会影响 AAD 用户在云中的凭据。下次,您仍然需要使用 AAD 作为身份提供者而不是本地用户帐户登录。

    您不需要也不应该将 Azure 凭据保存到本地数据库中。如果要重置特定用户的凭据,可以使用 Microsoft Graphupdate a user 。您的管理员 (IT) 用户应具有正确的权限(User.ReadWrite.AllDirectory.ReadWrite.All 委派权限)来更改租户中其他用户的信息。

    Here 是 Microsoft Graph Auth 文档,here 是 ASP.NET Core 的代码示例。

    【讨论】:

    • 这就是我想要发生的事情,您登录到 AAD 并且 ASP.NET Identity 将为您创建一个本地帐户。我并不真正关心用户是否能够使用该本地帐户登录,它只是用于获取用户订阅报告时的电子邮件地址。我的两个想法是;将电子邮件地址记录到表中,并在用户使用 AAD 登录后将其用作新参考,或者将 AAD 与 ASP.NET Identity 集成,因此如果我添加了任何其他外部提供程序(没有任何计划这样做) ) 我可以在不更改任何数据库的情况下保持所有标准。谢谢。
    • 是的,ASP.NET 身份有一个表(AspNetUserLogins 表)显示哪个本地用户与哪个身份提供者的哪个云用户相关联。您可以检查,asp.net 身份将允许用户在从外部提供商登录后输入他的电子邮件地址,并且如果确认不存在与该帐户关联的本地用户。
    • 不要忘记在 AAD 配置中使用 "CookieSchemeName": "Identity.External",如图所示 here
    【解决方案2】:

    我不认为将 Azure 凭据保存到 ASP.NET Identity 中有助于实现您的目标。

    如果您想使用 Azure 凭据登录,您需要将 Azure AD 集成到您的 ASP.NET Core Web 应用程序中,以使用户能够使用工作和学校帐户登录。

    在此处快速入门的详细步骤/示例:Add sign-in with Microsoft to an ASP.NET Core web appAzure Active Directory with ASP.NET Core

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-12-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-30
      • 2019-08-15
      相关资源
      最近更新 更多