I started a new MVC 5 site using the new Asp.Net Identity with Owin. In my "account" controller, which has the [Authorize] attribute, I have fairly standard actions;
// GET: /User/Login [AllowAnonymous] public ActionResult Login(string returnUrl) { ViewBag.ReturnUrl = returnUrl; return View(); } // POST: /User/Login [HttpPost] [AllowAnonymous] [ValidateAntiForgeryToken] public async Task<ActionResult> Login(LoginViewModel model, string returnUrl) { try { if (ModelState.IsValid) { var userApi = new UserService(); var apiUser = await userApi.LogIn(UserManager, model.CardNumber, model.Pin, model.RememberMe); if (apiUser != null) { await SignInAsync(apiUser, model.RememberMe); if (string.IsNullOrEmpty(returnUrl)) { return RedirectToAction("UserLoggedIn", "User"); } } else { ModelState.AddModelError("", "Invalid username or password."); } } } catch (Exception ex) { Trace.TraceError("Cannot login {0}", ex.ToString()); Response.AppendToLog(ex.ToString()); ModelState.AddModelError("", ex.ToString()); } // If we got this far, something failed, redisplay form return View(model); }
My question is about the behavior of returnUrl, the above code works in the sense that if the user is not logged in and calls the action in the controller with the [Authorize] attribute, he is sent to the input of the action above, and then returned to the controller / action that was requested . Whatβs great, BUT how? And is it safe?
In this article on β Preventing Open Redirect Attacks β (for earlier versions of Asp.Net MVC), it is recommended that you check returnUrl that it is a local url before doing the redirection, that there is something that I would have to do or now it processed by the frame?
Cheers, Ola
Ola karlsson
source share