public bool agregarRolUsuario(sp_ObtenerRolesUser_Result rolUser) { try { using (context = new BDContext()) { context.sp_agregarRolUser( rolUser.rolId, rolUser.userId, rolUser.Usuario ); context.SaveChanges(); } return(true); } catch (Exception) { return(false); } }
public async Task <ActionResult> Register(RegisterViewModel model) { if (ModelState.IsValid) { BDContext context = new BDContext(); var user = new ApplicationUser { UserName = model.Email, Email = model.Email }; var result = await UserManager.CreateAsync(user, model.Password); if (result.Succeeded) { usuarios usuario = new usuarios() { Usuario_ID = user.Id, nombre = model.Name, apellidos = model.FirstLastName + ' ' + model.SecondLastName, correoElectronico = model.Email }; UnidadDeTrabajo <usuarios> unidad = new UnidadDeTrabajo <usuarios>(context); unidad.genericDAL.Add(usuario); unidad.Complete(); var usuario_BD = context.usuarios.Where(u => u.Usuario_ID.Equals(usuario.Usuario_ID)).Single(); // descomentar si se quiere agregar un usuario con privilegios de admin var roleStore = new RoleStore <IdentityRole>(new ApplicationDbContext()); var roleManager = new RoleManager <IdentityRole>(roleStore); await roleManager.CreateAsync(new IdentityRole("Admin")); await UserManager.AddToRoleAsync(user.Id, "Admin"); context.sp_agregarRolUser(1, usuario_BD.userId, usuario_BD.id_estado); /* * var roleStore = new RoleStore<IdentityRole>(new ApplicationDbContext()); * var roleManager = new RoleManager<IdentityRole>(roleStore); * await roleManager.CreateAsync(new IdentityRole("Usuario")); * await UserManager.AddToRoleAsync(user.Id, "Usuario"); * context.sp_agregarRolUser(2, usuario_BD.userId, usuario_BD.id_estado); */ context.SaveChanges(); await SignInManager.SignInAsync(user, isPersistent : false, rememberBrowser : false); // For more information on how to enable account confirmation and password reset please visit https://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>"); return(RedirectToAction("Index", "Home")); } AddErrors(result); } // If we got this far, something failed, redisplay form return(View(model)); }