My MVC web is acting weird. When I login for the first time, the User.IsInRole("Admin") is returning true and everything works as expected.
But after I login and logout, when i try to login again, the User.IsInRole("Admin") always returns false. But this problem fixed after I tried to login again.
This is the code:
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public ActionResult Login(LoginModel model, string returnUrl)
{
string redirectUrl = returnUrl;
string userName = model.UserName;
UserProfile user = dbAccount.UserProfiles.Where(m => m.Email.Equals(userName, StringComparison.CurrentCultureIgnoreCase)).SingleOrDefault();
if (user != null)
{
userName = user.UserName;
}
if (ModelState.IsValid && WebSecurity.Login(userName, model.Password, persistCookie: model.RememberMe))
{
if (redirectUrl == null)
{
redirectUrl = User.IsInRole("Admin") ? "/Admin" : "/";
}
return RedirectToLocal(redirectUrl);
}
// If we got this far, something failed, redisplay form
ModelState.AddModelError("", "The user name or password provided is incorrect.");
return View(model);
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult LogOff()
{
WebSecurity.Logout();
Roles.DeleteCookie();
return RedirectToAction("Home", "Page");
}
Somehow after I logout, and login, the WebSecurity.Login() not giving me the correct user role.