public async Task <IdentityResult> ExternalRegister(ExternalLoginInfo info, ExternalLoginDTO model) { var user = await _userManager.FindByEmailAsync(model.Email); IdentityResult result; if (user != null) { result = await _userManager.AddLoginAsync(user, info); if (result.Succeeded) { await _signInManager.SignInAsync(user, isPersistent : false); } } else { model.Principal = info.Principal; user = _mapper.Map <AppUser>(model); result = await _userManager.CreateAsync(user); if (result.Succeeded) { result = await _userManager.AddLoginAsync(user, info); if (result.Succeeded) { await _signInManager.SignInAsync(user, isPersistent : false); } } } return(result); }
public IHttpActionResult ExternalLogin(ExternalLoginDTO login) { try { if (login.Validacion == Direcciones.PassLoginExterno) { if (login == null) { throw new HttpResponseException(HttpStatusCode.BadRequest); } BLUsuario blusuario = new BLUsuario(); SUsuario usuario = blusuario.externalLogin(new SUsuario() { Email = login.Email }); if (usuario != null) { dynamic res = new ExpandoObject(); res.Token = TokenGenerator.GenerateTokenJwt(login.Email);; res.Email = usuario.Email; res.Rol = usuario.Rol; res.Id = usuario.Id; return(Ok(res)); } throw new ECompartida("Esto no debería haber llegado a este punto"); } throw new ECompartida("La clave de validación no es correcta"); } catch (Exception e) { return(Content(HttpStatusCode.InternalServerError, e.Message)); } }
private async Task AddLogin(ExternalLoginDTO loginDTO, ApplicationUser user) { var userLoginInfo = new UserLoginInfo(loginDTO.LoginProvider, loginDTO.ProviderKey, null); var externalLoginResult = await _userManager.AddLoginAsync(user, userLoginInfo); if (!externalLoginResult.Succeeded) { throw new ApplicationException(DictionaryResources.InvalidRegistrationAttempt); } }
private async Task <ApplicationUser> ExternalRegister(ExternalLoginDTO loginDTO) { var user = _mapper.Map <ApplicationUser>(loginDTO); var result = await _userManager.CreateAsync(user); if (!result.Succeeded) { throw new ApplicationException(DictionaryResources.InvalidRegistrationAttempt); } await AddLogin(loginDTO, user); await CreateNotification(user.Id); return(user); }
public async Task <LoginResultDTO> ExternalLogin(ExternalLoginDTO loginDTO) { if (loginDTO.LoginProvider == DictionaryResources.Facebook) { await ValidateFacebookToken(loginDTO.Token); } else { await ValidateGoogleToken(loginDTO.Token); } var user = _userManager.Users .SingleOrDefault(u => u.Email == loginDTO.Email); if (user == null) { user = await ExternalRegister(loginDTO); } var loggedUser = await _userRepository.GetUserLogin(loginDTO.LoginProvider, user.Id); if (loggedUser == null) { await AddLogin(loginDTO, user); } var result = await _signInManager .ExternalLoginSignInAsync(loginDTO.LoginProvider, loginDTO.ProviderKey, false); if (!result.Succeeded) { throw new ApplicationException(DictionaryResources.InvalidLoginAttempt); } var loginResult = new LoginResultDTO { Token = GenerateJwtToken(user), Id = user.Id }; return(loginResult); }
public async Task <ActionResult> ExternalLogin([FromBody] ExternalLoginDTO loginDTO) { try { var result = await _accountService.ExternalLogin(loginDTO); return(Ok(result)); } catch (InvalidJwtException exception) { return(BadRequest(exception.Message)); } catch (AuthenticationException exception) { return(BadRequest(exception.Message)); } catch (Exception exception) { return(BadRequest(exception.Message)); } }
public ExternalLogin CreateOrUpdate(User user, ExternalLoginDTO externalLogin, string type) { var el = _context.ExternalLogin.SingleOrDefault(x => x.ClientId == externalLogin.Id && x.Type == ExternalLogin.FACEBOOK); if (el == null) { el = _mapper.Map <ExternalLogin>(externalLogin); el.UserId = user.Id; el.Type = type; _context.ExternalLogin.Add(el); } else { el.FirstName = externalLogin.FirstName; el.LastName = externalLogin.LastName; el.FullName = externalLogin.FullName; el.UpdatedDate = DateHelper.GetDate(); _context.ExternalLogin.Update(el); } _context.SaveChanges(); return(el); }
public IEnumerable <ExternalLoginDTO> GetExternalLogins(string returnUrl, bool generateState = false) { IEnumerable <AuthenticationDescription> descriptions = Authentication.GetExternalAuthenticationTypes(); List <ExternalLoginDTO> logins = new List <ExternalLoginDTO>(); string state; if (generateState) { const int strengthInBits = 256; state = RandomOAuthStateGenerator.Generate(strengthInBits); } else { state = null; } foreach (AuthenticationDescription description in descriptions) { ExternalLoginDTO login = new ExternalLoginDTO { Name = description.Caption, Url = Url.Route("ExternalLogin", new { provider = description.AuthenticationType, response_type = "token", client_id = Startup.PublicClientId, redirect_uri = new Uri(Request.RequestUri, returnUrl).AbsoluteUri, state = state }), State = state }; logins.Add(login); } return(logins); }
public async Task <ActionResult> ExternalLogin([FromQuery] ExternalLoginDTO externalLoginDTO) { externalLoginDTO.SecurityKeyHash = CryptographyUtils.Base64Decode(HttpUtility.UrlDecode(externalLoginDTO.SecurityKeyHash)); if (!(await ExternalRedirectUrlExist(externalLoginDTO.returnUrl))) { return(BadRequest("returnUrl no es un valor valido")); } var redirectUrl = Url.Action(nameof(ExternalLoginCallback), "Account", new { externalLoginDTO.returnUrl }); HttpContext.Response.Cookies.Append(ApplicationConstants.KeyHashCookieName, externalLoginDTO.SecurityKeyHash, new Microsoft.AspNetCore.Http.CookieOptions() { HttpOnly = true, MaxAge = TimeSpan.FromMinutes(10), Secure = true, SameSite = Microsoft.AspNetCore.Http.SameSiteMode.Lax }); var properties = signInManager.ConfigureExternalAuthenticationProperties(externalLoginDTO.Provider, redirectUrl, externalLoginDTO.SecurityKeyHash); return(Challenge(properties, externalLoginDTO.Provider)); }