Problem with Validate Anti Forgery

Posted by Mikael Egnér on Stack Overflow See other posts from Stack Overflow or by Mikael Egnér
Published on 2010-05-14T15:18:50Z Indexed on 2010/05/15 7:54 UTC
Read the original article Hit count: 316

Hi! I have a problem regarding MVC Anti forgery token. When I do my authentication I have pseudo code like this:

var user = userRepository.GetByEmail(email);
System.Threading.Thread.CurrentPrincipal = HttpContext.Current.User = user;

by doing so I'm able to get the current user in my code like this:

var user = HttpContext.Current.User as EntityUser;

This works fine until I add the [ValidateAntiForgeryToken] attribute to an action. When I add the attribute I get

A required anti-forgery token was not supplied or was invalid.

If I comment out this line:

System.Threading.Thread.CurrentPrincipal = HttpContext.Current.User = user;

The antiforgery validation works fine, but the I don't have my convenient way of getting my "EntityUser" from the HttpContext. Any ideas of how to work around this? Best regards Mikael

© Stack Overflow or respective owner

Related posts about asp.net-mvc

Related posts about asp.net-mvc-2