MVC登录模式未登录

时间:2018-05-01 16:12:08

标签: asp.net-mvc

我正在使用html表单发布用于登录的表单数据。我把它放在一个模态中但除了重定向到主页之外它绝对没有任何其他功能,并将验证令牌,id和密码附加到url。有什么理由吗?它甚至没有进入帐户控制器

我刚接受mvc项目附带的默认Login类的post请求,并把它放在模态中。

AccountController.cs:

// POST: /Account/Login
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Login(LoginViewModel model, string returnUrl)
{
    if (!ModelState.IsValid)
    {
        return PartialView("Login", model);
    }

    // This doesn't count login failures towards account lockout
    // To enable password failures to trigger account lockout, change to shouldLockout: true
    var result = await SignInManager.PasswordSignInAsync(model.Email, model.Password, model.RememberMe, shouldLockout: false);
    switch (result)
    {
        case SignInStatus.Success:
            return RedirectToLocal(returnUrl);
        case SignInStatus.LockedOut:
            return View("Lockout");
        case SignInStatus.RequiresVerification:
            return RedirectToAction("SendCode", new { ReturnUrl = returnUrl, RememberMe = model.RememberMe });
        case SignInStatus.Failure:
        default:
            ModelState.AddModelError("", "Invalid login attempt.");
            return View(model);
    }
}

Login.cshtml

@using Products.Models
@model LoginViewModel
@{
    ViewBag.Title = "Log in";
}

<head>
    <link rel="stylesheet" href="~/Content/loginmodal.css">
</head>

<!-- Modal -->
<div id="modal" class="modal">
    <div class="modal-content">
        <div class="modal-body">
            <div class="Absolute-Center is-Responsive">
                <div id="logo-container"></div>
                <div class="col-sm-12 col-md-10 col-md-offset-1">
                    <form action="" id="loginForm">
                        @using (Html.BeginForm("Login", "Account", new { ReturnUrl = ViewBag.ReturnUrl }, FormMethod.Post, new { @class = "form-horizontal", role = "form" }))
                        {
                            @Html.AntiForgeryToken()
                            @Html.ValidationSummary(true, "", new { @class = "text-danger" })
                            <div class="form-group input-group">
                                <span class="input-group-addon"><i class="glyphicon glyphicon-user"></i></span>
                                @Html.TextBoxFor(m => m.Email, new { placeholder = "E-mail", @class = "form-control" })
                                @Html.ValidationMessageFor(m => m.Email, "", new { @class = "text-danger" })
                            </div>
                            <div class="form-group input-group">
                                <span class="input-group-addon"><i class="glyphicon glyphicon-lock"></i></span>
                                @Html.PasswordFor(m => m.Password, new { placeholder = "Password", @class = "form-control" })
                                @Html.ValidationMessageFor(m => m.Password, "", new { @class = "text-danger" })
                            </div>
                            <div class="form-group">
                                <input type="submit" value="Log in" class="btn btn-def btn-block" />
                            </div>
                            <div class="form-group text-center">
                                @Html.CheckBoxFor(m => m.RememberMe)
                                @Html.LabelFor(m => m.RememberMe)
                                <a href="#">Forgot Password</a>&nbsp;|&nbsp;<a href="#">Support</a>
                            </div>
                        }
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>

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

LoginViewModel:

public class LoginViewModel
{
    [Required]
    [Display(Name = "Email")]
    [EmailAddress]
    public string Email { get; set; }

    [Required]
    [DataType(DataType.Password)]
    [Display(Name = "Password")]
    public string Password { get; set; }

    [Display(Name = "Remember me?")]
    public bool RememberMe { get; set; }
}

2 个答案:

答案 0 :(得分:2)

在您的代码中,您在以下行中有<form action="" id="loginForm">Html.BeginForm()Html.BeginForm()呈现另一个<form>元素,嵌套在您的案例中 - 这是无效的HTML。浏览器可能会占用您指定<form>的外部action="",这就是它没有进入您的帐户控制器的原因。

答案 1 :(得分:0)

删除此表单

表格行动=&#34;&#34; ID =&#34;登录表单&#34;

在创建2个表单时。

@using(Html.BeginForm ...)动态创建一个表单,它将触及你的actionresult。