public UserDTO SaveUser(UserDTO userdto) { try { ValidationResult results = uservalidation.Validate(userdto); if (!results.IsValid) { string failures = string.Empty; //TODO: Use projection and remove the for loop foreach (var failure in results.Errors) { failures += "Property " + failure.PropertyName + " failed validation. Error was: " + failure.ErrorMessage; } throw new InvalidUserException(failures); } else { User user = mapper.Map <User>(userdto); user.UserId = Guid.NewGuid(); user.Salt = EncryptSvc.GetSalt(); user.HashPassword = EncryptSvc.GenerateSaltedHashPassword(user.Salt, userdto.password).Hash; if (oauth.User.Where(x => x.UserName == user.UserName).Count() < 1) { oauth.User.Add(user); oauth.SaveChanges(); } else { throw new DuplicateWaitObjectException(); } userdto = mapper.Map <UserDTO>(user); } } catch (InvalidUserException exUser) { throw new InvalidUserException(exUser.Message); } catch (Exception ex) { Log.Log.Error(ex, TokenConstants.CannotCreateUser); throw new InvalidUserException(TokenConstants.CannotCreateUser); } return(userdto); }
public ClientDTO SaveClient(ClientDTO clientdto) { try { ValidationResult results = clientvalidation.Validate(clientdto); if (!results.IsValid) { string failures = string.Empty; //TODO: Use projection and remove the for loop foreach (var failure in results.Errors) { failures += "Property " + failure.PropertyName + " failed validation. Error was: " + failure.ErrorMessage; } throw new InvalidClientException(failures); } else { Client client = mapper.Map <Client>(clientdto); client.Client_Id = Guid.NewGuid(); client.Client_Secret = EncryptSvc.GetSalt(); if (oauth.Client.Where(x => x.Client_Id == client.Client_Id).Count() < 1) { oauth.Client.Add(client); oauth.SaveChanges(); } else { throw new DuplicateWaitObjectException(); } clientdto = mapper.Map <ClientDTO>(client); } } catch (InvalidClientException) { throw; } catch (Exception ex) { Log.Log.Error(ex, TokenConstants.CannotCreateClient); throw new InvalidClientException(TokenConstants.CannotCreateClient); } return(clientdto); }