mvc中的returnURL变为null [英] ReturnURL in mvc is getting null

查看:190
本文介绍了mvc中的returnURL变为null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我在mvc中尝试表单身份验证时,如果用户试图调用一个页面,则返回null。



以下是我的相同代码



在家庭控制器登录中,我正在编写以下代码



  public  ActionResult 索引()
{
return 查看();
}

public ActionResult Login(string ReturnUrl)
{
if (Request.Form [ UserName] == abcsde&&& Request.Form [ 密码] == kusdfasdfmar
{
FormsAuthentication.SetAuthCookie( abcsde,true);
if (!string.IsNullOrEmpty(ReturnUrl))
Response.Redirect(ReturnUrl);
else

Response.Redirect(FormsAuthentication.LoginUrl);
}
返回 查看 索引);
}
[授权]
public ActionResult 默认()
{
return 查看();
}







在Indexview页面下面的代码返回



 @ {
ViewBag.Title =登录;
ViewBag.ReturnUrl = Request [ReturnUrl];
}

< h2>登录< / h2>
@using(Html.BeginForm(Login,Login,FormMethod.Post,new {ReturnUrl = Request.QueryString [ReturnUrl]}))
{
@Html。 TextBox(UserName)
< br>< / br>
@ Html.TextBox(密码)
<输入类型=提交值=登录/>

}





所以当用户尝试默认页面时,它成功重定向到登录页面,但是因为return url null页面没有重定向到默认页面。在这方面帮助

解决方案

我真的相信你需要使用Request对象,而不是获取QueryString属性,因为在上面的代码中,您使用的是我正在讨论的相同方法。该值为null,只是因为有





  1. 要么没有带此名称的QueryString
  2. 或者它的值为null
  3. 或者数据是作为POST请求发送的,而不是作为GET发送的;与选项1相同。





 Html.BeginForm( 登录 登录 ,FormMethod.Post,
new {ReturnUrl = Request [ ReturnUrl]});





..这将得到来自的ReturnUrl请求,无论是作为QueryString附加,还是通过Form传递。 QueryString方法只返回一个字符串作为值,如果有一个QueryString用于追加到URL的ReturnUrl(例如这一页?ReturnUrl = defaultPage)。然而,即使从表单传递数据,或者如果它附加到URL,Request方法也会捕获数据;在这两种情况下。



我自己,使用简单的Request方法获取数据,因为它有可能通过POST方法从表单传递数据,在这种情况下没有QueryString,不仅如此,但许多其他条件也显示在哪里 .QueryString 方法失败。


为了拥有填充了ReturnUrl(并且你应该登录页面功能),你需要告诉ASP.NET框架哪个动作是你的登录操作 - 因为它不是直截了当的。

所以,你需要调整你的网页.config

 <  身份验证 < span class =code-attribute>   mode   = 表格 >  
< 表单 loginUrl = 〜/帐户/登录 timeout = 30 name = 。MVC3FORMSAUTH cookieless = UseCookies slidingExpiration = true 保护 = 所有 / >
< / authentication >



请参阅: https://msdn.microsoft.com/en-us/library/system.web.security.formsauthentication.loginurl(v = vs.110 ).aspx [ ^ ]。如您所见,还有其他属性需要考虑,因此请检查其余属性: https://msdn.microsoft.com/en-us/library/system.web.security.formsauthentication(v = vs.110).aspx [ ^ ]


Hi when I am trying forms authentication in mvc returnurl is coming null when user trying to call one page.

below is my same code

In Home controller login I am writing the following code

public ActionResult Index()
        {
            return View();
        }

        public ActionResult Login(string ReturnUrl)
        {
            if (Request.Form["UserName"] == "abcsde" && Request.Form["Password"] == "kusdfasdfmar")
            {
                FormsAuthentication.SetAuthCookie("abcsde", true);
                if (!string.IsNullOrEmpty(ReturnUrl))
                    Response.Redirect(ReturnUrl);
                else

                    Response.Redirect(FormsAuthentication.LoginUrl);
            }
            return View("Index");
        }
        [Authorize]
        public ActionResult Default()
        {
            return View();
        }




in Indexview page following code is return

@{
    ViewBag.Title = "Login";
    ViewBag.ReturnUrl = Request["ReturnUrl"];
}

<h2>Login</h2>
@using (Html.BeginForm("Login", "Login", FormMethod.Post, new { ReturnUrl = Request.QueryString["ReturnUrl"]}))
{
   @Html.TextBox("UserName")
   <br></br>
   @Html.TextBox("Password")
   <input type =submit value="Login" />

}



so when user trying for default page it is successfully redirecting to the login page but because of return url null the page is not redirecting to the default page .kindly help in this regard

解决方案

I really believe that you need to be using the Request object, instead of getting the QueryString property, because in the code above this one, you're using the same method I'm talking about. The value is null, only because there is


  1. either no QueryString with this name
  2. or the value of it is null
  3. or the data was sent as a POST request, not as GET; same as option 1.



Html.BeginForm("Login", "Login", FormMethod.Post, 
                new { ReturnUrl = Request["ReturnUrl"]});



.. this would get the ReturnUrl from the Request, whether it is attached as a QueryString, or passed through a Form. QueryString method would only return a string as a value, if there is a QueryString for ReturnUrl appended to the URL (such as this one page?ReturnUrl=defaultPage). Whereas, the Request method will capture the data even if it was passed from the form, or if it is attached to the URL; in both cases.

I would myself, go with the simple Request method of getting the data, because it a chance of data being passed from a form with a POST method, in this case there is no QueryString and not only this, but many other conditions also show up where .QueryString method fails.


In order to have ReturnUrl populated (and you login page function as it should), you need to tell ASP.NET framework which action is your login action - as it is not straightforward.
So, you need to adjust your web.config

<authentication mode="Forms">
      <forms loginUrl="~/Account/Login" timeout="30" name=".MVC3FORMSAUTH" cookieless="UseCookies" slidingExpiration="true" protection="All"/>
    </authentication>


See: https://msdn.microsoft.com/en-us/library/system.web.security.formsauthentication.loginurl(v=vs.110).aspx[^]. As you see there are other properties to consider, so check the rest of the properties also: https://msdn.microsoft.com/en-us/library/system.web.security.formsauthentication(v=vs.110).aspx[^]


这篇关于mvc中的returnURL变为null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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