yogeshrnaik / programming-problems

Solutions to different Programming Problems
0 stars 1 forks source link

".FindByNameAsync()" ALWAYS RETURNS "NULL" AND THEREFORE AUTHENTICATION PROCEES IS NOT SUCCESSFUL #1

Open SohJel opened 5 years ago

SohJel commented 5 years ago

I'm writing a project with asp.net core mvc 2.2, my problem is: During authentication ".FindByNameAsync()" alyays aeturns "NULL" and therefore authentication process is not successful. My LOGIN form contains:


And my AccountController.cs contains:

[HttpPost] public async Task 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.

SohJel commented 5 years ago

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.