AuthorizationCodeReceived 事件未触发

Fro*_*rar 5 authentication asp.net-mvc owin openid-connect

我正在尝试在 ASP.NET MVC 5 (.Net Framework) 应用程序中实现 OpenId Connect 中间件。

在我的中,AccountController.cs我发送了一个 OpenID Connect 签入请求。我实现了另一个 OpenId 连接中间件,这就是为什么我指定要挑战的中间件是“AlternateIdentityProvider”。

    public void SignIn()
    {
        HttpContext.GetOwinContext().Authentication.Challenge(
            new AuthenticationProperties { RedirectUri = "/" },
            "AlternateIdentityProvider");
    }
Run Code Online (Sandbox Code Playgroud)

对中间件发出质询后,RedirectToIdentityProvider事件Startup.cs触发,我被重定向到提供程序进行登录。但是,成功登录后,我被重定向到指定的重定向 uri,并添加了作为查询参数的状态和代码参数,即http: //localhost:63242/singin-oidc/?state=State&code=AuthorizationCode(为简洁起见删除了参数),这会导致 404,因为我的应用程序中不存在这样的路由。

相反,我希望成功登录会触发AuthorizationCodeReceived我可以实现附加逻辑的事件。事实上,其他事件都不会触发。

我已经在 ASP.Net Core 2.1 中实现了一个几乎相同的解决方案,在这里我能够在触发时逐步处理不同的事件。

我当前的相关代码Startup.cs如下所示。请注意,如果初始请求包含 reponse_mode 和一些遥测参数,则 OpenId 提供程序会抛出错误,因此在初始RedirectToIdentityProvider事件期间将删除这些参数。

任何想法为什么没有在中间件中接收来自 OpenId 提供程序的回调?

        app.SetDefaultSignInAsAuthenticationType(CookieAuthenticationDefaults.AuthenticationType);

        app.UseCookieAuthentication(new CookieAuthenticationOptions());

        app.UseOpenIdConnectAuthentication(
            new OpenIdConnectAuthenticationOptions("AlternateIdentityProvider")
        {
            ClientId = { { Client Id } },
            ClientSecret = { { Client Secret } },
            Scope = OpenIdConnectScope.OpenId,
            ResponseType = OpenIdConnectResponseType.Code,
            RedirectUri = "http://localhost:63242/singin-oidc",
            MetadataAddress = { { Discovery document url } },

            Notifications = new OpenIdConnectAuthenticationNotifications
            {
                RedirectToIdentityProvider = context =>
                {
                    Debug.WriteLine("Redirecting to identity provider for sign in..");

                    context.ProtocolMessage.EnableTelemetryParameters = false;
                    context.ProtocolMessage.ResponseMode = null;

                    return Task.FromResult(0);
                },

                AuthorizationCodeReceived = context => {

                    Debug.WriteLine("Authorization code received..");
                    return Task.FromResult(0);
                },

                SecurityTokenReceived = context =>
                {
                    Debug.WriteLine("Token response received..");
                    return Task.FromResult(0);
                },

                SecurityTokenValidated = context =>
                {
                    Debug.WriteLine("Token validated..");
                    return Task.FromResult(0);
                },
            }
        });
Run Code Online (Sandbox Code Playgroud)

cbe*_*ker 5

我遇到了同样的问题。我正在尝试将 Owin 插入我们的旧版 WebForms 应用程序中。

对我来说,我必须执行以下操作:

1) 更改 Azure 上应用程序定义的应用程序清单,将“oauth2AllowIdTokenImplicitFlow”属性从false设置为true

  1. 转到Azure 门户
  2. 选择到Azure Active Directory
  3. 选择应用注册
  4. 选择您的应用。
  5. 点击清单
  6. 找到值oauth2AllowIdTokenImplicitFlow并将其值更改为true
  7. 点击保存

2) 在您的 startup.cs 文件中,更改以下内容:

ResponseType = OpenIdConnectResponseType.Code

ResponseType = OpenIdConnectResponseType.CodeIdToken

有一次,我做了这两件事,SecurityTokenValidated 和 AuthorizationCodeReceived 开始触发。

不过,我不确定这是不是正确的方法。需要多读书。

希望这可以帮助。

  • 是的,这将是一个可能的解决方法,但不幸的是,我要集成的 OpenId Connect 提供商不是 Azure。我无法选择对我正在使用的产品进行这些更改,但我已向他们的支持团队留下了一张票。当我得到答案时会在这里更新.. (2认同)