private Token GenerateToken(string ipAddress) { const int numGuids = 2; var guidLength = Guid.Empty.ToByteArray().Length; var bytes = new byte[numGuids * guidLength]; for (int i = 0; i < numGuids; i++) { Guid.NewGuid().ToByteArray().CopyTo(bytes, i * guidLength); } var tokenValue = Convert.ToBase64String(bytes); var token = default(Token); using (var context = new LiberryContext()) { // Remove old tokens assigned to this IP Address var existingTokens = context.Tokens.Where(obj => obj.IpAddress == ipAddress); context.Tokens.RemoveRange(existingTokens); token = new Token { TokenValue = tokenValue, IpAddress = ipAddress, Expires = DateTime.Now.AddMinutes(_tokenLife) }; context.Tokens.Add(token); context.SaveChanges(); } return(token); }
public bool ValidateToken(string token, string ipAddress) { using (var context = new LiberryContext()) { var existingEntry = context.Tokens.FirstOrDefault(obj => obj.TokenValue == token && obj.IpAddress == ipAddress); return(existingEntry != null && existingEntry.Expires > DateTime.Now); } }
public List <SectionDTO> GetAllSections() { using (var context = new LiberryContext()) { var query = from section in context.Sections select new SectionDTO { SectionId = section.SectionId, Name = section.Name }; return(query.ToList()); } }
// TODO: Remove this public List <TokenTempDTO> GetAllTokens() { using (var context = new LiberryContext()) { var dtos = from token in context.Tokens select new TokenTempDTO { TokenId = token.TokenId, TokenValue = token.TokenValue, IpAddress = token.IpAddress, Expires = token.Expires }; return(dtos.ToList()); } }