【问题标题】:accessing two websites (built using mvc) simultaneously giving error message同时访问两个网站(使用 mvc 构建)给出错误消息
【发布时间】:2011-12-28 10:06:19
【问题描述】:

我正在尝试同时访问两个网站,这两个网站都是使用 MVC 构建的。如果我m logged In in one, I cant 访问其他。如何更正以下内容?

我收到错误消息:

无法验证数据。 说明:执行当前 Web 请求期间发生未处理的异常。请查看堆栈跟踪以获取有关错误及其源自代码的位置的更多信息。

异常详细信息:System.Web.HttpException:无法验证数据。

来源错误:

在执行当前 Web 请求期间生成了未处理的异常。可以使用下面的异常堆栈跟踪来识别有关异常起源和位置的信息。

堆栈跟踪:

[HttpException (0x80004005): 无法验证数据。]
System.Web.Configuration.MachineKeySection.EncryptOrDecryptData(布尔 fEncrypt, Byte[] buf, Byte[] 修饰符, Int32 start, Int32 length, Boolean useValidationSymAlgo, Boolean useLegacyMode, IVType ivType, 布尔符号数据)+4956871
System.Web.Configuration.MachineKeySection.EncryptOrDecryptData(布尔 fEncrypt, Byte[] buf, Byte[] 修饰符, Int32 start, Int32 length, Boolean useValidationSymAlgo, Boolean useLegacyMode, IVType ivType) +155 System.Web.Security.FormsAuthentication.Decrypt(String encryptedTicket) +283
MvcUI.MvcApplication.FormsAuthentication_OnAuthenticate(对象发送者, FormsAuthenticationEventArgs 参数)在 C:\Hg\MyProject\Code\MvcUI\Global.asax.cs:40
System.Web.Security.FormsAuthenticationModule.OnAuthenticate(FormsAuthenticationEventArgs e) +11497690
System.Web.Security.FormsAuthenticationModule.OnEnter(对象源, EventArgs eventArgs) +88
System.Web.SyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +80 System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +270


版本信息:Microsoft .NET Framework 版本:4.0.30319; ASP.NET 版本:4.0.30319.237


为两个应用插入相同的机器密钥后出现新的错误消息

值不能为空。 参数名称:值 说明:执行当前 Web 请求期间发生未处理的异常。请查看堆栈跟踪以获取有关错误及其源自代码的位置的更多信息。

异常详细信息:System.ArgumentNullException:值不能为空。 参数名称:值

来源错误:

第 71 行:{ 第 72 行:用户 = myProject.API.User.Load(用户名);第 73 行: HttpContext.Current.Cache.Add(key, user, null, System.Web.Caching.Cache.NoAbsoluteExpiration, Line 74: new TimeSpan(0, 2, 0), System.Web.Caching.CacheItemPriority.Default, 空值);第 75 行:}

[ArgumentNullException:值不能为空。 参数名称:值] System.Web.Caching.CacheEntry..ctor(String key, Object value, CacheDependency 依赖, CacheItemRemovedCallback onRemovedHandler, DateTime utcAbsoluteExpiration, TimeSpan rollingExpiration, CacheItemPriority priority, Boolean isPublic) +8942559 System.Web.Caching.CacheInternal.DoInsert(Boolean isPublic, String key, Object value, CacheDependency dependencies, DateTime utcAbsoluteExpiration, TimeSpan rollingExpiration, CacheItemPriority priority, CacheItemRemovedCallback onRemoveCallback, Boolean replace) +93 System.Web.Caching.Cache.Add(String key, Object value, CacheDependency dependencies, DateTime absoluteExpiration, TimeSpan rollingExpiration, CacheItemPriority priority, CacheItemRemovedCallback onRemoveCallback) +81 C:\Dev\myProject\Code\MvcUI\Global.asax.cs:73 中的 MvcUI.MvcApplication.GetUserFromCache(String userName) C:\Dev\myProject\Code\MvcUI\Global.asax.cs:40 中的 MvcUI.MvcApplication.FormsAuthentication_OnAuthenticate(Object sender, FormsAuthenticationEventArgs args) System.Web.Security.FormsAuthenticationModule.OnAuthenticate(FormsAuthenticationEventArgs e) +9043237 System.Web.Security.FormsAuthenticationModule.OnEnter(对象源,EventArgs eventArgs)+84 System.Web.SyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +148 System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +75


Global.asax.cs

公共类 MvcApplication : System.Web.HttpApplication { 公共静态无效RegisterGlobalFilters(GlobalFilterCollection过滤器) { filters.Add(new HandleErrorAttribute()); }

    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
        routes.IgnoreRoute("WebService/{*pathInfo}");

        routes.MapRoute(
                "Default", // Route name
                "{controller}/{action}/{id}", // URL with parameters
                new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
        );
    }

    public void FormsAuthentication_OnAuthenticate(object sender, FormsAuthenticationEventArgs args)
    {
        if (FormsAuthentication.CookiesSupported)
        {
            if (null != Request.Cookies[FormsAuthentication.FormsCookieName])
            {
                FormsAuthenticationTicket ticket = FormsAuthentication.Decrypt(Request.Cookies[FormsAuthentication.FormsCookieName].Value);
                args.User = new myProject.Web.UI.Classes.UserPrincipal(GetUserFromCache(ticket.Name));
            }
        }
        else
            throw new HttpException("Cookieless Forms Authentication is not supported for this application.");
    }

    public void WindowsAuthentication_OnAuthenticate(object sender, WindowsAuthenticationEventArgs args)
    {
        string username = args.Identity.Name.Substring(args.Identity.Name.IndexOf("\\") + 1);
        myProject.API.User user = GetUserFromCache(username);

        if (null == user)
            throw new HttpException("User could not be found.");

        args.User = new myProject.Web.UI.Classes.UserPrincipal(user);
    }

    protected void Application_Start()
    {
        AreaRegistration.RegisterAllAreas();

        RegisterGlobalFilters(GlobalFilters.Filters);
        RegisterRoutes(RouteTable.Routes);
    }

    private static myProject.API.User GetUserFromCache(string userName)
    {
        string key = "User " + userName;
        myProject.API.User user = (myProject.API.User)HttpContext.Current.Cache[key];
        if (null == user)
        {
            user = myProject.API.User.Load(userName);
            HttpContext.Current.Cache.Add(key, user, null, System.Web.Caching.Cache.NoAbsoluteExpiration,
                    new TimeSpan(0, 2, 0), System.Web.Caching.CacheItemPriority.Default, null);
        }

        return user;
    }
}

【问题讨论】:

  • 似乎正在使用相同的身份验证 cookie。
  • @leppie 我该如何纠正?
  • 您尝试过我和Softion 提供的解决方案吗?有什么反馈吗?

标签: asp.net-mvc


【解决方案1】:

我猜您正在使用表单身份验证?也许两个 cookie 都是使用相同的域但不同的机器密钥创建的。

因此,当站点 A 创建 cookie(此人登录)然后您转到站点 B(未登录)时,站点 B 会查看域并说嘿,此人已登录让我们解密数据,但是因为cookie 是使用 MACHINE KEY A 加密的,它失败了。

我的 2c :)

【讨论】:

  • 我不知道什么是 Gor Auth。是:) 你能给我一个链接吗?应该有一个配置区域,您可以在其中选择 cookie 的域,确保两个站点具有不同的域
  • 我正在使用表单身份验证。
  • ok 然后在 web.config 中的“authentication”节点下,您将看到一个“forms”节点。你可以在那里添加一个 domain="DOMAINNAME" 属性。确保两个站点的它们不同。
  • 问题是两个站点必须有相同的域,因为它们将被同一个人使用。
  • 我已按照 Softion 的某些说明进行操作,但之后我遇到了一些其他错误。请参阅编辑部分以获取更多信息。
【解决方案2】:

我猜:

1) 您的网站使用 2 个不同的自动生成的 MachineKey。 MachineKey 是用于对表单身份验证 cookie、哈希密码等内容进行编码/解码的常量密钥...

2) 您在两个应用中使用了相同的应用程序名称。

3) 我还猜想您正在尝试在同一台机器上设置测试网站和生产网站。

【讨论】:

  • 我已经从一个在线站点生成机器密钥并插入到两个应用程序中。现在我遇到另一个错误:[ArgumentNullException:值不能为空。参数名称:value] System.Web.Caching.CacheEntry..ctor(String key, Object value, CacheDependency dependency, CacheItemRemovedCallback onRemovedHandler, DateTime utcAbsoluteExpiration, TimeSpan slipExpiration, CacheItemPriority priority, Boolean isPublic) +8942559 请看编辑部分有完整错误留言
  • 应用名称是什么意思?它是程序集名称还是解决方案名称?解决方案名称不同但程序集名称不同
  • beta网站和生产网站不在同一台机器上
  • 乍一看,该错误消息看起来完全不相关。您正在尝试从缓存中获取/加载用户并且它失败了。 Global.asax 中发生了什么?您是否在应用程序启动或 global.asax 的开始请求中有代码?
  • 当我为两个应用程序插入相同的机器密钥时,出现了我在编辑部分中的错误消息。
【解决方案3】:

感谢大家尝试回答问题。确实,他们都很有帮助。我通过在 wenconfig 中添加一个机器密钥自己解决了这个问题,并且必须有一个表单名称。没有表单名,连机器键都没用

【讨论】:

  • 我在 web.config 中添加了 machineKey,但它不起作用:<machineKey validationKey="2D22CD7A956C67564FF77D740FF1377A71A3F5AEA01D8455CC53FA76F90DFF46D721A435FE7CCCF02FFFD7CA51816A69F1E86799E09F8AF698DCB56B387497F3" decryptionKey="9B2E487F2BA9785B0D427A2D911FC911C04F4AD48062D0AE1EC0C424E4D752DF" validation="SHA1" decryption="AES" /> 表单名称是什么意思。问候
  • 好的。问题是客户端使用远程桌面时。为什么?也许一些安全问题更新?问候。
  • 你能描述一下什么是'form name'吗?
猜你喜欢
  • 2013-08-20
  • 1970-01-01
  • 2021-11-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-02-05
  • 2021-10-12
  • 1970-01-01
相关资源
最近更新 更多