小编Mic*_*ell的帖子

在ASP.NET Core 2.0中使用Azure Active Directory OAuth和身份模型

问题陈述

我们正在开发新的企业级应用程序,并希望利用Azure Active Directory登录应用程序,以便我们不必创建另一组用户凭据.但是,我们对此应用程序的权限模型比通过AAD内部的组处理的更复杂.

那个想法

我们的想法是,除了ASP.NET核心标识框架之外,我们还可以使用Azure Active Directory OAuth 2.0来强制用户通过Azure Active Directory进行身份验证,然后使用身份框架来处理授权/权限.

问题

您可以使用Azure OpenId身份验证创建项目开箱即用,然后您可以使用Identity框架轻松地将Microsoft帐户身份验证(非AAD)添加到任何项目.但是没有内置的东西可以将AAD的OAuth添加到身份模型中.

在尝试破解这些方法以使它们像我需要的那样工作之后,我终于尝试了自己创建自己的解决方案并建立了类OAuthHandlerOAuthOptions类.

我在这条路线上遇到了很多问题,但设法解决了大部分问题.现在我到了一个点,我从端点获得一个令牌,但我的ClaimsIdentity似乎没有效果.然后,当重定向到ExternalLoginCallback时,我的SigninManager无法获取外部登录信息.

几乎肯定必须有一些我想念的简单但我似乎无法确定它是什么.

代码

Startup.cs

services.AddAuthentication()
.AddAzureAd(options =>
{
    options.ClientId = Configuration["AzureAd:ClientId"];
    options.AuthorizationEndpoint = $"{Configuration["AzureAd:Instance"]}{Configuration["AzureAd:TenantId"]}/oauth2/authorize";
    options.TokenEndpoint = $"{Configuration["AzureAd:Instance"]}{Configuration["AzureAd:TenantId"]}/oauth2/token";
    options.UserInformationEndpoint = $"{Configuration["AzureAd:Instance"]}{Configuration["AzureAd:TenantId"]}/openid/userinfo";
    options.Resource = Configuration["AzureAd:ClientId"];
    options.ClientSecret = Configuration["AzureAd:ClientSecret"];
    options.CallbackPath = Configuration["AzureAd:CallbackPath"];
});
Run Code Online (Sandbox Code Playgroud)

AzureADExtensions

namespace Microsoft.AspNetCore.Authentication.AzureAD
{
    public static class AzureAdExtensions
    {
        public static AuthenticationBuilder AddAzureAd(this AuthenticationBuilder builder)
            => builder.AddAzureAd(_ => { });

        public static AuthenticationBuilder AddAzureAd(this AuthenticationBuilder builder, Action<AzureAdOptions> configureOptions)
        {
            return …
Run Code Online (Sandbox Code Playgroud)

c# oauth azure azure-active-directory asp.net-core

10
推荐指数
1
解决办法
2053
查看次数

标签 统计

asp.net-core ×1

azure ×1

azure-active-directory ×1

c# ×1

oauth ×1