public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context) { var userManager = context.OwinContext.GetUserManager <BitUserManager>(); BitUser user = await userManager.FindAsync(context.UserName, context.Password); if (user == null) { context.SetError("invalid_grant", "El nombre de usuario o la contraseña no son correctos."); return; } if (!user.EmailConfirmed) { context.SetError("invalid_grant", "El email no está confirmado"); return; } ClaimsIdentity oAuthIdentity = await user.GenerateUserIdentityAsync(userManager, OAuthDefaults.AuthenticationType); ClaimsIdentity cookiesIdentity = await user.GenerateUserIdentityAsync(userManager, CookieAuthenticationDefaults.AuthenticationType); AuthenticationProperties properties = CreateProperties(user.UserName); AuthenticationTicket ticket = new AuthenticationTicket(oAuthIdentity, properties); context.Validated(ticket); context.Request.Context.Authentication.SignIn(cookiesIdentity); }
public ServiceResult <Usuario> Create(CreateUsuarioDto createUsuario) { ServiceResult <Usuario> result; try { IdentityResult idenityResult = null; Usuario usuario = null; var user = new BitUser { UserName = createUsuario.Email, Email = createUsuario.Email, EmailConfirmed = createUsuario.EmailConfirmed }; //adding roles if (createUsuario.RolesIds.Length > 0) { foreach (var roleId in createUsuario.RolesIds) { user.Roles.Add(new BitUserRole { RoleId = roleId }); } } var securityContext = new BitSecurityContext(); BitUserManager userManager = new BitUserManager(new BitUserStore(securityContext)); using (var scope = new TransactionScope(TransactionScopeOption.Required)) { idenityResult = userManager.Create(user, createUsuario.Password); if (idenityResult.Succeeded) { usuario = new Usuario { Id = user.Id, Email = user.Email }; _usuarioRepository.Insert(usuario); _context.SaveChanges(); } else { result = new ServiceErrorResult <Usuario>(new OperationError(ErrorType.Validation, string.Join(",", idenityResult.Errors))); } scope.Complete(); } result = new ServiceSucceedResult <Usuario>(usuario); } catch (Exception ex) { OperationError error = new OperationError(ErrorType.Exception, "No se realizó la inserción", ex); result = new ServiceErrorResult <Usuario>(ErrorType.Exception, "No se insertó", ex); } return(result); }
public async Task <ActionResult> ExternalLoginConfirmation(ExternalLoginConfirmationViewModel model, string returnUrl) { if (User.Identity.IsAuthenticated) { return(RedirectToAction("Index", "Manage")); } if (ModelState.IsValid) { // Obtener datos del usuario del proveedor de inicio de sesión externo var info = await AuthenticationManager.GetExternalLoginInfoAsync(); if (info == null) { return(View("ExternalLoginFailure")); } var user = new BitUser { UserName = model.Email, Email = model.Email }; var result = await UserManager.CreateAsync(user); if (result.Succeeded) { result = await UserManager.AddLoginAsync(user.Id, info.Login); if (result.Succeeded) { await SignInManager.SignInAsync(user, isPersistent : false, rememberBrowser : false); return(RedirectToLocal(returnUrl)); } } AddErrors(result); } ViewBag.ReturnUrl = returnUrl; return(View(model)); }