System.Web.dll 中发生类型 'System.InvalidOperationException' 的异常,但未在用户代码中处理

An exception of type 'System.InvalidOperationException' occurred in System.Web.dll but was not handled in user code

我不是开发人员。 我正在参与迁移项目。我正在尝试将一个应用程序从 VS 2010 迁移到 VS 2013,在 VS 2010 中,该应用程序 运行 正常,没有任何错误。

但是在迁移之后,当 运行 应用程序出现以下 运行 时错误。我只是被困在这里。

An exception of type 'System.InvalidOperationException' occurred in System.Web.dll but was not handled in user code

Additional information: This method can only be called after the authentication event.

Global.asax中的小代码用于检查应用程序的权限。

 public void Application_BeginRequest(object sender,EventArgs e)
    {
        if (!Request.Path.Contains("NoAccess"))
        {
            //Checks rights to application
            var secData = SecurityProvider.GetSecurityData("TEST", Request.LogonUserIdentity, false);

            var access = new SecurityClient(secData).HasAccess("TEST", SecurityAccessLevel.Read);

            if (!access)
            {
                Response.Redirect("NoAccess");
            }
        }

错误指向Request.LogonUserIdentity

请帮我解决这个问题。

按照这个 http://www.iis.net/learn/application-frameworks/building-and-running-aspnet-applications/aspnet-20-breaking-changes-on-iis

"You will receive an ASP.NET 500 – Server Error: This method can only be called after the authentication event. HttpRequest.LogonUserIdentity throws an InvalidOperationException when accessed before PostAuthenticateRequest, because the value of this property is unknown until after the client has been authenticated."

解决方法是将代码移至 PostAuthenticateRequest 事件(或更高版本)

所以将此添加到您的 Global.asax

protected void Application_OnPostAuthenticateRequest(object sender, EventArgs e)
{
    if (!Request.Path.Contains("NoAccess"))
    {
        //Checks rights to application
        var secData = SecurityProvider.GetSecurityData("TEST", Request.LogonUserIdentity, false);

        var access = new SecurityClient(secData).HasAccess("TEST", SecurityAccessLevel.Read);

        if (!access)
        {
            Response.Redirect("NoAccess");
        }
    } 
}

然后删除 Application_BeginRequest 事件中的所有代码。