【问题标题】:Validation of viewstate MAC failed and anti XSRF issue视图状态 MAC 验证失败和反 XSRF 问题
【发布时间】:2014-09-09 21:13:19
【问题描述】:

我知道这已在这里发布了很多次,并提出了宝贵的建议和修复建议。但他们都没有帮助。 我们有一个电子商务网站,它是在线订购系统。我们的网站托管在带有 .net framework 4.0 的 IIS 7.0 上。(这是唯一在 IIS 上运行的网站)。在我们的测试环境中,一切正常,我们部署了它。 现在,当用户尝试访问该站点或者如果他在该站点中并浏览到其他页面时,我们会收到 MAC ERROR,则用户将面临此错误。这是堆栈跟踪。 例外:视图状态 MAC 的验证失败。如果此应用程序由 Web Farm 或集群托管,请确保配置指定相同的 validationKey 和验证算法。 AutoGenerate 不能在集群中使用。

Stack Trace: System.Web.HttpException

Stack Trace: 

at System.Web.UI.ViewStateException.ThrowError(Exception inner, String persistedState, String errorPageMessage, Boolean macValidationError) at System.Web.UI.ViewStateException.ThrowMacValidationError(Exception inner, String persistedState) at System.Web.UI.ObjectStateFormatter.Deserialize(String inputString, Purpose purpose) at System.Web.UI.ObjectStateFormatter.System.Web.UI.IStateFormatter2.Deserialize(String serializedState, Purpose purpose) at System.Web.UI.Util.DeserializeWithAssert(IStateFormatter2 formatter, String serializedState, Purpose purpose) at System.Web.UI.HiddenFieldPageStatePersister.Load() at System.Web.UI.Page.LoadPageStateFromPersistenceMedium() at System.Web.UI.Page.LoadAllState() at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) at System.Web.UI.Page.ProcessRequest(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) at System.Web.UI.Page.ProcessRequest(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) at System.Web.UI.Page.ProcessRequest() at System.Web.UI.Page.ProcessRequestWithNoAssert(HttpContext context) at System.Web.UI.Page.ProcessRequest(HttpContext context) at ASP.pages_usagereports_index_aspx.ProcessRequest(HttpContext context) at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)

当我们在基本页面中实施代码以修复 CSRF 问题时,我们开始遇到此问题。 如果我们取出代码并与用户一起测试,我们不会收到任何错误。 到目前为止,我们已经尝试了这些解决方案 1) 应用机器密钥到网站,即使我们不是网络农场环境。 2) 我们不想将 enableviewstate 禁用为 false 或将 viewstateencryption 模式禁用为 never,因为最好不要这样做。我们甚至尝试过,但没有帮助。 3)我们尝试为 base.viewstateuserkey 分配不同的组合,例如 userhostaddress 和 user Agent 的组合,但它没有帮助。 4) 我们从此链接使用了 Microsoft .net 网络保护库 http://software-security.sans.org/developer-how-to/developer-guide-csrf 在此之后,我们开始收到另一个错误

Inner Exception Type: System.InvalidOperationException

Inner Exception: Validation of Anti-XSRF token failed.
Inner Stack Trace: 
at System.Web.UI.Page.HandleError(Exception e) at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) at System.Web.UI.Page.ProcessRequest(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) at System.Web.UI.Page.ProcessRequest(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) at System.Web.UI.Page.ProcessRequest() at System.Web.UI.Page.ProcessRequestWithNoAssert(HttpContext context) at System.Web.UI.Page.ProcessRequest(HttpContext context) at ASP.signin_aspx.ProcessRequest(HttpContext context) at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)

现在我们遇到了两个错误。 MAC 错误和 Anti XSRF 错误。 搜索了这个错误,我看到了很多帖子,但我试过了。他们都没有帮助。 非常感谢您对此提出任何建议。

提前致谢

【问题讨论】:

    标签: asp.net


    【解决方案1】:

    ViewState 的 MAC 是使用母版页中生成的 ANTiXsrfTokenKey 派生的。如果用户访问您的站点时令牌不存在(cookie 被禁用或 cookie 中的值已更改或删除),则页面将生成一个新令牌。然后发生的情况是,如果在回发的页面中有 ViewState,MAC 将使用新令牌进行检查并且不会匹配旧令牌,这是您可以看到的第一个错误。

    您的母版页中的 Xsrf 检查有点令人困惑,但使用存储在 cookie 中的 Xsrf 令牌并将其与页面中存储的值相匹配。通常这些会匹配,但如果另一个站点试图向您的站点发出非法请求,他们的站点将不会发送 Xsrf cookie,并且检查将失败。这令人困惑的原因是,如果您有一个 ViewState,这将在 Xsrf 检查之前失败。

    Xsrf 检查的另一个问题是它使用登录用户名作为检查的一部分。如果用户登录,然后 auth cookie 被删除或过期,这部分检查将失败,您将看到一个误导性的 Xsrf 失败,真正的错误是“身份验证已过期导致 Xsrf 检查失败”。

    我已经多次重做 Site.Master.cs 中的函数,以使这些错误更加清晰。

    希望对您有所帮助。

    【讨论】:

      猜你喜欢
      • 2010-09-24
      • 2011-12-18
      相关资源
      最近更新 更多