/* * Logs the user into the system. * * Takes username and password in * If the username and password is valid, this generates a token for the employee * This token is stored into the database for validation when using in other methods. */ public WCF_Employee Login(string username, string password) { WCF_Employee wcfEmployee = null; // If login succeeds, fetch the token, otherwise, return null // Validate username and password if (Membership.ValidateUser(username, password)) { // Fetch or generate token var context = new LogicUniversityEntities(); var query = from x in context.employees where x.user_id == username select x; var result = query.ToList(); if (query.Any()) { // Generate a token for the resulting employee. String token = GenerateToken(); // Store token in database var first = result.First(); first.token = token; System.Diagnostics.Debug.WriteLine(context.SaveChanges()); // Pass the token to the service consumer wcfEmployee = new WCF_Employee(first.employee_id, first.employee_name, first.email_id, username, first.department_id, first.supervisor_id, token, Roles.GetRolesForUser(username).FirstOrDefault()); } } // Return the token to user return(wcfEmployee); }
// Logs the user out /* * Doing this will clear the token in the database for the employee, if it exists. */ public string Logout(string token) { var context = new LogicUniversityEntities(); var query = from x in context.employees where x.token == token select x; var result = query.First(); result.token = null; context.SaveChanges(); return("done"); }