How does redirect to returnUrl work in Asp.Net MVC5

前端 未结 4 694
生来不讨喜
生来不讨喜 2020-12-31 11:55

I\'ve started a new MVC 5 site, using the new Asp.Net Identity with Owin. In my \"account\" controller which has the attribute [Authorize], I have fairly standard actions;

4条回答
  •  执笔经年
    2020-12-31 12:15

    As Sandeep Phadke told, the returnUrl Parameter is filled, because of configuration in startup.Auth.cs.

    The CookieAuthenticationOptions has a property ReturnUrlParameter which is by Default set to "returnUrl". That is the reason, why it looks like magic. You can Change it to whatever you want:

    app.UseCookieAuthentication(new CookieAuthenticationOptions
        {
            AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
            LoginPath = new PathString("/Account/Login"),
            ReturnUrlParameter = "returnTo"
        });
    

    Then you can Change the AccountController Login-Action to:

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

提交回复
热议问题