C# 设置ViewStateUserKey会给我一个";viewstate MAC验证失败”;错误
在我的C# 设置ViewStateUserKey会给我一个";viewstate MAC验证失败”;错误,c#,asp.net,asp.net-2.0,viewstate,failed-to-load-viewstate,C#,Asp.net,Asp.net 2.0,Viewstate,Failed To Load Viewstate,在我的BasePage类中,我的所有ASPX页面都源自以下内容: protected override void OnInit(EventArgs e) { base.OnInit(e); ViewStateUserKey = Session.SessionID; } 我还在Web.config中设置了machineKey。我不认为这个错误是因为web服务器场,因为这也发生在我的开发机器上 我的主机现在已升级到.NET 3.5 SP1。在这次更新之后,每次我使用上面的views
BasePage
类中,我的所有ASPX页面都源自以下内容:
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
ViewStateUserKey = Session.SessionID;
}
我还在Web.config
中设置了machineKey
。我不认为这个错误是因为web服务器场,因为这也发生在我的开发机器上
我的主机现在已升级到.NET 3.5 SP1。在这次更新之后,每次我使用上面的viewstateurskey
设置编译时,每次回发都会出现“viewstate MAC验证失败”错误
我做错了什么?最新的框架更新是否还需要此设置?能否使用EnableViewStateMac@Page属性关闭ViewStateMac编码?我现在通过将代码更改为:
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
if (User.Identity.IsAuthenticated)
ViewStateUserKey = User.Identity.Name;
}
好吧,我的谈话迟到了一年,但这怎么是正确的答案呢?这仅适用于经过身份验证的用户,并且使用
ViewStateUserKey
,因为用户名比会话id GUID容易猜测
顺便说一句,如果您想“修复”上面的代码,请使用会话ID,但是您必须设置会话变量,以便会话ID停止每次更改。前任。
Session[“Anything”]=DateTime.Now
ViewStateUserKey = Session.SessionID;
当然,这是假设您要使用会话,否则您需要使用一些其他密钥,例如保存在cookie中的用户名或任何其他guid。我已经搜索了很多次,以找到问题的最终原因。 微软的这篇文章确实帮助解释了所有不同的原因。 原因4是我们降落的对象,其ViewStateUserKeyValue无效 将ViewStateUserKey设置为Session.SessionID或User.Identity.Name对我们不起作用 由于以下原因,我们间歇性地得到验证错误。 当IIS重置应用程序池时,会话将被更新,从而导致错误。 我们在登录时删除会话以避免会话固定,这也会导致登录时出错 最终对我们起作用的是基于cookie的解决方案,该解决方案现在在VS2012中提供
public partial class SiteMaster : MasterPage
{
private const string AntiXsrfTokenKey = "__AntiXsrfToken";
private const string AntiXsrfUserNameKey = "__AntiXsrfUserName";
private string _antiXsrfTokenValue;
protected void Page_Init(object sender, EventArgs e)
{
//First, check for the existence of the Anti-XSS cookie
var requestCookie = Request.Cookies[AntiXsrfTokenKey];
Guid requestCookieGuidValue;
//If the CSRF cookie is found, parse the token from the cookie.
//Then, set the global page variable and view state user
//key. The global variable will be used to validate that it matches in the view state form field in the Page.PreLoad
//method.
if (requestCookie != null
&& Guid.TryParse(requestCookie.Value, out requestCookieGuidValue))
{
//Set the global token variable so the cookie value can be
//validated against the value in the view state form field in
//the Page.PreLoad method.
_antiXsrfTokenValue = requestCookie.Value;
//Set the view state user key, which will be validated by the
//framework during each request
Page.ViewStateUserKey = _antiXsrfTokenValue;
}
//If the CSRF cookie is not found, then this is a new session.
else
{
//Generate a new Anti-XSRF token
_antiXsrfTokenValue = Guid.NewGuid().ToString("N");
//Set the view state user key, which will be validated by the
//framework during each request
Page.ViewStateUserKey = _antiXsrfTokenValue;
//Create the non-persistent CSRF cookie
var responseCookie = new HttpCookie(AntiXsrfTokenKey)
{
//Set the HttpOnly property to prevent the cookie from
//being accessed by client side script
HttpOnly = true,
//Add the Anti-XSRF token to the cookie value
Value = _antiXsrfTokenValue
};
//If we are using SSL, the cookie should be set to secure to
//prevent it from being sent over HTTP connections
if (FormsAuthentication.RequireSSL &&
Request.IsSecureConnection)
responseCookie.Secure = true;
//Add the CSRF cookie to the response
Response.Cookies.Set(responseCookie);
}
Page.PreLoad += master_Page_PreLoad;
}
protected void master_Page_PreLoad(object sender, EventArgs e)
{
//During the initial page load, add the Anti-XSRF token and user
//name to the ViewState
if (!IsPostBack)
{
//Set Anti-XSRF token
ViewState[AntiXsrfTokenKey] = Page.ViewStateUserKey;
//If a user name is assigned, set the user name
ViewState[AntiXsrfUserNameKey] =
Context.User.Identity.Name ?? String.Empty;
}
//During all subsequent post backs to the page, the token value from
//the cookie should be validated against the token in the view state
//form field. Additionally user name should be compared to the
//authenticated users name
else
{
//Validate the Anti-XSRF token
if ((string)ViewState[AntiXsrfTokenKey] != _antiXsrfTokenValue
|| (string)ViewState[AntiXsrfUserNameKey] !=
(Context.User.Identity.Name ?? String.Empty))
{
throw new InvalidOperationException("Validation of
Anti-XSRF token failed.");
}
}
}
}
非常奇怪,我也有类似的问题3天了,现在我解决了。 1.我启用了表单身份验证,但ssl为false
<forms defaultUrl="~/" loginUrl="~/Account/Login.aspx" requireSSL="false" timeout="2880" />
是的,如果我这样做就行了。如果ViewStateUserKey设置没有任何用处,我宁愿删除它。另外,有时我会看到sessionid每次都会更改,直到会话被使用为止,而其他时候,会话cookie会立即发送到客户端,而似乎没有使用会话。我还不是100%确定为什么会这样,但我注意到了一些事情。这非常有帮助。我不知道ASP如果在it中没有保存任何内容,则不会保留会话。请您详细说明一下好吗?不仅仅是链接。解释了为什么其他解决方案对我们不起作用。并将代码示例发布到文章中。我目前正在尝试您的解决方案,到目前为止,它似乎表现良好。我没有深入检查它,但我想知道可选部分“web.config的功能并不完全符合我们的预期:)我对上述代码的主要问题是:我们是否正在通过将AntiXsrfTokenKey删除到浏览器中而从不更新它来执行类似于会话固定的操作?它能持续一年吗?给恶意用户这么多时间来编织一个新的攻击回发表单?@Druid我在这里有同样的问题,除了把上面的代码放在主页的Oninit事件中。。我是否需要在任何子页面中设置viewstateuserkey..请告诉我..@pratapk据我所知,没有必要在每个页面上都设置它。