【问题标题】:Asp.net Core Identity - Azure Authentication MiddlewareAsp.net Core Identity - Azure 身份验证中间件
【发布时间】:2018-07-06 22:51:22
【问题描述】:

是否有用于 Asp.net Core 的 Azure AD 身份验证包?

例如,查询 Nuget 时存在以下 Authentication 包:

  • Microsoft.AspNetCore.Authentication.Facebook
  • Microsoft.AspNetCore.Authentication.Twitter
  • Microsoft.AspNetCore.Authentication.Google
  • Microsoft.AspNetCore.Authentication.MicrosoftAccount

我尝试使用 MicrosoftAccount 包,但连接成功后,我从 Microsoft 页面收到以下错误:

Application '{my-app-id}' {my-app-name} is not supported over the /common or /consumers endpoints. Please use the /organizations or tenant-specific endpoint.

examples 不使用中间件包,但是中间件包提供了易用性,并且更直接地与身份框架集成。

是否有任何使用 Azure AD 的直接包,或者无论如何指定 MicrosoftAccount 包应该指向 organizations/azure/tenant url?

【问题讨论】:

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


【解决方案1】:

这对我有用:

authBuilder
    .AddMicrosoftAccount(Auth.Constants.AuthenticationScheme, options =>
    {
        options.ClientId = clientId;
        options.ClientId = clientSecret;
        if (tenantId != null)
        {
            var resource = "https://graph.microsoft.com";
            options.AuthorizationEndpoint = $"https://login.microsoftonline.com/{tenantId}/oauth2/authorize?resource={resource}";
            options.TokenEndpoint = $"https://login.microsoftonline.com/{tenantId}/oauth2/token?resource={resource}";
        }
    });
  • tenantId - 查看“应用程序注册”刀片中的“端点”部分。您可以从 URL 中提取它,或者直接使用这些 URL,而不是我上面所做的。
  • clientId - 这是您注册应用中的“应用程序 ID”。
  • clientSecret - 这是您在已注册应用的“密钥”部分创建和注册的密码。

然后您可以通过使用带有https://graph.microsoft.com 的访问令牌来获取更多信息,例如添加如下选项:

options.ClaimActions.DeleteClaim(ClaimTypes.Name);
options.ClaimActions.MapJsonKey(ClaimTypes.Name, "displayName");
options.ClaimActions.MapJsonKey(ClaimTypes.NameIdentifier, "userPrincipalName");
options.Events = new OAuthEvents
{
    OnCreatingTicket = async context =>
    {
        // Get the GitHub user
        var request = new HttpRequestMessage(HttpMethod.Get, "https://graph.microsoft.com/v1.0/me/");
        request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", context.AccessToken);
        request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

        var response = await context.Backchannel.SendAsync(request, context.HttpContext.RequestAborted);
        response.EnsureSuccessStatusCode();

        var contents = await response.Content.ReadAsStringAsync();
        var user = JObject.Parse(contents);

        context.RunClaimActions(user);
    }
};

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-05-28
    • 2018-01-30
    • 2017-10-29
    • 1970-01-01
    • 2019-02-25
    • 2018-12-30
    • 2017-06-26
    • 2019-12-13
    相关资源
    最近更新 更多