ASP.NET MVC Login ReturnUrl always NULL?

后端 未结 5 2336
死守一世寂寞
死守一世寂寞 2020-12-08 06:15

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 \"returnU

5条回答
  •  执念已碎
    2020-12-08 06:42

    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:

    View:

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

    Controller:

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

提交回复
热议问题