ビジネスロジックを分離したい場合はUserStore
とUserManager
を使用できます。私はいつもunity依存性注入が大好きです。
例:
public class UserRepository
{
private readonly UserStore _store;
private readonly UserManager _manager;
public UserRepository(DbContext context)
{
_store = new UserStore<ApplicationUser>(context);
_manager = new UserManager<ApplicationUser>(_store);
}
public async Task<ApplicationUser> GetUserByNameAsync(string username)
{
return await _store.FindByNameAsync(username);
}
public async Task<IEnumerable<ApplicationUser>> GetAllUsersAsync()
{
return await _store.Users.ToArrayAsync();
}
public async Task CreateAsync(ApplicationUser user, string password)
{
await _manager.CreateAsync(user, password);
}
public async Task DeleteAsync(ApplicationUser user)
{
await _manager.DeleteAsync(user);
}
public async Task UpdateAsync(ApplicationUser user)
{
await _manager.UpdateAsync(user);
}
private bool _disposed = false;
public void Dispose()
{
if (!_disposed)
{
_manager.Dispose();
_store.Dispose();
}
_disposed = true;
}
}
私はDIの登録を行ってきた方法ですhttp://tech.trailmax.info/2014/09/aspnet-identity-and-ioc-container-registration/ – trailmax