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

后端 未结 3 1142
暗喜
暗喜 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:

    
      
    
    
    [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.

提交回复
热议问题