好吧,就像标题所说的那样,我的登录操作中的returnUrl参数始终为null,我遵循默认的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]”装饰器是什么。
提前致谢!
答案 0 :(得分:0)
我猜你正在设置两件事但是用作一个:在你设置的.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)
请检查并重试,参数区分大小写。
答案 1 :(得分:0)
在参数中你有:
new { ReturnUrl = ViewBag.ReturnUrl }
^
|
|
但在行动参数是:
public ActionResult Login(LoginModel usuario, string returnUrl)
^
|
|
答案 2 :(得分:0)
如果您使用自定义身份验证,则需要添加方法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 }));
}