OpenIdConnectEvents.OnTokenValidated not being reached

Viewed 1199

using asp.net core 2.2, I have the following in my startup below i 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)
        });
1 Answers

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 =>
        {
                   ...
        },
Related