在domain中创建一个manager,继承UserManager
实现创建用户方法。
public class NonWasteCityUserManager: UserManager<IdentityUser>, IDomainService
{
public NonWasteCityUserManager(IUserStore<IdentityUser> store,
IOptions<IdentityOptions> optionsAccessor,
IPasswordHasher<IdentityUser> passwordHasher,
IEnumerable<IUserValidator<IdentityUser>> userValidators,
IEnumerable<IPasswordValidator<IdentityUser>> passwordValidators,
ILookupNormalizer keyNormalizer, IdentityErrorDescriber errors,
IServiceProvider services, ILogger<UserManager<IdentityUser>> logger)
: base(store, optionsAccessor, passwordHasher, userValidators, passwordValidators,
keyNormalizer, errors, services, logger)
{
}
/// <summary>
/// Creates the specified <paramref name="user"/> in the backing store with given password,
/// as an asynchronous operation.
/// </summary>
/// <param name="user">The user to create.</param>
/// <param name="password">The password for the user to hash and store.</param>
/// <param name="validatePassword">to validate Password for the user in the store.</param>
/// <returns>
/// The <see cref="Task"/> that represents the asynchronous operation, containing the <see cref="IdentityResult"/>
/// of the operation.
/// </returns>
public virtual async Task<IdentityResult> CreateAsync(IdentityUser user, string password, bool validatePassword = false)
{
ThrowIfDisposed();
if (user == null)
{
throw new ArgumentNullException(nameof(user));
}
if (string.IsNullOrEmpty(password))
{
throw new ArgumentNullException(nameof(password));
}
var result = await UpdatePasswordHash(user, password,validatePassword);
if (!result.Succeeded)
{
return result;
}
return await CreateAsync(user);
}
private async Task<IdentityResult> CreateAsync(IdentityUser user)
{
this.ThrowIfDisposed();
// await UpdateSecurityStampInternal(user);
// IdentityResult async = await ValidateUserAsync(user);
// if (!async.Succeeded)
// return async;
// if (this.Options.Lockout.AllowedForNewUsers && this.SupportsUserLockout)
// await this.GetUserLockoutStore().SetLockoutEnabledAsync(user, true, this.CancellationToken);
await SetLockoutEnabledAsync(user, true);
await UpdateNormalizedUserNameAsync(user);
await UpdateNormalizedEmailAsync(user);
return await Store.CreateAsync(user, this.CancellationToken);
}
}