public async Task <IHttpActionResult> RegisterExternal() { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var info = await Authentication.GetExternalLoginInfoAsync(); if (info == null) { return(InternalServerError()); } if (!_administrationService.UserEmailExists(info.Email)) { if (_administrationService.UserIsSoftDeleted(info.Email)) { _administrationService.RestoreUser(info.Email); } else { var requestedOrganization = RequestedOrganization; var result = await _administrationService.CreateNewUserWithExternalLogin(info, requestedOrganization); if (!result.Succeeded) { return(GetErrorResult(result)); } } } else if (_administrationService.HasExistingExternalLogin(info.Email, info.Login.LoginProvider)) { await _administrationService.AddProviderImage(_userManager.FindByEmail(info.Email).Id, info.ExternalIdentity); return(Ok("User already exists")); } else if (_administrationService.HasExistingExternalLogin(info.Email, AuthenticationConstants.InternalLoginProvider)) { var user = await _userManager.FindByEmailAsync(info.Email); if (user != null && !user.EmailConfirmed) { await _userManager.RemoveLoginAsync(user.Id, new UserLoginInfo(AuthenticationConstants.InternalLoginProvider, user.Id)); await _userManager.RemovePasswordAsync(user.Id); } } var userId = _userManager.FindByEmail(info.Email).Id; await _userManager.AddLoginAsync(userId, info.Login); await _administrationService.AddProviderImage(userId, info.ExternalIdentity); _administrationService.AddProviderEmail(userId, info.Login.LoginProvider, info.Email); return(Ok()); }
public void RestoreUser(string email) { var shroomsContext = _context as ShroomsDbContext; if (shroomsContext == null) { throw new ArgumentNullException(nameof(shroomsContext)); } shroomsContext .Database .ExecuteSqlCommand("UPDATE [dbo].[AspNetUsers] SET[IsDeleted] = '0' WHERE Email = @email", new SqlParameter("@email", email)); var user = _userManager.FindByEmail(email); AddNewUserRoles(user.Id); }