Click here to Skip to main content
15,886,724 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:
HTML
<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:
C#
[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:
C#
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.
Posted
Updated 6-Feb-19 0:34am
v2
Comments
CHill60 1-Feb-19 6:43am    
I have reformatted your post to properly format your code snippets, but I have also removed all the CAPITALised words - using all capitals is viewed as shouting on the internet - in other words people think it is rude. Try to avoid it.
Richard Deeming 4-Feb-19 13:38pm    
If it's returning null, that means the matching record doesn't exist in the database.

If you can see it in the database, then most likely you're either looking at the wrong database, or looking in the wrong table.

public async Task<ActionResult> Login(LoginViewModel model, string returnUrl)
    {
        if (!ModelState.IsValid)
        {
            return View(model);
        }

        var user = await UserManager.FindByNameAsync(model.Email);
        if (user != null)
        {
            var getPasswordResult = UserManager.CheckPassword(user, model.Password);

            if (getPasswordResult)
            {
                AuthenticationManager.SignOut(DefaultAuthenticationTypes.ApplicationCookie);

                var identity = await UserManager.CreateIdentityAsync(
                          user, DefaultAuthenticationTypes.ApplicationCookie);
 
Share this answer
 
public async Task<ActionResult> Login(LoginViewModel model, string returnUrl)
    {
        if (!ModelState.IsValid)
        {
            return View(model);
        }

        var user = await UserManager.FindByNameAsync(model.Email);
        if (user != null)
        {
            var getPasswordResult = UserManager.CheckPassword(user, model.Password);

            if (getPasswordResult)
            {
                AuthenticationManager.SignOut(DefaultAuthenticationTypes.ApplicationCookie);

                var identity = await UserManager.CreateIdentityAsync(
                          user, DefaultAuthenticationTypes.ApplicationCookie);

                AuthenticationManager.SignIn(new AuthenticationProperties { IsPersistent = model.RememberMe }, identity);

                if (model.RememberMe == true)
                {
                    HttpCookie cookie = new HttpCookie("UsersLogin");
                    cookie.Values.Add("UserName", model.Email);
                    cookie.Expires = DateTime.Now.AddDays(15);
                    Response.Cookies.Add(cookie);
                }



                return RedirectToAction("NavigateAuthUser", "Home", new { ReturnUrl = returnUrl });
            }
            else
            {
                ModelState.AddModelError("", "Invalid login attempt.");
                return View(model);
            }
        }

        return new EmptyResult();
    }




this is code from my project. hope you can find better solution from this pera.
 
Share this answer
 
Comments
Richard Deeming 6-Feb-19 12:27pm    
Why have you posted this twice?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900