本文介绍了未达到OpenIdConnectEvents.OnTokenValidated的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用asp.net core 2.2,我在下面的启动中有以下内容我到达OnRedirectToIdentityProvider断点,然后到达appsettings"CallbackPath":中的相对路径.但是我没有到达OnTokenValidated断点.Auth由控制器的[Authorize]装饰触发.我想念什么?

using asp.net core 2.2, I have the following in my startup belowi reach OnRedirectToIdentityProvider breakpoint , and then I reach relative path in appsettings "CallbackPath": " . But I don't i reach OnTokenValidated breakpoint . the Auth is triggered by [Authorize] decoration of a controller. What am i missing ?

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

        services.Configure<OpenIdConnectOptions>(AzureADDefaults.OpenIdScheme, options =>
        {
            options.Authority = options.Authority + "/v2.0/";         // Microsoft identity platform
            options.Events = new OpenIdConnectEvents
            {
                OnRedirectToIdentityProvider = async n =>
                {
                    //save url to state
                    n.ProtocolMessage.State = n.HttpContext.Request.Path.Value.ToString();
                },

                OnTokenValidated = ctx =>
                {
                    var url = ctx.ProtocolMessage.GetParameter("state");
                    var claims = new List<Claim>
                    {
                        new Claim("myurl", url)
                    };
                    var appIdentity = new ClaimsIdentity(claims);

                    //add url to claims
                    ctx.Principal.AddIdentity(appIdentity);

                    return Task.CompletedTask;
                },

                OnTicketReceived = ctx =>
                {
                    var url = ctx.Principal.FindFirst("myurl").Value;
                    ctx.ReturnUri = url;
                    return Task.CompletedTask;
                }
            };
            options.TokenValidationParameters.ValidateIssuer = false; // accept several tenants (here simplified)
        });

推荐答案

您可以将 ResponseMode 更改为 FormPost ,并将异步添加到 OnTokenValidated 那么它将被修复.

You can change of ResponseMode to FormPost and add async to OnTokenValidated then it will be fixed.

services.Configure<OpenIdConnectOptions>(AzureADDefaults.OpenIdScheme, options =>
{
    options.Authority = options.Authority + "/v2.0/";         // Microsoft identity platform
    options.ResponseMode = OpenIdConnectResponseMode.FormPost;
    options.CallbackPath = "/";
    options.Events = new OpenIdConnectEvents
    {
        OnRedirectToIdentityProvider = async n =>
        {
                   ...
        },
        OnTokenValidated = async ctx =>
        {
                   ...
        },

这篇关于未达到OpenIdConnectEvents.OnTokenValidated的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-22 18:28