我怎么使用RETURNURL = ViewBag.ReturnUrl在MVC 4 [英] How am I supposed to use ReturnUrl = ViewBag.ReturnUrl in MVC 4

查看:1205
本文介绍了我怎么使用RETURNURL = ViewBag.ReturnUrl在MVC 4的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的工作ASP.NET MVC 4'的应用程序。我使用/学习SimpleMembershipProvider并尝试坚持由 VS2012 互联网模板创建的默认逻辑(如我没有记错,一个与SimpleMembershipProvider'开箱即用)。

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 Internet template (if I'm not mistaken, the one with 'SimpleMembershipProvider' out of the box).

我被困在的AccountController 在这里我只是想不出把我究竟如何可以使用这个方法:

I'm stuck at the AccountController where I just can't figure put how exactly I can use this method:

private ActionResult RedirectToLocal(string returnUrl)
        {
            if (Url.IsLocalUrl(returnUrl))
            {
                return Redirect(returnUrl);
            }
            else
            {
                return RedirectToAction("Index", "Home");
            }
        }

据我了解整个构思是让从那里你决定登录(我要完成什么)重定向到的位置。我看了看它是如何在视图中使用:

From what I understand the whole idea is to get redirected to the location from where you've decided to log in (exactly what I want to accomplish). I took a look at how it's used in the view :

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

寻找一个地方,居然 ViewBag.ReturnUrl 设置了一些价值,我只拿到了这个方法在这里:

Look for a place where actually ViewBag.ReturnUrl is set with some value and I only got this method here:

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

和我得到pretty困惑我应该究竟如何获得位置/ URL。我设置一些断点,我从来没有见过 RETURNURL 来从不同的东西在这种情况下,似乎pretty合乎逻辑的我,因为它没有得到任何地方的价值(除非我错过了,当然东西)。

and I'm getting pretty confused about how exactly I'm supposed to get the location/url. I set some breakpoints and I have never seen returnUrl to be something different from null which in this scenario seems pretty logical to me since it doesn't get value anywhere (unless I miss something of course).

所以,我真的想不出如何工作。我张贴上面只是为了显示我试图做我的功课,我调查,就像我可以,但我没有找到答案,所以我在这里问。难道你这实际上是如何工作提供解释/例子吗?

So I really can't figure out how this work. I post the above just to show that I tried to do my homework, I investigate as much as I could but I didn't found an answer so I ask here. Could you provide explanation/example on how this actually work?

推荐答案

在使用窗体身份验证和用户没有通过验证或授权的ASP.NET安全管道将重定向到登录页面,并通过作为查询字符串参数在 RETURNURL 的等于重定向到登录页面的页面。登录操作抓住这个参数的值,并把它在ViewBag,因此它可以被传递到视图。

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();
    }

视图然后存储如在观赏这一行code的形式此值。

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);
    }

如果模型的状态是有效的,它们被调用的 WebSecurity.Login 的方法,然后调用该方法的 RedirectToLocal 的用的 RETURNURL 这些来自查看,原本就形成创建的视图登录操作。

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.

RETURNURL 的值为null,如果用户不重定向到登录页面时,他们只需点击登录链接页面的默认布局顶部的情况下。在这种情况下,用户将成功登录后被重定向到主页。整个目的的 RETURNURL 的是自动发送用户返回到他们试图访问他们被认证之前的页面/授权。

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.

这篇关于我怎么使用RETURNURL = ViewBag.ReturnUrl在MVC 4的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆