简体   繁体   English

ASP.NET Core 3.1:为什么我无法获得访问令牌?

[英]ASP.NET Core 3.1: Why can't I get Access Token?

I am able to get id_token but I get null when I try to get access token.我能够获得 id_token 但是当我尝试获得访问令牌时我得到 null 。 And I can't figure out why?我不知道为什么?

在此处输入图像描述

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

Startup.cs:启动.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);
                }
            };
        });

    }

Please try the code, see here .请尝试代码,请参见此处

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

And we usually authorize by Azure AD following this sample: https://github.com/AzureAdQuickstarts/AppModelv2-WebApp-OpenIDConnect-DotNet我们通常通过 Azure AD 按照此示例进行授权: https://github.com/AzureAdQuickstarts/AppModelv2-WebApp-OpenIDConnect-DotNet

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ASP.NET Core 3.1 不授权不记名令牌 - ASP.NET Core 3.1 doesn't authorize bearer token 如何让用 ASP.Net Core 2.0 编写的自定义异常处理程序在 ASP.Net Core 3.1 中工作? - How can I get a custom exception handler written in ASP.Net Core 2.0 to work in ASP.Net Core 3.1? 无法使用 asp.net 内核 3.1 打开 SqlLiteConnection - Can't open a SqLiteConnection with asp.net core 3.1 ASP.NET 核心 3.1 - 访问被拒绝 - ASP.NET Core 3.1 - Access denied 为什么我无法在 ASP.NET Core 应用程序中添加新视图? - Why I can't get the new view added in the ASP.NET Core application? 在 cookie 中存储 JWT 令牌后如何打破该 cookie 并在 ASP.NET Core 3.1 中获取信息 - After store JWT token in cookie how to break that cookie and get information in ASP.NET Core 3.1 不记名令牌出现 401 错误 - asp.net 核心 3.1 - 401 error with bearer token - asp.net core 3.1 无法使用 ASP.NET Core 从 JWT 令牌中获取声明 - Can't get claims from JWT token with ASP.NET Core ASP .NET Core 3.1 问题使用 Microsoft OAuth 2.0 获取 access_token - ASP .NET Core 3.1 problem get access_token with Microsoft OAuth 2.0 ASP.NET Core 3.1 - EF Core 无法翻译 GroupBy 和 Join - ASP.NET Core 3.1 - EF Core can't translate GroupBy and Join
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM