How am I supposed to use ReturnUrl = ViewBag.ReturnUrl in MVC 4

后端 未结 3 1134
暗喜
暗喜 2020-12-07 18:47

I\'m working on \'ASP.NET MVC 4\' application. I\'m using/learning SimpleMembershipProvider and try to stick to the default logic created by VS2012 with the

相关标签:
3条回答
  • 2020-12-07 19:28

    That's because the default ASP.NET MVC template is using Forms authentication, and controllers are decorated with [Authorize] attribute:

    <authentication mode="Forms">
      <forms loginUrl="~/Account/Login" timeout="2880" />
    </authentication>
    
    [Authorize]
    public class AccountController : Controller
    {
        //...
    }
    

    That means that if the user is not authenticated it will be redirected to the logon page defined in the LoginUrl attribute of the forms element.

    During the redirection, FormsAuthentication which is an HttpModule will append the url which was requested in the query string automatically.

    So if you navigate to /Account/Login, you wont get anything in the query string since it is decorated with [AllowAnonymous] attribute. But if you navigate to /Account/Manage you'll notice that the returnUrl in the query string becomes /Account/Manage (/Account/Login?ReturnUrl=%2fAccount%2fManage)

    So you are not setting the returnUrl, the framework does it for you, you just use it in the AccountController to know where to redirect the user after he is authenticated.

    0 讨论(0)
  • 2020-12-07 19:29

    When using forms authentication and the user is not authenticated or authorized the ASP.NET security pipeline will redirect to the login page and pass as a parameter in the query string the returnUrl equal to the page that redirected to the login page. The login action grabs the value of this parameter and puts it in the ViewBag so it can be passed to the View.

        [AllowAnonymous]
        public ActionResult Login(string returnUrl)
        {
            ViewBag.ReturnUrl = returnUrl;
            return View();
        }
    

    The View then stores this value in the form as shown by this line of code in the View.

    @using (Html.BeginForm(new { ReturnUrl = ViewBag.ReturnUrl }))
    

    The reason it is stored in the View is so that when the user does a Submit after entering their user name and password, the controller action that handles the post back will have access to this value.

        [HttpPost]
        [AllowAnonymous]
        [ValidateAntiForgeryToken]
        public ActionResult Login(LoginModel model, string returnUrl)
        {
            if (ModelState.IsValid && WebSecurity.Login(model.UserName, model.Password, persistCookie: model.RememberMe))
            {
                return RedirectToLocal(returnUrl);
            }
    
            // If we got this far, something failed, redisplay form
            ModelState.AddModelError("", "The user name or password provided is incorrect.");
            return View(model);
        }
    

    If the model state is valid and they are authenticated by calling the WebSecurity.Login method then it calls the method RedirectToLocal with the value of returnUrl which came from the View, which originally came form the login action that created the View.

    The returnUrl value will be null if the user is not redirected to the login page as is the case when they just click on the login link at the top of the page in the default layout. In this case the user will be redirected to the home page after successful login. The whole purpose of the returnUrl is to automatically send the user back to the page they were trying to access before they were authenticated/authorized.

    0 讨论(0)
  • 2020-12-07 19:39

    When an unauthenticated user tries to get into a section of your application which requires authentication, then returnUrl comes into the picture. The Url requested by the unauthenticated user is basically stored in returnUrl.

    You can go through the PluralSight tutorial: Building Applications with ASP.NET MVC 4

    0 讨论(0)
提交回复
热议问题