I'm having an issue where redirecting to a secured action after setting a custom forms authentication ticket. Here's what is happening:
Can anybody shed any light?
My HomeController:
[Authorize]
public ActionResult Index()
{
return View();
}
My AccountController:
[HttpGet]
[AllowAnonymous]
public ActionResult Login(string returnUrl)
{
ViewBag.ReturnUrl = returnUrl;
return View();
}
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public ActionResult Login(LoginModel model, string returnUrl)
{
if (ModelState.IsValid)
{
bool bLogin = MyAuthentication.Login(model.UserName, model.Password);
if (bLogin)
{
Response.Cookies.Add(MyAuthentication.GetAuthenticationCookie(model.UserName.ToLower(), model.RememberMe));
RedirectToUrl(returnUrl);
}
else
ModelState.AddModelError("", "That is not a valid Username/Password combination");
}
return View(model);
}
private ActionResult RedirectToUrl(string returnUrl)
{
if (Url.IsLocalUrl(returnUrl))
return Redirect(returnUrl);
else
return RedirectToAction("Index", "Home");
}
Here is how I create the custom ticket (just adding userdata):
public static HttpCookie GetAuthenticationCookie(string UserName, bool persistLogin)
{
var userData = null; // Code removed for brevity
FormsAuthenticationTicket authTicket = new FormsAuthenticationTicket(
1,
UserName,
DateTime.Now,
DateTime.Now.AddMinutes(20),
persistLogin,
userData);
string encTicket = FormsAuthentication.Encrypt(authTicket);
return new HttpCookie(FormsAuthentication.FormsCookieName, encTicket);
}
Ugh!!!
RedirectToUrl(returnUrl);
Needs to be
return RedirectToUrl(returnUrl);