private async Task <TenantUser> UpdateUser(TenantUserEntity user, string method) { var result = await _userManager.UpdateAsync(user); if (result.Succeeded) { return(_mapper.Map <TenantUser>(user)); } else { throw new RepositoryException(method, result.Errors.Select(x => x.Code)); } }
public async Task <IActionResult> ExternalSignIn( [FromRoute] string provider, [FromServices] IOpenIdRequest openIdRequest, [FromServices] IRedirectRequest redirectRequest) { string returnUrl = await redirectRequest.GetReturnUrl(); TenantUserEntity tenantUser = await openIdRequest.RegisterUser(); await signInManager.SignInAsync(tenantUser, true, nameof(ExternalSignIn)); ClaimsPrincipal principal = await signInManager.CreateUserPrincipalAsync(tenantUser); await signInManager.SignInAsync(tenantUser, false, nameof(ExternalSignIn)); await HttpContext.SignInAsync(principal); await eventService.RaiseAsync(new UserLoginSuccessEvent(tenantUser.UserName, tenantUser.Id, tenantUser.UserName)); return(RedirectToAction("Index", "Home")); }
public async Task <TenantUserEntity> RegisterUser() { TenantUserEntity user = userManager.Users .Where(d => d.Email == Email) .Where(d => d.IdentitySource == IdentitySource) .Where(d => d.IdentityProvider == IdentityProvider) .FirstOrDefault(); if (user != null) { tenantUser = user; return(tenantUser); } tenantUser = new TenantUserEntity(); tenantUser.Id = Guid.NewGuid().ToString(); tenantUser.Email = Email; tenantUser.UserName = Email; tenantUser.ExternalUserId = ExternalUserId; tenantUser.IdentitySource = IdentitySource; tenantUser.IdentityProvider = IdentityProvider; IdentityResult userResult = await userManager.CreateAsync(tenantUser); if (!userResult.Succeeded) { throw new Exception(string.Join(" ", userResult.Errors)); } IdentityResult claimsResult = await userManager.AddClaimsAsync(tenantUser, claims); if (!userResult.Succeeded) { throw new Exception(string.Join(" ", userResult.Errors)); } return(tenantUser); }
public async Task <TenantUser> CreateUser(CreateUserData data) { var tenantUser = new TenantUserEntity { Email = data.Email, UserName = data.UserGuid.ToString(), DisplayName = data.DisplayName, UserGuid = data.UserGuid, ImageUrl = data.ImageUrl, TenantId = data.TenantId, SelectedAvatarType = data.AvatarType }; var result = await _userManager.CreateAsync(tenantUser, data.Password); if (!result.Succeeded) { throw new RepositoryException(nameof(CreateUser), result.Errors.Select(x => x.Code)); } return(_mapper.Map <TenantUser>(tenantUser)); }