返回Url未在MVC登录表单中传递

时间:2016-10-21 18:37:54

标签: c# asp.net-mvc asp.net-mvc-4

我有一个网站,我现在按照本教程(http://benfoster.io/blog/aspnet-identity-stripped-bare-mvc-part-1)使用ASP.NET Identity,Owin等添加身份验证...当前而不是使用html帮助程序并使用下面的代码段对于视图,我创建了自己的自定义表单,但由于某种原因,我不认为我正确传递了返回URL,因为我得到了404错误。有人可以帮帮我吗?我是.NET MVC的初学者。提前谢谢。

@Html.ValidationSummary(true)

@using (Html.BeginForm())
{
  @Html.EditorForModel()
  <p>
    <button type="submit">Log In</button>
  </p>
}

模型

public class LogInModel
{
    [Required]
    [DataType(DataType.EmailAddress)]
    public string Email { get; set; }

    [Required]
    [DataType(DataType.Password)]
    public string Password { get; set; }

    [HiddenInput]
    public string ReturnUrl { get; set; }
}

控制器

[AllowAnonymous]
    public class AuthController : Controller
    {
        // GET: Auth
        [HttpGet]
        public ActionResult LogIn(string returnUrl)
        {
            var model = new LogInModel
            {
                ReturnUrl = returnUrl
            };

            return View(model);
        }

        [HttpPost]
        public ActionResult LogIn(LogInModel model)
        {
            if (!ModelState.IsValid)
            {
                return View();
            }

            // Don't do this in production!
            if (model.Email == "admin@admin.com" && model.Password == "#G16#")
            {
                var identity = new ClaimsIdentity(new[] {
                new Claim(ClaimTypes.Name, "Admin"),
                new Claim(ClaimTypes.Email, "Admin@Admin.com"),
                new Claim(ClaimTypes.Country, "USA")
            },
                    "ApplicationCookie");

                var ctx = Request.GetOwinContext();
                var authManager = ctx.Authentication;

                authManager.SignIn(identity);

                return Redirect(GetRedirectUrl(model.ReturnUrl));
            }

            // user authN failed
            ModelState.AddModelError("", "Invalid email or password");
            return View();
        }

        private string GetRedirectUrl(string returnUrl)
        {
            if (string.IsNullOrEmpty(returnUrl) || !Url.IsLocalUrl(returnUrl))
            {
                return Url.Action("index", "home");
            }

            return returnUrl;
        }

        public ActionResult LogOut()
        {
            var ctx = Request.GetOwinContext();
            var authManager = ctx.Authentication;

            authManager.SignOut("ApplicationCookie");
            return RedirectToAction("index", "home");
        }
    }

查看

    <div class="col-md-4" style="margin-top:200px;">
        @using (Html.BeginForm("LogInModel", "LogIn", FormMethod.Post, new { @class="form-horizontal" }))
        {
            <div class="form-group">
                <label class="label label-default">Email Address</label>
                <div class="form-control">
                    @Html.TextBoxFor(m => m.Email, new { @class = "form-control"})
                </div>
            </div>

            <div class="form-group">
                <label class="label label-default">Password</label>
                <div class="form-control">
                    @Html.TextBoxFor(m => m.Password, new { @class = "form-control" })
                </div>
            </div>

            @Html.HiddenFor(m => m.ReturnUrl)

            <button class="btn btn-default" type="submit">Log In</button>
        }
    </div>

1 个答案:

答案 0 :(得分:2)

您没有正确使用Html.BeginForm

而不是:

Html.BeginForm("LogInModel", "LogIn", FormMethod.Post, new {})

应该是:

Html.BeginForm("LogIn", "Auth", FormMethod.Post, new {})

Correct usage

Html.BeginForm("actionName", "controllerName", FormMethod.Post, new {})