未达到OpenIdConnectEvents.OnTokenValidated

时间:2019-06-28 12:50:44

标签: asp.net-core azure-active-directory

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

            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 个答案:

答案 0 :(得分:0)

您可以将ResponseMode更改为FormPost并向OnTokenValidated添加异步,然后将其修复。

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