asp.net-core - 未达到 OpenIdConnectEvents.OnTokenValidated

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

使用 asp.net core 2.2,我在下面的启动中有以下内容 我到达 OnRedirectToIdentityProvider 断点,然后我到达 appsettings "CallbackPath": "中的相对路径。但我没有到达 OnTokenValidated 断点。Auth 由 Controller 的 [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)
        });

最佳答案

您可以将 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 =>
        {
                   ...
        },

关于asp.net-core - 未达到 OpenIdConnectEvents.OnTokenValidated,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56807290/

相关文章:

oauth-2.0 - Microsoft Power 自动处理错误的 OAuth token

azure - 是否可以打开不在默认目录中的 azure 门户资源链接

asp.net-core - 为什么DistributedCache SessionHandler抛出连接问题?

c# - EF - 嵌套包含抛出错误

azure - VS2019 Azure服务身份验证帐户选择用于本地调试

Azure Devops - 生产部署的服务主体手动(或)服务主体自动?

c# - 错误 CTC1014 Docker 命令失败,退出代码为 1,带有 dotnet 核心 api

docker - 使用实时观察在 VS Code 中调试 dockerize ASP.Net Core 应用程序

c# - 尝试使用启动文件中的 GetSection 访问它们时,配置值为空。(IOptions-config 检索)

azure - 我可以导入从 Azure 外部托管的 APIM 吗?