2

On an ASP.NET Core 1.1 application with ASP.NET Identity I created a user:

PasswordHasher<User> hasher = new PasswordHasher<User>();

User user = new User {
  Email = "john@company.com",
  Username = "john@company.com
};

user.PasswordHash = hasher.HashPassword(user, "johnpass");

context.Users.Add(user);
context.SaveChanges();

The user is created but when I try to sign using the following it fails:

SignInResult result = await _signInManager.PasswordSignInAsync("john@company.com", "johnpass", false, true);     

I then tried to create the user using the UserManager:

PasswordHasher<User> hasher = new PasswordHasher<User>();

User user = new User {
  Email = "john@company.com",
  Username = "john@company.com
};

await userManager.CreateAsync(user, "johnpass");

Now I am able to sign in. It seems the problem is with HashPassword method.

Does anyone knows how to create the user without UserManager?

UPDATE
I tried to create a UserManager without reeling on Injection because I am creating my test data on a Console Application and not on the Web Application:

var userStore = new UserStore(Context);

var userManager = new UserManager<User>(userStore, null, null, null, null, null, null, null, null); 

The user is created but I am still not able to sign in.

Miguel Moura
  • 36,732
  • 85
  • 259
  • 481
  • It's not just the password hash, but also the security stamp. – DavidG Mar 17 '17 at 16:12
  • The security stamp? Can you give me an example how to solve it? – Miguel Moura Mar 17 '17 at 16:14
  • 1
    Well you've already got the answer: use the `UserManager` – DavidG Mar 17 '17 at 16:15
  • That is the point ... I am trying not to use it since I am injecting test data using a Console application so outside of the web application context. And it seems UserManager expects a bunch of classes to be injected in it and on my Console Application I don't have that. – Miguel Moura Mar 17 '17 at 16:18
  • The user manager class doesn't really need that much injecting. It takes a `UserStore` which in itself takes a `DbContext`. Shouldn't be that hard to figure out. – DavidG Mar 17 '17 at 16:28
  • I tried to create a UserManager instance on the console application and not in the web application and I was able to create a user but still not able to sign in ... I added an update. Any idea what am I missing? – Miguel Moura Mar 17 '17 at 16:40
  • Miguel, check this out - works like a charm. https://stackoverflow.com/a/42447607/2708357 – JDR Jul 24 '17 at 12:26

2 Answers2

3

you have to do the following things:

userManager.UpdateSecurityStampAsync(domainUser);
        domainUser.NormalizedUserName = domainUser.NormalizedEmail = domainUser.UserName = domainUser.Email;
        await userManager.UpdateNormalizedUserNameAsync(domainUser);
        await userManager.UpdateNormalizedEmailAsync(domainUser);
        await userManager.UpdateAsync(domainUser);

and if you set up in your config email confirmation

var token = await userManager.GenerateEmailConfirmationTokenAsync(domainUser);
await userManager.ConfirmEmailAsync(domainUser, token);
Przemysław R
  • 107
  • 1
  • 3
0

Follow the following scenario in login.

  1. get the user using userName or Email.
    user = context.Users.FirstOrDefault(usr => usr.UserName == userName);

  2. Check if this user has this password or not using: userManager.CheckPasswordAsync(user, password);

where userManager is an object from UserManager<User>

Matze
  • 5,100
  • 6
  • 46
  • 69