private async Task <CustomerUserResult> CreateCustomerUser(RegisterViewModel model) { bool success = false; var user = new ApplicationUser { UserName = model.Nick, Email = model.Email }; var result = await UserManager.CreateAsync(user, model.Password); if (result.Succeeded) { var usuario = await _db.Usuarios.FirstOrDefaultAsync(u => u.Nick == model.Nick); if (usuario == null) { _db.Usuarios.Add(new Usuarios { Contraseña = model.Password, DNI = model.DNI, Nick = model.Nick, Nombre = model.Name, Rol = "C" }); _db.Clientes.Add(new Clientes { Nombre = model.Name, Apellidos = model.Surname, DNI_Cliente = model.DNI, Direccion = model.Address, Fecha_Nacimiento = model.DateBorn, Poblacion = model.City, Telefono = model.Phone, Email = model.Email, Nick = model.Nick, Estado = EstadoCliente.Activo.ToString(), Foto = "koala.png" }); await _db.SaveChangesAsync(); } var manager = new IdentityManager(); await manager.CreateRoleAsync(KoalaRoles.UserCliente); await manager.AddUserToRoleAsync(user.Id, KoalaRoles.UserCliente); // For more information on how to enable account confirmation and password reset please visit http://go.microsoft.com/fwlink/?LinkID=320771 // Send an email with this link // string code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id); // var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme); // await UserManager.SendEmailAsync(user.Id, "Confirm your account", "Please confirm your account by clicking <a href=\"" + callbackUrl + "\">here</a>"); success = true; } return(new CustomerUserResult { Success = success, Result = result }); }
private async Task CreateAdminUser() { var user = new ApplicationUser { UserName = "******", Email = "*****@*****.**" }; var manager = new IdentityManager(); await manager.CreateRoleAsync(KoalaRoles.UserAdmin); bool success = await manager.CreateUserAsync(user, "123456"); if (success) { await manager.AddUserToRoleAsync(user.Id, KoalaRoles.UserAdmin); } }
public async static void ConfigureRoles() { var identityManager = new IdentityManager(); const string superAdminRole = "SuperAdmin"; const string adminUser = "******"; var roles = new List<string>() {superAdminRole, "Admin", "Moderator", "Member", "Guest"}; foreach (var role in roles) { if (!identityManager.RoleExists(role)) { identityManager.CreateRole(role); } } if (!await identityManager.UserExistsAsync(adminUser)) { await identityManager.CreateUserAsync(new ApplicationUser() { UserName = adminUser }, "jk/2eRb296#{1=m"); } if (!identityManager.IsUserInRole(identityManager.GetUserIdByName(adminUser), superAdminRole)) await identityManager.AddUserToRoleAsync(await identityManager.GetUserIdByNameAsync(adminUser), superAdminRole); if (!identityManager.IsUserInRole(identityManager.GetUserIdByName(adminUser), "Admin")) await identityManager.AddUserToRoleAsync(await identityManager.GetUserIdByNameAsync(adminUser), "Admin"); }
private async Task <AdminUserResult> CreateAdminUser(RegisterAdminViewModel model) { bool success = false; var user = new ApplicationUser { UserName = model.Nick, Email = model.Email }; var result = await UserManager.CreateAsync(user, model.Password); if (result.Succeeded) { var usuario = await _db.Usuarios.FirstOrDefaultAsync(u => u.Nick == model.Nick); if (usuario == null) { _db.Usuarios.Add(new Usuarios { Contraseña = model.Password, DNI = model.DNI, Nick = model.Nick, Nombre = model.Name, Rol = "A" }); _db.Administradores.Add(new Administradores { Nombre = model.Name, Apellidos = model.Surname, DNI_Admin = model.DNI, Email = model.Email, Nick = model.Nick, Foto = "koala.png" }); try { await _db.SaveChangesAsync(); } catch (System.Data.Entity.Validation.DbEntityValidationException e) { string msg = Helpers.Util.ValidationErrors(e); throw; } } var manager = new IdentityManager(); await manager.CreateRoleAsync(KoalaRoles.UserAdmin); await manager.AddUserToRoleAsync(user.Id, KoalaRoles.UserAdmin); // For more information on how to enable account confirmation and password reset please visit http://go.microsoft.com/fwlink/?LinkID=320771 // Send an email with this link // string code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id); // var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme); // await UserManager.SendEmailAsync(user.Id, "Confirm your account", "Please confirm your account by clicking <a href=\"" + callbackUrl + "\">here</a>"); success = true; } return(new AdminUserResult { Success = success, Result = result }); }