【发布时间】: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