Using VS 2013, standard MVC template, and the Identity provider framework
The user is logged in, and I have:
//....
UserManager.AddToRole(User.Identity.GetUserId(), "Members"); # Line X
RedirectToAction("Index", "Members");
And the Members controller is as follows:
[Authorize(Roles="Members")]
public class MembersController : Controller
{
// GET: Members
public ActionResult Index()
{
return View();
}
}
After Line X is executed, I can confirm that the user is added to the table dbo.AspNetUserRoles
. However, the user upon reaching the Members controller fails the role check. User.IsInRole("Members")
returns false.
If the user logs off and then logs in again, then access to the Members controller will go through, ie User.IsInRole("Members")
now returns true.
Is there some caching? Why the delay? How do I overcome it?
I also tried converting the method at Line X to an async method and used UserManager.AddToRoleAsync
. The same delayed effect is still there.