我正在嘗試使用 UserName 而不是 Email to Login 來完成登錄功能。
我正在使用 Bootstrap Modal 對話框使用 jquery 和 ajax 登錄用戶以進行錯誤處理。
UserName 不是電子郵件地址,因此 ModelState IsValid 始終回傳 false。如果我在 javascrip 檔案中硬編碼一個電子郵件地址,那么一切正常。
無論如何圍繞這個問題。任何幫助將非常感激。
<div class="modal-dialog modal-dialog-centered" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">Login</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body">
<form id="UserLoginForm" asp-controller="UserAuth" asp-action="login">
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<input type="hidden" asp-for="LoginInValid" />
<div class="form-group row mb-3">
<label asp-for="UserName" class="col-md-4 col-form-label"></label>
<div class="col-md-8">
<input asp-for="UserName" placeholder="User Name" class="form-control" />
<span asp-validation-for="UserName" class="text-danger"></span>
</div>
</div>
<div class="form-group row mb-3">
<label asp-for="Password" class="col-md-4 col-form-label"></label>
<div class="col-md-8">
<input asp-for="Password" placeholder="Password" class="form-control" />
<span asp-validation-for="Password" class="text-danger"></span>
</div>
</div>
<div class="form-group-row">
<div class="col-md-8 offset-md-4">
<div class="form-check">
<input asp-for="RememberMe" class="form-check-input" />
<label asp-for="RememberMe" class="col-md-4" col-form-label"></label>
</div>
</div>
</div>
</form>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
<button type="button" id="login" name="login" class="btn btn-primary">Login</button>
</div>
</div>
[AllowAnonymous]
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Login(LoginModel loginModel)
{
loginModel.LoginInValid = "true";
if (ModelState.IsValid)
{
var result = await _signInManager.PasswordSignInAsync(loginModel.UserName, loginModel.Password, loginModel.RememberMe, lockoutOnFailure: false);
if (result.Succeeded)
{
loginModel.LoginInValid = "";
}
else
{
ModelState.AddModelError(string.Empty, "Invalid login attempt");
}
}
return PartialView("_UserLoginPartial", loginModel);
}
不顯示錯誤處理
(function () {
var userloginButton = $("#UserLoginModal button[name='login']").click(onUserLoginClick);
function onUserLoginClick() {
var url = "UserAuth/Login";
var antiForgeryToken = $("#UserLoginModal input[name='__RequestVerificationToken']").val();
var username = $("#UserLoginModal input[name = 'UserName']").val();
var email = "mydomain.com"
/*var email = $("#UserLoginModal input[name = 'Email']").val();*/
var password = $("#UserLoginModal input[name = 'Password']").val();
var rememberMe = $("#UserLoginModal input[name = 'RememberMe']").prop('checked');
var userInput = {
__RequestVerificationToken: antiForgeryToken,
UserName: username,
Email: email,
Password: password,
RememeberMe: rememberMe
};
}
});
登錄模型
public class LoginModel
{
[Required]
[Display(Name = "User Name")]
[StringLength(25, MinimumLength = 2)]
public string UserName { get; set; }
[Required]
[StringLength(100, MinimumLength = 2)]
[EmailAddress]
public string Email { get; set; }
[Required]
[StringLength (100, MinimumLength = 2)]
[DataType(DataType.Password)]
public string Password { get; set; }
public bool RememberMe { get; set; }
public string LoginInValid { get; set; }
}
uj5u.com熱心網友回復:
LoginModel 的 UserName、Email 和 Password 都標記為“必需”,因此除非全部提供,否則 ModelState 將無效。
您需要為每一個提供值(我在您的 HTML 標記中看不到任何電子郵件輸入)或從您不想提供的 LoginModel 屬性中洗掉 [Required] 注釋。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/425437.html
