【问题标题】:returnUrl always null?returnUrl 总是为空?
【发布时间】:2014-05-27 04:55:54
【问题描述】:

好吧,正如标题所说,我的登录操作中的 returnUrl 参数始终为空,我遵循默认的 Internet 应用程序示例但我无法使其工作(我对 MVC 比较陌生)

景色

@using MundialDeFutbol.Models
@using System.Web.Optimization
@model LoginModel

@{
    ViewBag.Title = "Login";
}

<h3 class="text-info">Login</h3>
<div class="separadorHorizontal"></div>

@using (Html.BeginForm("Login", "Usuarios", new { ReturnUrl = ViewBag.ReturnUrl }, FormMethod.Post,
    new { @class = "form-horizontal", @style = "margin-top: 20px;" }))
{
    @Html.AntiForgeryToken()

    <div id="loginErrors" class="form-group">
        <div class="col-sm-offset-2 col-sm-10 ">
            <strong>
                @Html.ValidationSummary(true, null, new { @class = "text-danger", @id = "margin-top: 10px;" })
            </strong>
        </div>
    </div>
    <div class="form-group">
        <label for="Nick" class="col-sm-2 control-label">@Html.LabelFor(model => model.Nick)</label>
        <div class="col-sm-10">
            @Html.TextBoxFor(model => model.Nick, new { @class = "form-control", @placeholder = "Nombre de usuario" })
            <strong>
                @Html.ValidationMessageFor(model => model.Nick, null, new { @class = "text-danger", @style = "display: block; margin-top: 10px;" })
            </strong>
        </div>
    </div>
    <div class="form-group">
        <label for="Contraseña" class="col-sm-2 control-label">@Html.LabelFor(model => model.Contraseña)</label>
        <div class="col-sm-10">
            @Html.TextBoxFor(model => model.Contraseña, new { @class = "form-control", @placeholder = "Escribe contraseña", @type = "password" })
            <strong>
                @Html.ValidationMessageFor(model => model.Contraseña, null, new { @class = "text-danger", @style = "display: block; margin-top: 10px;" })
            </strong>
        </div>
    </div>
    <div class="form-group">
        <div class="col-sm-offset-2 col-sm-10">
            <button type="submit" class="btn btn-default">Entrar</button>
        </div>
    </div>
}

@section scripts {
    @Scripts.Render("~/bundles/jqueryval")
}

控制器

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

    [HttpPost]
    [AllowAnonymous] // --> Preguntar
    [ValidateAntiForgeryToken] // --> Preguntar
    public ActionResult Login(LoginModel usuario, string returnUrl)
    {
        if (ModelState.IsValid)
        {
            if (IsValid(usuario.Nick,usuario.Contraseña))
            {
                FormsAuthentication.SetAuthCookie(usuario.Nick, false);
                return Redirect(returnUrl);
            }
            else
            {
                ModelState.AddModelError("", "Usuario o contraseña incorrectos");
            }
        }
        else
        {
            var errors = ModelState.Keys.SelectMany(key => ModelState[key].Errors);
        }

        return View();
    }

最后,我想问一下“[AllowAnonymous]”和“[ValidateAntiForgeryToken]”装饰器是什么。

提前致谢!

【问题讨论】:

    标签: c# asp.net-mvc


    【解决方案1】:

    如果使用自定义认证,则需要在方法 OnAuthorization 中添加:ReturnUrl = filterContext.HttpContext.Request.RawUrl

    例子:

     public override void OnAuthorization(AuthorizationContext filterContext)
     {
    
        //Some logic here...
        filterContext.Result = new RedirectToRouteResult(
                               new RouteValueDictionary(
                               new { controller = "Account",
                                                 action = "Login", 
                                                 ReturnUrl = filterContext.HttpContext.Request.RawUrl }));            
     }
    

    【讨论】:

      【解决方案2】:

      我猜你正在设置两件事,但作为一个使用:在你下面设置的 .cshtml 页面中:

      @using (Html.BeginForm("Login", "Usuarios", new { ReturnUrl = ViewBag.ReturnUrl }, FormMethod.Post,
      new { @class = "form-horizontal", @style = "margin-top: 20px;" }))
      

      但在您发布登录操作时,您将其名称命名为“returnUrl”,如下所示:

      public ActionResult Login(LoginModel usuario, string returnUrl)
      

      请检查后重试,参数区分大小写。

      【讨论】:

      • 如上所述:[AllowAnonymous] 是一个可用于装饰动作或控制器的属性,如果这样,匿名用户可以访问您的网页;如果没有,则需要登录才能采取进一步的行动;另一个[ValidateAntiForgeryToken]用于防御跨站请求伪造;
      • 您好,感谢您的回复。但是不,它根本没有改变任何东西,登录的 GET “版本”甚至不带参数“returnUrl”,它始终为空。它应该保存单击登录链接的 url。这很奇怪,因为我做了与默认示例完全相同的事情:|
      • 这能解决您的问题吗?我的意思是统一网页中的参数名称和动作签名“RetrunUrl”和“returnUrl”?就像上面提到的 Ehsan Sajjad。
      • 现在所有参数都同名(ReturnUrl),即使是表单,依然返回null
      • 确保在发布登录信息时为参数 returnUrl 赋值。在您发布表单之前在 url 中检查它。如果一切正常,我真的很抱歉,我无法进一步帮助您。糟糕的一天。 :o(
      【解决方案3】:

      在参数中你有:

      new { ReturnUrl = ViewBag.ReturnUrl }
            ^
            |
            |
      

      但实际参数是:

      public ActionResult Login(LoginModel usuario, string returnUrl)
                                                           ^
                                                           |
                                                           |
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-01-26
        • 2018-03-20
        • 2013-03-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多