Click here to Skip to main content
15,878,970 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
am working with Microsoft.AspNetCore.Identity.UserManager and I'm trying to mock the creation of a new user. In fact, it does create a new user with username, email etc. but the password hash property is still null.

This is how I set up mock usermanager with some additional setup:

C#
var store = new Mock>();

var validator = new UserValidator();
var passValidator = new PasswordValidator();

var mgr = new Mock>(store.Object, null, null, null, null, null, null, null, null);
mgr.Object.UserValidators.Add(validator);
mgr.Object.PasswordValidators.Add(passValidator);
mgr.Object.PasswordHasher = new PasswordHasher();
mgr.Object.Options = AuthenticationRules.DefaultAuthenticationRules();

List users= new List();

mgr.Setup(x => x.DeleteAsync(It.IsAny())).ReturnsAsync(IdentityResult.Success);
mgr.Setup(x => x.CreateAsync(It.IsAny(), It.IsAny())).ReturnsAsync(IdentityResult.Success).Callback((x, y) => users.Add(x));
mgr.Setup(x => x.UpdateAsync(It.IsAny())).ReturnsAsync(IdentityResult.Success);


The 'DefaultAuthenticationRules' returns this:

C#
 public static IdentityOptions DefaultAuthenticationRules(
IdentityOptions identityOptions = null)
    {
        if(identityOptions == null)
            identityOptions = new IdentityOptions();

        identityOptions.User.RequireUniqueEmail = true;
        identityOptions.Password.RequireNonAlphanumeric = false;
        identityOptions.Password.RequiredUniqueChars = 0;

        return identityOptions;
    }


I am then passing the mgr.Object to a method that handles the creation of the new user where 'Object' is _userManager

C#
var creationResult = await _userManager.CreateAsync(_user, _registrationModel.Password);

if (!creationResult.Succeeded)
    return false;

return true;


_registrationModel.Password IS populated.

So now when the setup adding a new user in the callback to the list of users the user is populated without password hash. I am not exactly sure what I'm missing here. I'm missing something in mgr.Setup?

What I have tried:

I have tried to create identity options and create a setup for it and then add it as parameter like so:

C#
var options = new Mock>();
var idOptions = AuthenticationRules.DefaultAuthenticationRules();
options.Setup(o => o.Value).Returns(idOptions);

var mgr = new Mock>(store.Object, options.Object, null, null, null, null, null, null, null);


But passwordhash property is still null for the user.

Thanks in advance.

Thanks in advance
Posted
Updated 19-Apr-20 20:37pm

1 solution

Are you under the premise the PasswordHasher should be automatically called ?

I did some research and wasn't convinced - I would have thought you'd need
ApplicationUser user = _userManager.Users...;
user.PasswordHash = _userManager.PasswordHasher.HashPassword(user, _registrationModel.Password);

somewhere in there
 
Share this answer
 

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