public async Task<IHttpActionResult> Register(RegisterBindingModel model) { if (!ModelState.IsValid) { Logger.Logger.Instance.LogAction(LoggerHelper.GetActionString(model.Email, "Register failed - Bad Request")); return BadRequest(ModelState); } var user = new ApplicationUser() { UserName = model.Email, Email = model.Email, DomainUser = new DomainUser { FirstName = model.FirstName, LastName = model.LastName, Email = model.Email } }; IdentityResult result = await UserManager.CreateAsync(user, model.Password); if (!result.Succeeded) { Logger.Logger.Instance.LogAction(LoggerHelper.GetActionString(model.Email, "Register failed")); return GetErrorResult(result); } result = await UserManager.AddToRoleAsync(user.Id, "student"); Logger.Logger.Instance.LogAction(LoggerHelper.GetActionString(model.Email, "Registered user")); return Ok(); }
private void SeedUsers() { var passwordHash = new PasswordHasher(); string password = passwordHash.HashPassword("aaa"); var store = new UserStore<ApplicationUser>(_context); var manager = new UserManager<ApplicationUser>(store); var user1 = new ApplicationUser { UserName = "******", PasswordHash = password, DomainUser = new DomainUser { FirstName = "Ana", LastName = "Pop", MatriculationNumber = "AnPo01", Email = "*****@*****.**" } }; manager.Create(user1); manager.AddToRole(user1.Id, "Student"); var user2 = new ApplicationUser { UserName = "******", PasswordHash = password, DomainUser = new DomainUser { FirstName = "Marius", LastName = "Muresan", MatriculationNumber = "MaMu01", Email = "*****@*****.**", } }; manager.Create(user2); manager.AddToRole(user2.Id, "Student"); var user3 = new ApplicationUser { UserName = "******", PasswordHash = password, DomainUser = new DomainUser { FirstName = "Adela", LastName = "Berindean", MatriculationNumber = "AdBe01", Email = "*****@*****.**", } }; manager.Create(user3); manager.AddToRole(user3.Id, "Student"); var user4 = new ApplicationUser { UserName = "******", PasswordHash = password, DomainUser = new DomainUser { FirstName = "Adina", LastName = "Duma", MatriculationNumber = "AdDu01", Email = "*****@*****.**", } }; manager.Create(user4); manager.AddToRole(user4.Id, "Teacher"); var user7 = new ApplicationUser { UserName = "******", PasswordHash = password, DomainUser = new DomainUser { FirstName = "Adrian", LastName = "Achim", MatriculationNumber = "AdAc01", Email = "*****@*****.**", } }; manager.Create(user7); manager.AddToRole(user7.Id, "Teacher"); var user5 = new ApplicationUser { UserName = "******", PasswordHash = password, DomainUser = new DomainUser { FirstName = "Iulia", LastName = "Duca", MatriculationNumber = "IuDu01", Email = "*****@*****.**", } }; manager.Create(user5); manager.AddToRole(user5.Id, "Admin"); _context.SaveChanges(); }