未找到方法 Microsoft.Owin.Security.Notifications.MessageReceivedNotification

Method not found Microsoft.Owin.Security.Notifications.MessageReceivedNotification

最近我用 owin 将一个 mvc 项目中的包更新到 4.0.0 到目前为止,我已经能够通过这次升级解决很多问题(其他需要升级的软件包等)

但目前我被困在这个例外中:

Method not found: 'System.Func`2<Microsoft.Owin.Security.Notifications.MessageReceivedNotification`2<Microsoft.IdentityModel.Protocols.OpenIdConnectMessage,Microsoft.Owin.Security.OpenIdConnect.OpenIdConnectAuthenticationOptions>,System.Threading.Tasks.Task> Microsoft.Owin.Security.OpenIdConnect.OpenIdConnectAuthenticationNotifications.get_MessageReceived()'.

我试过谷歌搜索,我试过在造成的问题上设置断点 class(构造函数被命中,但抛出异常的方法甚至从未被命中)

有人知道下一步要做什么吗?或者更好地解决这个问题?

 protected override async Task<AuthenticationTicket> AuthenticateCoreAsync()
    {
        if (Options.CallbackPath.HasValue && Options.CallbackPath != (Request.PathBase + Request.Path))
        {
            return null;
        }

        OpenIdConnectMessage openIdConnectMessage = null;

        if (string.Equals(Request.Method, "POST", StringComparison.OrdinalIgnoreCase)
            && !string.IsNullOrWhiteSpace(Request.ContentType)
            && Request.ContentType.StartsWith("application/x-www-form-urlencoded", StringComparison.OrdinalIgnoreCase)
            && Request.Body.CanRead)
        {
            if (!Request.Body.CanSeek)
            {
                //this._logger.WriteVerbose("Buffering request body");
                // Buffer in case this body was not meant for us.
                var memoryStream = new MemoryStream();
                await Request.Body.CopyToAsync(memoryStream);
                memoryStream.Seek(0, SeekOrigin.Begin);
                Request.Body = memoryStream;
            }

            var form = await Request.ReadFormAsync();
            Request.Body.Seek(0, SeekOrigin.Begin);

            openIdConnectMessage = new OpenIdConnectMessage(form);
        }

        if (openIdConnectMessage == null)
        {
            return null;
        }

        ExceptionDispatchInfo authFailedEx = null;
        try
        {
            var messageReceivedNotification = new MessageReceivedNotification<OpenIdConnectMessage, OpenIdConnectAuthenticationOptions>(this.Context, this.Options)
            {
                ProtocolMessage = openIdConnectMessage
            };

            await this.Options.Notifications.MessageReceived(messageReceivedNotification);
            if (messageReceivedNotification.HandledResponse)
            {
                return null;
            }

            if (messageReceivedNotification.Skipped)
            {
                return null;
            }

            // runtime always adds state, if we don't find it OR we failed to 'unprotect' it this is not a message we should process.
            AuthenticationProperties properties = null;
            if (properties == null)
            {
                return null;
            }
        }
        catch (Exception exception)
        {
            // We can't await inside a catch block, capture and handle outside.
            authFailedEx = ExceptionDispatchInfo.Capture(exception);
        }

        if (authFailedEx != null)
        {
            //Refresh the configuration for exceptions that may be caused by key rollovers.The user can also request a refresh in the notification.
            if (this.Options.RefreshOnIssuerKeyNotFound && authFailedEx.SourceException.GetType() == typeof(SecurityTokenSignatureKeyNotFoundException))
                {
                    this.Options.ConfigurationManager.RequestRefresh();
                }

            var authenticationFailedNotification = new AuthenticationFailedNotification<OpenIdConnectMessage, OpenIdConnectAuthenticationOptions>(this.Context, this.Options)
            {
                ProtocolMessage = openIdConnectMessage,
                Exception = authFailedEx.SourceException
            };
            await this.Options.Notifications.AuthenticationFailed(authenticationFailedNotification);
            if (authenticationFailedNotification.HandledResponse)
            {
                return null;
            }
            if (authenticationFailedNotification.Skipped)
            {
                return null;
            }

            authFailedEx.Throw();
        }

        return null;
    }
}

以上代码是导致此错误发生所需的最少代码,一旦代码使用来自 options.notifications 的任何内容,应用程序会立即崩溃并出现未找到方法异常。

通知属性设置如下:

 Notifications = new OpenIdConnectAuthenticationNotifications
                 {
                     RedirectToIdentityProvider = context =>
                     {
                         if (Startup.IsApiRequest(context.Request))
                         {
                             context.HandleResponse();
                             return Task.FromResult(0);
                         }
                         context.ProtocolMessage.RedirectUri = redirectUri;
                         context.ProtocolMessage.PostLogoutRedirectUri = postLogoutRedirectUri;
                         return Task.FromResult(0);
                     },
                     AuthenticationFailed = context =>
                     {
                         context.OwinContext.Response.Redirect("/Home/Error");
                         context.HandleResponse(); // Suppress the exception

                         return Task.FromResult(0);
                     },
                     AuthorizationCodeReceived = context =>
                     {
                         var userIdentity = context.AuthenticationTicket.Identity;
                         userIdentity = userIdentity.TransformClaims();

                         context.AuthenticationTicket = new AuthenticationTicket(
                             userIdentity,
                             context.AuthenticationTicket.Properties
                         );

                         return Task.FromResult(0);
                     },
                     SecurityTokenReceived = context => Task.FromResult(0),
                 }
             });

小提示我不是这段代码的原始开发者....

尝试替换

using Microsoft.IdentityModel.Protocols;

using Microsoft.IdentityModel.Protocols.OpenIdConnect;

代码中的任何地方。这应该可以解决问题。