gpt4 book ai didi

Asp.net Core 身份 - Azure 身份验证中间件

转载 作者:行者123 更新时间:2023-12-03 04:21:21 29 4
gpt4 key购买 nike

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

例如,查询 Nuget 时存在以下身份验证包:

  • 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不使用中间件包,但中间件包提供了易用性,并且更直接地与 Identity 框架集成。

是否有任何使用 Azure AD 的直接包,或者以任何方式指定 MicrosoftAccount 包应指向 Organizations/azure/tenant url?

最佳答案

这对我有用:

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 - 这是您在注册应用的“ key ”部分下创建和注册的密码。

然后,您可以使用访问 token 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);
}
};

关于Asp.net Core 身份 - Azure 身份验证中间件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48480995/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com