使用ASP.Net,C#,MVC,我得到ASP.Net MVC無效的登錄嘗試
無效的登錄嘗試。
當我嘗試登錄。我註釋掉了電子郵件引用,因爲我只想使用用戶名和密碼登錄。在調試時,我驗證這些值是否正確,並且我還檢查了表中的值,以確保沒有任何使用戶名或密碼更長並且檢出的隱藏字符。
這裏是我的代碼:
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);
}
}
我幾乎使用模板供應d由VS,並根據我在互聯網上找到的東西改變了一切,所以我不明白什麼是錯的。似乎它應該工作......
你打開並激活ssl?一個很好的方式來說明,如果您的網站在默認地址的起始處有https:// – Abob
「AccountController」中'result'的值是什麼?奇怪的是,你是否完全確信憑證*確實是正確的,而不是邏輯錯誤?也許嘗試創建另一個本地帳戶並登錄,因爲這是完全可以確定的? –
在你的答案幫助下,我發現了這個問題。在編寫代碼和測試以及由於註冊時的幾種情況而失敗時,我有一些用戶名不在SQL表中。這些值顯然保存在某個地方,我需要以某種方式刪除它們,所以沒有再發生這種情況。爲了正確回答這個問題,任何人都可以提供解決方案來擺脫這些流氓用戶名? –