【问题标题】:How to combine Asp.Net Identity with Azure AD Authorization如何将 Asp.Net Identity 与 Azure AD 授权结合使用
【发布时间】:2019-04-09 15:40:55
【问题描述】:

如何将 Asp.Net Identity 与 Azure AD 授权集成

是否可以通过 OpenIdConnect 将 Asp.Net Identity 与 Azure AD 授权集成?我想要一个用于本地授权的两个授权提供程序(通过标准 Asp.net 核心身份,第二个通过 Azure AD

        _services
            .AddAuthentication(options =>
            {
                options.DefaultAuthenticateScheme = CookieAuthenticationDefaults.AuthenticationScheme;
                options.DefaultChallengeScheme = OpenIdConnectDefaults.AuthenticationScheme;
                options.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
            })
            .AddOpenIdConnect(OpenIdConnectDefaults.AuthenticationScheme, options =>
            {
                options.SignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
                options.ClientId = clientId;
                options.ClientSecret = clientSecret;
                options.Authority = $"{baseAuthorityUrl}/{tenantId}/v2.0";
                options.CallbackPath = new PathString(callBackPath);
                options.Scope.Add("email");
                options.Scope.Add("profile");
                options.ResponseType = "code id_token";

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

这适用于 Azure AD 授权,但我无法将授权方法从 Azure AD 更改为 ASp.Net Identity。非常感谢任何帮助

【问题讨论】:

    标签: c# asp.net-core asp.net-identity


    【解决方案1】:

    我建议使用默认的 ASP.NET 标识模板来启动项目:

    1. 使用 ASP.NET 身份(个人用户帐户模板)创建新应用程序。

    2. 使用迁移Add-Migration NameUpdate-Database 为数据库播种。

    3. 添加您的 OIDC 提供商:

      services
      .AddAuthentication(options =>
      {
          options.DefaultChallengeScheme = OpenIdConnectDefaults.AuthenticationScheme;
          options.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
      
      }).AddCookie()
      .AddOpenIdConnect(OpenIdConnectDefaults.AuthenticationScheme, options =>
      {
      
          options.SignInScheme = IdentityConstants.ExternalScheme;
      
          options.ClientId = ClientId;
          options.ClientSecret = ClientSecret;
          options.Authority = $"{baseAuthorityUrl}/{tenantId}/v2.0";
          options.CallbackPath = new PathString("/signin-oidc");
          options.Scope.Add("email");
          options.Scope.Add("profile");
          options.ResponseType = "code id_token";
      
          options.SaveTokens = true;
          options.GetClaimsFromUserInfoEndpoint = true;
      
          options.TokenValidationParameters = new TokenValidationParameters
          {
              NameClaimType = "name"
          };
      
      });
      

    确保将IdentityConstants.ExternalScheme 用于SignInScheme,否则身份将无法正确接受外部登录信息。

    Asp.net 将创建一个与您的外部帐户相关联的本地帐户,以便您可以使用本地身份系统执行授权。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-08-02
      • 1970-01-01
      • 2021-09-25
      • 2023-01-21
      • 2021-10-22
      • 2020-09-03
      • 1970-01-01
      • 2022-01-19
      相关资源
      最近更新 更多