【问题标题】:An exception of type 'System.InvalidOperationException' occurred in System.Web.dll but was not handled in user codeSystem.Web.dll 中出现“System.InvalidOperationException”类型的异常,但未在用户代码中处理
【发布时间】:2015-05-30 14:34:17
【问题描述】:

我不是开发人员。 我进入迁移项目。我正在尝试将应用程序从 VS 2010 迁移到 VS 2013,在 VS 2010 中,应用程序运行良好,没有任何错误。

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

System.Web.dll 中出现“System.InvalidOperationException”类型的异常,但未在用户代码中处理

附加信息:该方法只能在 身份验证事件。

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

请帮我解决这个问题。

【问题讨论】:

  • 错误的哪一部分你不明白?
  • 代码中有对 System.Security 的引用吗?
  • 一个快速的谷歌足以告诉你这是一个突破性的变化(见第 7 点)iis.net/learn/application-frameworks/…
  • 它与 System.Web.MVC 相关。我的疑问是在迁移到 VS 2013 之后,我是否应该对代码进行任何更改以使这些东西正常工作。我对编码真的很陌生,我不是编码专家。所以需要别人的帮助。
  • 不要采取错误的方式,但如果您是编写和理解代码的新手,那么您不应该从尝试迁移项目开始。尽管从 VS2010 迁移到 VS2013 是什么意思?通常,只有在尝试更改要编译的运行时版本时才会遇到问题,而不仅仅是在新版本的 Visual Studio 中打开项目。在 Visual Studio 2013 中右键单击您的项目,导航到属性。在属性下导航到应用程序并检查目标框架。您可能需要将其更改为 VS2010 中的任何内容。

标签: c# asp.net asp.net-mvc .net-4.0


【解决方案1】:

据此http://www.iis.net/learn/application-frameworks/building-and-running-aspnet-applications/aspnet-20-breaking-changes-on-iis

"您将收到 ASP.NET 500 – 服务器错误:此方法只能在身份验证事件之后调用。在 PostAuthenticateRequest 之前访问时,HttpRequest.LogonUserIdentity 会抛出 InvalidOperationException,因为在客户端之前该属性的值是未知的已通过身份验证。”

解决方法是将代码移动到 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 事件中删除所有代码。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-10-25
    • 1970-01-01
    • 1970-01-01
    • 2022-01-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多