/// <summary> /// Authenticate via a token. /// </summary> /// <param name="token">The token to authenticate.</param> /// <returns>authentication status success/failure</returns> public static bool Authenticate(string token) { InventoryDatabaseEntities db = new InventoryDatabaseEntities(); byte[] data = Convert.FromBase64String(token); string decoded = Encoding.UTF8.GetString(data); string[] userPass = decoded.Split(':'); string username = userPass[0]; Models.User user = db.Users.Where(u => u.userName == username).First(); Debug.WriteLine(username + " : " + userPass[1]); if (user.authDate.HasValue && (DateTime.Now - user.authDate.Value).TotalMinutes < 30) { if (userPass[1] == user.authToken) { user.authDate = DateTime.Now; db.SaveChanges(); return true; } } return false; }
public static string generateToken(string username) { InventoryDatabaseEntities db = new InventoryDatabaseEntities(); Models.User user = db.Users.Where(u => u.userName == username).First(); string token = Convert.ToBase64String(Guid.NewGuid().ToByteArray()); user.authToken = token; user.authDate = DateTime.Now; db.SaveChanges(); return token; }