Asp.net mvc AuthorizationCodeReceived事件未触发

Asp.net mvc AuthorizationCodeReceived事件未触发,asp.net-mvc,authentication,owin,openid-connect,Asp.net Mvc,Authentication,Owin,Openid Connect,我正在尝试在ASP.NET MVC 5(.NET Framework)应用程序中实现OpenId Connect中间件 在我的AccountController.cs中,我发送了一个OpenID连接sing-In请求。我实现了另一个OpenId connect中间件,这就是为什么我指定要挑战的中间件是“AlternateIdentityProvider” 对中间件发出质询后,Startup.cs中的RedirectToIdentityProvider事件将触发,我将被重定向到提供商进行登录。但是

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

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

对中间件发出质询后,
Startup.cs
中的
RedirectToIdentityProvider
事件将触发,我将被重定向到提供商进行登录。但是,在成功登录后,我被重定向到指定的重定向uri,并将状态和代码参数添加为查询参数,即(为简洁起见删除了参数),这将导致404,因为我的应用程序中不存在此类路由

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

我在ASP.NETCore2.1中实现了一个几乎相同的解决方案,在这里,我能够在触发不同的事件时逐步完成它们

我当前的
Startup.cs
的相关代码如下所示。请注意,如果初始请求包括reponse_模式和一些遥测参数,则OpenId提供程序将抛出错误,因此在初始
重定向到dentityprovider
事件期间,这些参数将被删除

你知道为什么中间件中没有接收到来自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);
                },
            }
        });

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

对我来说,我必须做到以下几点:

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

  • 转到Azure门户网站
  • 选择以Azure Active Directory
  • 选择应用程序注册
  • 选择你的应用程序
  • 单击清单
  • 找到值oauth2AllowIdTokenImplicitFlow,并将其值更改为true
  • 单击保存
  • 2) 在startup.cs文件中,更改以下内容:

    ResponseType=OpenIdConnectResponseType.Code

    ResponseType=OpenIdConnectResponseType.CodeIdToken

    有一次,我做了这两件事,SecurityTokenValidated和AuthorizationCodeReceived开始启动

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


    希望这能有所帮助。

    请注意,.Net Framework中的OpenId Connect实现只支持response\u mode=form\u post。(见附件)

    由于将请求中的参数剥离到OpenId连接提供程序(在RedirectToIdentityProvider通知中),因此提供程序将默认为response_mode=query pr.规格。(参见ind中响应类型和响应模式之间的关系。)


    因此,简言之,OpenId Connect中间件希望出现一个HTTP POST(带有表单主体),您的提供商将正确发送一个HTTP GET(以参数作为查询字符串)。

    是的,这将是一个可能的解决方法,但不幸的是,我集成的OpenId Connect提供商不是Azure。我没有选择对我正在使用的版本进行这些更改,但我给他们的支持团队留下了一张罚单。当我得到答案时,将在此处更新..我试图将其与MSAL一起使用,这根本不可能:。不得不求助于
    应用程序。改用WindowsAzureActiveDirectoryBeareAuthentication
            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);
                    },
                }
            });