public async Task <bool> Delete(int companyId, string username) { //-- Connects to the database using (var entity = new CompanyBrokerAccountEntities()) { //-- fetches an account based on the informations var account = entity.CompanyAccounts.Where(a => a.CompanyId == companyId && a.Username == username).Single <CompanyAccount>(); //-- checks the account if (account != null) { //-- removes the account entity.CompanyAccounts.Remove(account); //-- informs of the state entity.Entry(account).State = EntityState.Deleted; //-- saves the state await entity.SaveChangesAsync(); //-- return return(true); } else { return(false); } } }
public async Task <bool> CreateAccount(AccountRequest accountRequest) { bool resultProcess = false; //-- generating the salt var salt = GenerateSalt(32); if (accountRequest != null) { //-- Creates the new account var user = new CompanyAccount { CompanyId = accountRequest.CompanyId, Username = accountRequest.Username, Email = accountRequest.Email, PasswordSalt = salt, PasswordHash = GetHash(accountRequest.Password, salt), Active = accountRequest.Active }; //-- Connects to the database using the entity using (var entitys = new CompanyBrokerAccountEntities()) { //-- adds a new user to the CompanyAccounts table entitys.CompanyAccounts.Add(user); //-- Saves the changes to the database await entitys.SaveChangesAsync(); resultProcess = true; } } //-- Returns the user wished to be created return(resultProcess); }
public async Task <IList <AccountResponse> > Get() { //-- Uses the CompanyBrokeraccountEntity to access the database using (var entitys = new CompanyBrokerAccountEntities()) { return((await entitys.CompanyAccounts.ToListAsync()).Select(a => new AccountResponse(a)).ToList()); } }
public async Task <IList <AccountResponse> > Get(int companyId) { //-- Uses the CompanyBrokeraccountEntity to access the database using (var entitys = new CompanyBrokerAccountEntities()) { //-- Fetches the account list return((await entitys.CompanyAccounts.ToListAsync()).Select(a => new AccountResponse(a)).Where(c => c.CompanyId == companyId).ToList()); } }
public async Task <AccountResponse> VerifyLogin(string Username, string Password) { if (!string.IsNullOrEmpty(Username) && !string.IsNullOrEmpty(Password)) { //-- Deserialize the JSon string var _usernameJS = JsonConvert.DeserializeObject <string>(Username); var _passwordJS = JsonConvert.DeserializeObject <string>(Password); //-- Decode the encoding base64 string var _username = ASCIIEncoding.ASCII.GetString(Convert.FromBase64String(_usernameJS)); var _password = ASCIIEncoding.ASCII.GetString(Convert.FromBase64String(_passwordJS)); //-- Uses the account entities to log on the database using (var entitys = new CompanyBrokerAccountEntities()) { //-- fetches the user var user = await entitys.CompanyAccounts.Where(a => a.Username == _username).SingleOrDefaultAsync(); //-- checks the response of it exists if (user != null) { //-- sets the results depending on the password matching var loginResult = GetHash(_password, user.PasswordSalt).SequenceEqual(user.PasswordHash); if (loginResult != false) { return(new AccountResponse(user)); } else { return(null); } } else { return(null); } } } else { return(null); } }
public async Task <AccountResponse> Get(string username) { //-- Uses the CompanyBrokeraccountEntity to access the database using (var entitys = new CompanyBrokerAccountEntities()) { //-- Fetches the account list var responseData = await entitys.CompanyAccounts.FirstOrDefaultAsync(a => a.Username == username); //-- Returns the results if (responseData != null) { return(new AccountResponse(responseData)); } else { return(null); } } }
public async Task <AccountResponse> Get(string userName, int companyId) { //-- Uses the CompanyBrokeraccountEntity to access the database using (var entitys = new CompanyBrokerAccountEntities()) { //-- fetches the account based on the ID the users has requested var responseData = await entitys.CompanyAccounts.FirstOrDefaultAsync(c => c.Username == userName && c.CompanyId == companyId); //-- Creates a new accountResponse and parsing the information to remove sensitive data //-- Returns the results if (responseData != null) { return(new AccountResponse(responseData)); } else { return(null); } } }
public async Task <bool> PUT(AccountRequest AccountAPIModel) { //-- Getting access to the using (var entity = new CompanyBrokerAccountEntities()) { //-- fetches the account var responsData = entity.CompanyAccounts.Where(a => a.CompanyId == AccountAPIModel.CompanyId && a.Username == AccountAPIModel.Username).Single <CompanyAccount>(); //-- checks the account if (responsData != null) { //-- sets the new informations responsData.Email = AccountAPIModel.Email; responsData.Active = AccountAPIModel.Active; responsData.Username = AccountAPIModel.Username; //-- checks if password has been changed if (!string.IsNullOrEmpty(AccountAPIModel.Password)) { //-- creates new salt var newSalt = GenerateSalt(32); //-- sets new password informations responsData.PasswordHash = GetHash(AccountAPIModel.Password, newSalt); responsData.PasswordSalt = newSalt; } //-- Sets the data entry and sate entity.Entry(responsData).State = EntityState.Modified; //-- saves the data await entity.SaveChangesAsync(); //-- returns return(true); } else { return(false); } } }