public UserViewModel(User user) { Email = user.Email; TrainingLanguage = user.TrainingLanguageId; MainLanguage = user.MainLanguageId; AdditionalLanguages = user.AdditionalLanguages.Select(x => x.LanguageId).ToList(); }
private string GetToken(User user, DateTime? expires) { var handler = new JwtSecurityTokenHandler(); ClaimsIdentity identity = new ClaimsIdentity(new GenericIdentity(user.Email, "TokenAuth"), new[] { new Claim("http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier", user.Id.ToString()) }); var securityToken = handler.CreateToken(new SecurityTokenDescriptor { Issuer = _tokenOptions.Issuer, Audience = _tokenOptions.Audience, SigningCredentials = _tokenOptions.SigningCredentials, Subject = identity, Expires = expires }); return handler.WriteToken(securityToken); }
public async Task<IActionResult> Create([FromBody]CreateUserViewModel requestModel) { if (requestModel.MainLanguage == requestModel.TrainingLanguage) { return Error("You should select unique languages"); } var user = new User { Email = requestModel.Email, MainLanguageId = requestModel.MainLanguage, TrainingLanguageId = requestModel.TrainingLanguage }; if (requestModel.AdditionalLanguages != null) { if (requestModel.AdditionalLanguages.Any(x => x == requestModel.MainLanguage) || requestModel.AdditionalLanguages.Any(x => x == requestModel.TrainingLanguage) || requestModel.AdditionalLanguages.Count != requestModel.AdditionalLanguages.Distinct().Count()) { return Error("You should select unique languages"); } foreach (var additionalLanguageId in requestModel.AdditionalLanguages) { user.AdditionalLanguages.Add(new UserAdditionalLanguage {UserId = user.Id, LanguageId = additionalLanguageId }); } } IdentityResult result; try { result = await UserManager.CreateAsync(user, requestModel.Password); } catch (Exception) { return Error("Language not found"); } if (!result.Succeeded) { return Error(result.Errors.Select(x => x.Description)); } return Created("/api/user/" + user.Id); }