Note that there are some explanatory texts on larger screens.

plurals
  1. POASP.Net MVC 4 Custom Authorization Ticket Redirect Issue
    primarykey
    data
    text
    <p>I'm having an issue where redirecting to a secured action after setting a custom forms authentication ticket. Here's what is happening:</p> <ol> <li>I navigate to Site/Home/Index</li> <li>I'm automatically redirected to Site/Account/Login</li> <li>I login with a valid user/pass</li> <li>The RedirecToUrl() function attempts to redirect me back to Site/Home/Index, yet I'm automatically returned back to Site/Account/Login</li> <li>The request IS authenticated. If I manually navigate to Site/Home/Index, I'm allowed in.</li> </ol> <p>Can anybody shed any light?</p> <p>My HomeController:</p> <pre><code>[Authorize] public ActionResult Index() { return View(); } </code></pre> <p>My AccountController:</p> <pre><code> [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"); } </code></pre> <p>Here is how I create the custom ticket (just adding userdata):</p> <pre><code> 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); } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload