I am trying create a login page that should redirect to an action result on successful login. I have user roles that should check. For now I can validate user but it does not redirect to Authorize Action result. Besides it provide return url to login page. Here is my Code for Account Controller
public ActionResult Login()
{
return View();
}
[HttpPost]
public ActionResult Login(string UserName, string Password)
{
if (new UserManager().IsValid(UserName, Password))
{
string userName = "Test Name"; //Hard coded for test purpose
string[] userRoles = {"Admin"};//Hard coded for test purpose
ClaimsIdentity identity = new ClaimsIdentity(DefaultAuthenticationTypes.ApplicationCookie);
identity.AddClaim(new Claim(ClaimTypes.NameIdentifier, userName));
userRoles.ToList().ForEach((role) => identity.AddClaim(new Claim(ClaimTypes.Role, role)));
identity.AddClaim(new Claim(ClaimTypes.Name, userName));
AuthenticationManager.SignIn(identity);
return RedirectToAction("Index","Home"); // auth succeed
// auth succeed
}
// invalid username or password
ModelState.AddModelError("", "invalid username or password");
return View();
}
private IAuthenticationManager AuthenticationManager
{
get { return HttpContext.GetOwinContext().Authentication; }
}
And Home controller
[Authorize]
public ActionResult Index()
{
return View(); //Which contain Dashboard for admin panel
}
But problem is when it appear to return RedirectToAction("Index","Home");it not redirect but URL change to ../Account/Login?ReturnUrl=%2fHome.
This is very new to me (Past I restrict user from database), Is there anything wrong with this code? Thanks in advance....