/// <summary> /// Creates a login with the given parameters /// </summary> /// <param name="login"></param> public int CreateLogin(LoginDTO login) { try { using (var db = new ReportAppLoginEntities()) { db.Login.Add(new Login() { Username = login.Username, Password = login.Password }); db.SaveChanges(); } return (int)codes.success; } catch(DbUpdateException e){ return (int)codes.user_exists; } catch(Exception e){ return (int)codes.fail; } }
/// <summary> /// Return the Login with the given username /// </summary> /// <param name="username"></param> /// <returns>A LoginDTO</returns> public LoginDTO GetUserByUsername(string username, string password) { try { using (var db = new ReportAppLoginEntities()) { var login = db.Login.FirstOrDefault(x => x.Username.Equals(username)); if (login == null) { return new LoginDTO() { Code = (int)codes.no_user, Token = null, ID = 0, Username = null, LoginDate = DateTime.Now, AccesPath = null, }; } else if (login.Password != password) { return new LoginDTO() { Code = (int)codes.wrong_password, Token = null, ID = 0, Username = login.Username, LoginDate = DateTime.Now, AccesPath = null, }; } else { string token = null; while (token == null || token.Contains("/")) { token = Convert.ToBase64String(Guid.NewGuid().ToByteArray()); } var loginCred = new LoginDTO() { Code = (int)codes.success, Token = token, ID = login.ID, Username = login.Username, LoginDate = DateTime.Now, AccesPath = login.AccesPath, }; dic.Add(token, loginCred); return loginCred; } //return new LoginDTO() //{ // ID = login.ID, // Username = login.Username, // Password = login.Password, // Code = (int)codes.success //}; } } catch (Exception e) { return new LoginDTO() { Code = (int)codes.fail }; } }