public async Task <ActionResult <DefaultResponse <bool> > > SaveClaim([FromBody] SaveUserClaimViewModel model) { if (!ModelState.IsValid) { NotifyModelStateErrors(); return(Response(false)); } await _userManageAppService.SaveClaim(model); return(Response(true)); }
public async Task <ActionResult <SaveUserClaimViewModel> > SaveClaim(string username, [FromBody] SaveUserClaimViewModel model) { if (!ModelState.IsValid) { NotifyModelStateErrors(); return(ModelStateErrorResponseError()); } model.Username = username; await _userManageAppService.SaveClaim(model); return(ResponsePost(nameof(Claims), new { username }, model)); }
private async Task <UserViewModel> AutoProvisionUserAsync(string provider, string providerUserId, List <Claim> claims) { // create a list of claims that we want to transfer into our store var filtered = new List <Claim>(); // user's display name var name = claims.FirstOrDefault(x => x.Type == JwtClaimTypes.Name)?.Value ?? claims.FirstOrDefault(x => x.Type == ClaimTypes.Name)?.Value; var username = claims.FirstOrDefault(x => x.Type == "user_name" || x.Type == "username")?.Value ?? claims.FirstOrDefault(x => x.Type == ClaimTypes.Sid)?.Value; if (name != null) { filtered.Add(new Claim(JwtClaimTypes.Name, name)); } else { var first = claims.FirstOrDefault(x => x.Type == JwtClaimTypes.GivenName)?.Value ?? claims.FirstOrDefault(x => x.Type == ClaimTypes.GivenName)?.Value; var last = claims.FirstOrDefault(x => x.Type == JwtClaimTypes.FamilyName)?.Value ?? claims.FirstOrDefault(x => x.Type == ClaimTypes.Surname)?.Value; if (first != null && last != null) { filtered.Add(new Claim(JwtClaimTypes.Name, first + " " + last)); } else if (first != null) { filtered.Add(new Claim(JwtClaimTypes.Name, first)); } else if (last != null) { filtered.Add(new Claim(JwtClaimTypes.Name, last)); } } // email var email = claims.FirstOrDefault(x => x.Type == JwtClaimTypes.Email)?.Value ?? claims.FirstOrDefault(x => x.Type == ClaimTypes.Email)?.Value; if (email != null) { filtered.Add(new Claim(JwtClaimTypes.Email, email)); } //picture var picture = claims.FirstOrDefault(x => x.Type == JwtClaimTypes.Picture)?.Value ?? claims.FirstOrDefault(x => x.Type == "image")?.Value; var user = new SocialViewModel() { Username = username ?? email, Name = name, Email = email, Picture = picture, Provider = provider, ProviderId = providerUserId }; var userExist = await _userAppService.CheckUsername(user.Username) || await _userAppService.CheckEmail(user.Email); if (userExist) { await _userAppService.AddLogin(user); } else { await _userAppService.RegisterWithoutPassword(user); } var claimsFromUser = filtered.Select(f => new SaveUserClaimViewModel() { Type = f.Type, Username = user.Username, Value = f.Value }); foreach (var saveUserClaimViewModel in claimsFromUser) { await _userManageAppService.SaveClaim(saveUserClaimViewModel); } return(await _userManageAppService.FindByProviderAsync(provider, providerUserId)); }