【问题标题】:ASP.NET Core 3.1: Why can't I get Access Token?ASP.NET Core 3.1:为什么我无法获取访问令牌?
【发布时间】:2021-05-26 12:04:42
【问题描述】:

我可以获取 id_token,但是当我尝试获取访问令牌时我得到了 null。我不知道为什么?

var token = await HttpContext.GetTokenAsync(OpenIdConnectParameterNames.IdToken); // token has value
var accessToken = await HttpContext.GetTokenAsync(OpenIdConnectParameterNames.AccessToken);// accessToken is null

Startup.cs:

   public void ConfigureServices(IServiceCollection services)
    {
        var jwtTokenConfig = Configuration.GetSection("jwtTokenConfig").Get<JwtTokenConfig>();
        services.AddSingleton(jwtTokenConfig);
        services.AddSingleton<IJwtAuthManager, JwtAuthManager>();
        services.AddHostedService<JwtRefreshTokenCache>();
        services.Configure<CookiePolicyOptions>(options =>
        {
            options.MinimumSameSitePolicy = SameSiteMode.Unspecified;
            options.OnAppendCookie = cookieContext =>
                CheckSameSite(cookieContext.Context, cookieContext.CookieOptions);
            options.OnDeleteCookie = cookieContext =>
                CheckSameSite(cookieContext.Context, cookieContext.CookieOptions);
        });

        services.AddCors(options =>
        {

            options.AddPolicy("CorsPolicy",
                builder => builder.AllowAnyOrigin()
                .AllowAnyMethod()
                .AllowAnyHeader());
        });
        
        services.Configure<OpenIdConnectOptions>(OpenIdConnectDefaults.AuthenticationScheme, options =>
        {
            

            options.Events.OnRedirectToIdentityProviderForSignOut = async context =>
            {
                Console.WriteLine("intercepted");
            };
        });

    


        var azureAd = new AzureAd();
        Configuration.GetSection("AzureAd").Bind(azureAd);
        services.AddControllersWithViews();

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

     
        var url = "https://localhost:5001/platform/signin-oidc";

        services.Configure<OpenIdConnectOptions>(AzureADDefaults.OpenIdScheme, options =>
        {
            options.SaveTokens = true;

            options.Events = new OpenIdConnectEvents
            {

                OnRedirectToIdentityProvider = async context =>
                {
                    context.ProtocolMessage.RedirectUri = url;

                    //context.Response.Headers.Add("Referrer-Policy", "no-referrer");
                    await Task.FromResult(0);
                }
            };
        });

    }

【问题讨论】:

    标签: c# .net azure asp.net-core-3.1


    【解决方案1】:

    请尝试代码,请参阅here

    var accessToken = await HttpContext.GetTokenAsync(IdentityConstants.ExternalScheme, OpenIdConnectParameterNames.AccessToken)
    

    我们通常按照此示例通过 Azure AD 进行授权:https://github.com/AzureAdQuickstarts/AppModelv2-WebApp-OpenIDConnect-DotNet

    【讨论】:

    • InvalidOperationException:没有为方案“Identity.External”注册身份验证处理程序。注册的方案有:AzureAD、AzureADOpenID、AzureADCookie。您是否忘记调用 AddAuthentication().Add[SomeAuthHandler]("Identity.External",...)?
    • 嗨,@SamuraiJack。请分享有关如何设置 AAD 配置的代码。您要请求哪个 API?
    猜你喜欢
    • 2013-02-09
    • 1970-01-01
    • 2022-01-01
    • 2018-06-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多