asp.net-mvc ASP.NET MVC 登录 ReturnUrl 始终为 NULL?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/827905/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-07 23:34:04  来源:igfitidea点击:

ASP.NET MVC Login ReturnUrl always NULL?

asp.net-mvcredirectlogin

提问by aherrick

Using Forms Authentication in ASP.NET MVC when trying to log back into a site, it puts a ReturnUrl parameter in the query string. My Logon action method accepts a "returnUrl" string. However it seems that returnUrl string is always null, even when it is clearly in the query string. Any thoughts on why this might be the case or a possible fix?

当尝试重新登录站点时,在 ASP.NET MVC 中使用表单身份验证时,它会在查询字符串中放置一个 ReturnUrl 参数。我的登录操作方法接受“returnUrl”字符串。然而,似乎 returnUrl 字符串始终为空,即使它显然在查询字符串中也是如此。关于为什么会出现这种情况或可能的解决方法的任何想法?

采纳答案by ?a?da? Tekin

Maybe you don't include the ReturnURLparameter into you login form's action attribute, thus posting to a URL without that parameter?

也许您没有将该ReturnURL参数包含在登录表单的 action 属性中,从而发布到没有该参数的 URL?

回答by davewasthere

This tends to happen when you're using one generic logon form, but you're explicitly specifying the Controller and ActionMethod (which is causing a form post, but losing the querystring)

当您使用一个通用登录表单时,往往会发生这种情况,但您明确指定了 Controller 和 ActionMethod(这会导致表单发布,但丢失了查询字符串)

Just to clarify, this is what your code should look like in your BeginForm:

澄清一下,这就是你的代码在你的 BeginForm 中的样子:

Html.BeginForm("LogOn", "Account", new { ReturnUrl = Request.QueryString["ReturnUrl"] })

EDIT: This is by design as RickAnd mentions in comments below. However it doesn't allow for the UI pattern of being deep in a site, clicking on LogOn, then returning to the page you were previously on, if it allows anonymous users. It's a commonly requested pattern. David Allen's approach to LogOff would also work nicely for a clean redirect at LogOn.

编辑这是 RickAnd 在下面的评论中提到的设计。但是,如果它允许匿名用户,它不允许深入站点,单击登录,然后返回到您以前所在的页面的 UI 模式。这是一个普遍要求的模式。David Allen 的 LogOff 方法也适用于 LogOn 的干净重定向。

回答by Amirhossein Mehrvarzi

Basically, The Asp.net MVC has some hidden features. For Example when you pass variable 'id' to controller action, it interprets 'id' as default identifier and puts it on browser query with fore slash.By using another name instead of 'id' we will see '?' rather than fore slash. Because of setting the 'id' name on RegisterRoutesmethod on global.asaxfile.

基本上,Asp.net MVC 有一些隐藏的功能。例如,当您将变量“id”传递给控制器​​操作时,它会将“id”解释为默认标识符,并使用前斜杠将其放在浏览器查询中。通过使用另一个名称而不是“id”,我们将看到“?” 而不是前斜线。因为RegisterRoutesglobal.asax文件上的方法上设置了“id”名称。

In this Problem you have created a custom data passer to controller by using this code:

在这个问题中,您使用以下代码创建了一个自定义数据传递器到控制器:

using(Html.BeginForm("LogOn", "Account", FormMethod.Post))
{
//form fields
}

So Asp.net MVC ignores other useful data to pass to controller action, and we'll see returnUrlalways null.

因此,Asp.net MVC 会忽略传递给控制器​​操作的其他有用数据,我们将returnUrl始终看到null.

While, by using this, Asp.net MVC acts Correctly and returnUrlis mounted:

而通过使用它,Asp.net MVC 可以正确运行并returnUrl安装:

using(Html.BeginForm())
{
//form fields in LogOn View
}

By the way, When we use custom data passer to controller action, must pass another data manuallylike this:

顺便说一句,当我们使用自定义数据传递器来控制动作时,必须像这样手动传递另一个数据:

using(Html.BeginForm("LogOn", "Account", new {ReturnUrl = Request.QueryString["ReturnUrl"] }))
{
//form fields
}

回答by David Allen

There are two ways I can think of to deal with logon and logoff scenarios. Dave Beer outlined one way, above. There is another approach that works in many situations. I used it when I coded the NerdDinner tutorial. The tutorial provides us with a logoff function that logs you off and takes you home. I did not want that. I wanted to return to the page I was on before I logged off. So I modified my Account controller logoff action to look like this

我可以想到两种方法来处理登录和注销场景。Dave Beer 概述了上面的一种方式。还有另一种方法适用于许多情况。我在编写 NerdDinner 教程时使用了它。本教程为我们提供了注销功能,可让您注销并带您回家。我不想那样。我想回到我注销前所在的页面。所以我修改了我的帐户控制器注销操作,如下所示

   public ActionResult LogOff()
    {
        FormsService.SignOut();
        return Redirect(Request.UrlReferrer.ToString());
    }

You can get fancier and pass in a returnUrl and test for it, in case you want to override this behavior. But I don't need that. This achieves the desired result. The Logon can work similarly. Maybe there are ways to use the MVC framework to do this for me, but until I learn them, this is VERY simple and works reliably.

您可以更高级并传入 returnUrl 并对其进行测试,以防您想覆盖此行为。但我不需要那个。这达到了预期的结果。登录可以类似地工作。也许有一些方法可以使用 MVC 框架来为我做这件事,但在我学会它们之前,这非常简单并且工作可靠。

回答by kazimanzurrashid

Try the following:

请尝试以下操作:

        public static MvcForm BeginForm(this HtmlHelper htmlHelper, string id)
    {
        string formAction = htmlHelper.ViewContext.HttpContext.Request.RawUrl;

        TagBuilder tagBuilder = new TagBuilder("form");

        tagBuilder.MergeAttribute("id", id);
        tagBuilder.MergeAttribute("action", formAction);
        tagBuilder.MergeAttribute("method", HtmlHelper.GetFormMethodString(FormMethod.Post), true);

        HttpResponseBase httpResponse = htmlHelper.ViewContext.HttpContext.Response;
        httpResponse.Write(tagBuilder.ToString(TagRenderMode.StartTag));

        return new MvcForm(htmlHelper.ViewContext.HttpContext.Response);
    }

First ensure you have set the login url in the web.config, Next, ensure your Signin Form does not contain anything like action, for example:

首先确保您在 web.config 中设置了登录 url,接下来,确保您的登录表单不包含任何类似操作的内容,例如:

View:

看法:

If you specify action you will always get null for return url:

如果您指定操作,您将始终为返回 url 获取 null:

Controller:

控制器:

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult SignIn(string userName, string password, bool? rememberMe, string returnUrl)
{
}