public ServerResult <Auth_DB_Context.User> Authenticate(string email, string password) { ServerResult <Auth_DB_Context.User> sr = ServerResult <Auth_DB_Context.User> .create(); try{ sr.result = db.User.Where(x => x.email == email && x.password == password).First(); } catch { sr.error.addMessage(Helper.HttpError.getIdNotExist(TabelList.User, email)); return(sr); } // authentication successful so generate jwt token var tokenHandler = new JwtSecurityTokenHandler(); var key = Encoding.ASCII.GetBytes(_appSettings.Secret); var tokenDescriptor = new SecurityTokenDescriptor { Subject = new ClaimsIdentity(new Claim[] { new Claim(ClaimTypes.Name, sr.result.apiId), new Claim("Role", "Admin"), new Claim(ClaimTypes.Country, "DE") }), Expires = DateTime.UtcNow.AddDays(7), SigningCredentials = new SigningCredentials(new SymmetricSecurityKey(key), SecurityAlgorithms.HmacSha256Signature) }; var token = tokenHandler.CreateToken(tokenDescriptor); sr.result.token = tokenHandler.WriteToken(token); sr.result.WithoutPassword(); return(sr); }
public ServerResult <User> getByEmail(string email, bool withMsg = true) { ServerResult <User> sr = ServerResult <User> .create(); try{ sr.result = db.User.Where(el => el.email == email).First(); } catch { sr.error.addMessage(HttpError.getNoTableEntryForValue(TabelList.User, "email", email), withMsg); sr.fail(); return(sr); }; return(sr); }
public ServerResult <User> getById(string id, bool withMsg = true) { ServerResult <User> sr = ServerResult <User> .create(); try{ sr.result = db.User.Find(id); } catch { sr.error.addMessage(HttpError.getNoTableEntryForValue(TabelList.User, "id", id), withMsg); sr.fail(); return(sr); }; return(sr); }
public ServerResult <Group> passCreateGurd(Group entity, bool withMsg = true) { ServerResult <Group> sr = ServerResult <Group> .create(); sr.result = entity; if (sr.result == null) { sr.error.addMessage(HttpError.getProvideNoEntity(TabelList.Group), withMsg); sr.fail(); return(sr); } return(sr); }
public ServerResult <Group> getByUniqueParams(Group entity, bool withMsg = true) { ServerResult <Group> sr = ServerResult <Group> .create(); if (entity.apiId != null) { return(getById(entity.apiId, withMsg)); } string[] parameter = { "id" }; sr.error.addMessage(Helper.HttpError.getNoUniqueParameter("Group", parameter)); sr.fail(); return(sr); }
public ServerResult <List <Group> > search(SearchModel search, bool withMsg = true) { ServerResult <List <Group> > sr = ServerResult <List <Group> > .create(); sr.result = new List <Group>(); try { foreach (Group group in db.Group.Where(el => el.name.Contains(search.searchString)).ToList()) { sr.result.Add(group); } ; } catch { sr.fail(); sr.error.addMessage(Helper.HttpError.getNoElementFound(TabelList.Group)); } return(sr); }
public ServerResult <Group> getById(string id, bool withMsg = true) { ServerResult <Group> sr = ServerResult <Group> .create(); try{ sr.result = db.Group.Find(id); if (sr.result == null) { sr.error.addMessage(HttpError.getNoTableEntryForValue(TabelList.Group, "id", id), withMsg); sr.fail(); } } catch { sr.error.addMessage(HttpError.getNoTableEntryForValue(TabelList.Group, "id", id), withMsg); sr.fail(); return(sr); }; return(sr); }
public ServerResult <User> passCreateGurd(User entity, bool withMsg = true) { ServerResult <User> sr = ServerResult <User> .create(); sr.result = entity; if (sr.result == null) { sr.error.addMessage(HttpError.getProvideNoEntity(TabelList.User), withMsg); sr.fail(); return(sr); } if (sr.result.email == null) { sr.error.addMessage(HttpError.getFieldsNotProvidedForTable(TabelList.User, "email"), withMsg); sr.fail(); return(sr); } return(sr); }