MVC3 AntiForgeryToken Issue

后端 未结 3 1410
醉梦人生
醉梦人生 2021-01-01 08:03

I am trying to implement AntiForgeryToken for my MVC3 Application. I am having a problem with AntiForgeryToken after setting FormAuthentication cookie. Here is a simple exam

3条回答
  •  没有蜡笔的小新
    2021-01-01 08:22

    The AntiForgeryToken Helper does not add any cookie to response, if a cookie with same name exist in the request. Also the AntiForgeryToken Helper uses Principal.Identity.Name to return a value for hidden field.

                AntiForgeryData formToken = new AntiForgeryData(cookieToken) {
                   Salt = salt,
                   Username = AntiForgeryData.GetUsername(httpContext.User)
                };
    

    So when your Login view uses Html.AntiForgeryToken, a new cookie is set on response and a hidden field with same value. When your Login view post this cookie with hidden field, no exception will be thrown because both request cookie and hidden field value matches. But in the case of About view, no additional cookie will be added to response, but due to IIdentty, a new hidden value will be return for helper. So when you post About action, an exception will raise because cookie and hidden value does not match.

    This may be a bug in AntiForgeryToken implementation.

提交回复
热议问题