ASP.Net MVC登录尝试无效

时间:2016-07-29 22:52:36

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

使用ASP.Net,C#,MVC我得到

  

登录尝试无效。

当我尝试登录时我注释掉了电子邮件参考,因为我只想使用UserName和Password登录。在调试时我验证了值是否正确,我还检查了表中的值,以确保没有任何隐藏的字符使UserName或Password更长并且签出。

这是我的代码:

AccountViewModels.cs:

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

        //[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; }
    }

Login.cshtml:

<div class="row">
    <div class="col-md-8">
        <section id="loginForm">
            @using (Html.BeginForm("Login", "Account", new { ReturnUrl = ViewBag.ReturnUrl }, FormMethod.Post, new { @class = "form-horizontal", role = "form" }))
            {
                @Html.AntiForgeryToken()
                <h4>Use a local account to log in.</h4>
                <hr />
                @Html.ValidationSummary(true, "", new { @class = "text-danger" })
                @*<div class="form-group">
                    @Html.LabelFor(m => m.Email, new { @class = "col-md-2 control-label" })
                    <div class="col-md-10">
                        @Html.TextBoxFor(m => m.Email, new { @class = "form-control" })
                        @Html.ValidationMessageFor(m => m.Email, "", new { @class = "text-danger" })
                    </div>
                </div>*@
              <div class="form-group">
                @Html.LabelFor(m => m.UserName, new { @class = "col-md-2 control-label" })
                <div class="col-md-10">
                  @Html.TextBoxFor(m => m.UserName, new { @class = "form-control" })
                  @Html.ValidationMessageFor(m => m.UserName, "", new { @class = "text-danger" })
                </div>
              </div>
                <div class="form-group">
                    @Html.LabelFor(m => m.Password, new { @class = "col-md-2 control-label" })
                    <div class="col-md-10">
                        @Html.PasswordFor(m => m.Password, new { @class = "form-control" })
                        @Html.ValidationMessageFor(m => m.Password, "", new { @class = "text-danger" })
                    </div>
                </div>
                <div class="form-group">
                    <div class="col-md-offset-2 col-md-10">
                        <div class="checkbox">
                            @Html.CheckBoxFor(m => m.RememberMe)
                            @Html.LabelFor(m => m.RememberMe)
                        </div>
                    </div>
                </div>
                <div class="form-group">
                    <div class="col-md-offset-2 col-md-10">
                        <input type="submit" value="Log in" class="btn btn-default" />
                    </div>
                </div>
                <p>
                    @Html.ActionLink("Register as a new user", "Register")
                </p>
                @* Enable this once you have account confirmation enabled for password reset functionality
                    <p>
                        @Html.ActionLink("Forgot your password?", "ForgotPassword")
                    </p>*@
            }
        </section>
    </div>
    <div class="col-md-4">
        <section id="socialLoginForm">
            @Html.Partial("_ExternalLoginsListPartial", new ExternalLoginListViewModel { ReturnUrl = ViewBag.ReturnUrl })
        </section>
    </div>
</div>

AccountController.cs:

// POST: /Account/Login
        [HttpPost]
        [AllowAnonymous]
        [ValidateAntiForgeryToken]
        public async Task<ActionResult> Login(LoginViewModel model, string returnUrl)
        {
            if (!ModelState.IsValid)
            {
                return View(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.UserName, 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);
            }
        }

我几乎使用VS提供的模板,并根据我在互联网上找到的内容改变了一切,所以我无法理解什么是错的。好像应该有用......

3 个答案:

答案 0 :(得分:0)

model.UserName可以与两个字段进行比较:Email或UserName。 要定义要使用的字段,请转到App_Start \ IdentityConfig.cs

我认为RequireUniqueEmail是真的(使用电子邮件)。将此更改为false(UserName)可以解决问题。

@Override
public int getItemPosition(Object object) {
    return POSITION_NONE; //In source code, POSITION_NONE = -2 FYI
}

答案 1 :(得分:0)

由于您没有指定注册帐户的方式,因此请查看模板的工作方式 如果您从网站注册,那么您的电子邮件也将是您的用户名,因此当您登录网站并输入电子邮件时,它将成功,因为您必须输入的电子邮件与用户名相同。
但是,如果您没有在网站上注册并指定用户名与电子邮件不同,则必须输入您的用户名。您可以从数据库中查看您的用户名。

答案 2 :(得分:0)

在我的情况下,我在startup.cs文件中将RequireConfirmedAccount = true更改为RequireConfirmedAccount = false。

之前

services.AddDefaultIdentity<IdentityUser>(options => options.SignIn.RequireConfirmedAccount = true)
                .AddEntityFrameworkStores<ApplicationDbContext>();

之后

services.AddDefaultIdentity<IdentityUser>(options => options.SignIn.RequireConfirmedAccount = false)
                .AddEntityFrameworkStores<ApplicationDbContext>();