I'm writing a project with asp.net core mvc 2.2, my problem is:
During Authentication ".FindByNameAsync()" always returns "NULL" and therefore authentication process is not successful.
My login form contains:
<form asp-controller="Account" asp-action="Login" method="post">
<div asp-validation-summary="All" class="Danger"></div>
<div class="form-group">
<div>
<input asp-for="Mobile" type="text" class="form-control" style="direction:ltr;">
<span asp-validation-for="Mobile" class="text-danger"></span>
</div>
</div>
<div class="form-group">
<div>
<input asp-for="Password" type="password" class="form-control" style="direction:ltr;">
<span asp-validation-for="Password" class="text-danger"></span>
</div>
</div>
<br />
<div class="form-group">
<div>
<input type="submit" class="btn btn-lg btn-primary btn-block" style="text-align: center;" value="LOG IN" />
</div>
</div>
</form>
and my AccountController.cs contains:
[HttpPost]
public async Task<IActionResult> Login(Register register)
{
if (!ModelState.IsValid)
return View();
var user = await _userManager.FindByNameAsync(register.Mobile);
if(user!=null)
{
var result = await _signInManager.PasswordSignInAsync(user, register.Password, false, false);
if(result.Succeeded)
{
return RedirectToAction("Welcome", "Account");
}
}
ModelState.AddModelError("", "User name/password not found");
return View();
}
and my database table is:
public class Register
{
[Key]
[Required]
public int Id { get; set; }
[ForeignKey("IdFK")]
public ICollection<Advertisement> Advertisements { get; set; }
[Required]
public string Mobile { get; set; }
public string Name { get; set; }
public string Family { get; set; }
[Required]
public string Password { get; set; }
[EmailAddress]
public string Mail { get; set; }
}
}
Please help me to get rid of this problem
What I have tried:
I also used register.Name but it did not work!!!
The values were in DB too. I tried with .FindByEmailAsync(register.Mail) too but it did not work.
I used breakpoint on begin of "AccountController.cs" and I saw "register.Mobile" value was "CORRECT" but unfortunately "user" value was "NULL"!!!
I also created a new project and implemented previous project into it, but it didn't work.
Please note that I have Windows 10 and VisualStudio 2017 15.9.5 and VisualStudio 2019 preview 1.1 installed on my computer.