public static Hashtable RequestKey(string Description) { System.Collections.Hashtable ht = new System.Collections.Hashtable(); HttpContext context = HttpContext.Current; if (context.User.Identity.IsAuthenticated) { try { vwar.service.host.APIKeyManager keyMan = new vwar.service.host.APIKeyManager(); vwar.service.host.APIKey key = keyMan.CreateKey(context.User.Identity.Name, context.Server.HtmlEncode(Description)); ht["Key"] = key.Key; ht["Usage"] = key.Usage; ht["Active"] = (key.State == vwar.service.host.APIKeyState.ACTIVE) ? "Yes" : "No"; ht["Message"] = System.Configuration.ConfigurationManager.AppSettings["ProfilePage_KeyRequestSuccess"]; } catch { ht["Message"] = System.Configuration.ConfigurationManager.AppSettings["ProfilePage_KeyRequestError"]; } } else { ht["Message"] = System.Configuration.ConfigurationManager.AppSettings["ProfilePage_KeyRequestError"]; } return(ht); }
//Get all keys registered to a user public List <APIKey> GetKeysByUser(string email) { CheckConnection(); List <APIKey> results = new List <APIKey>(); using (var command = mConnection.CreateCommand()) { command.CommandText = "{CALL GetByUser(?)}"; command.CommandType = System.Data.CommandType.StoredProcedure; command.Parameters.AddWithValue("newEmail", email); using (var resultSet = command.ExecuteReader()) { while (resultSet.Read()) { APIKey newkey = new APIKey(); newkey.Email = resultSet["Email"].ToString(); newkey.Key = resultSet["KeyText"].ToString(); newkey.Usage = resultSet["UsageText"].ToString(); newkey.State = (APIKeyState)(System.Convert.ToInt16(resultSet["State"].ToString())); results.Add(newkey); } } } return(results); }
//Get the user a key is registered to public APIKey GetKeyByKey(string key) { CheckConnection(); APIKey newkey = null; using (var command = mConnection.CreateCommand()) { command.CommandText = "{CALL GetByKey(?)}"; command.CommandType = System.Data.CommandType.StoredProcedure; command.Parameters.AddWithValue("newkey", key); using (var resultSet = command.ExecuteReader()) { while (resultSet.Read()) { newkey = new APIKey(); newkey.Email = resultSet["Email"].ToString(); newkey.Key = resultSet["KeyText"].ToString(); newkey.Usage = resultSet["UsageText"].ToString(); newkey.State = (APIKeyState)(System.Convert.ToInt16(resultSet["State"].ToString())); } } } return(newkey); }
//Add a key to the database public bool UpdateKey(APIKey key) { //you must give a valid key - also, you cannot change a keycode APIKey oldkey = GetKeyByKey(key.Key); if (oldkey == null) { return(false); } //you cannot modify the email registerd to a key if (key.Email != oldkey.Email) { return(false); } using (var command = mConnection.CreateCommand()) { command.CommandText = "{CALL UpdateKey(?,?,?,?)}"; command.CommandType = System.Data.CommandType.StoredProcedure; command.Parameters.AddWithValue("newEmail", key.Email); command.Parameters.AddWithValue("newKeyText", key.Key); command.Parameters.AddWithValue("newUsage", key.Usage); command.Parameters.AddWithValue("newState", key.State); command.ExecuteScalar(); } return(true); }
public override void SetupTest() { base.SetupTest(); Login(false); mTestKey = new APIKey(); mTestKey.Email = ConfigurationManager.AppSettings["_3DR_UserName"]; mTestKey.Usage = "This is a test for the key request"; mKeyMananger = new APIKeyManager(); }
public override bool Equals(object obj) { APIKey compareTo = obj as APIKey; return(compareTo != null && compareTo.Email.ToLower().Equals(this.Email.ToLower()) && compareTo.Usage.Equals(this.Usage) && compareTo.Key.Equals(this.Key) && compareTo.State.Equals(this.State)); }
//Create a new key for a user public APIKey CreateKey(string email, string usage) { CheckConnection(); APIKey key = new APIKey(); key.Email = email; key.Usage = usage; key.Key = GetNewKey(email, usage); InsertKey(key); return(key); }
//Add a key to the database private bool InsertKey(APIKey key) { using (var command = mConnection.CreateCommand()) { command.CommandText = "{CALL InsertKey(?,?,?,?)}"; command.CommandType = System.Data.CommandType.StoredProcedure; command.Parameters.AddWithValue("newEmail", key.Email); command.Parameters.AddWithValue("newKeyText", key.Key); command.Parameters.AddWithValue("newUsage", key.Usage); command.Parameters.AddWithValue("newState", key.State); command.ExecuteScalar(); } return(true); }
public static Hashtable UpdateKey(string Key, string Description) { System.Collections.Hashtable ht = new System.Collections.Hashtable(); HttpContext context = HttpContext.Current; if (context.User.Identity.IsAuthenticated) { try { vwar.service.host.APIKeyManager keyMan = new vwar.service.host.APIKeyManager(); if (keyMan.GetUserByKey(Key).Equals(context.User.Identity.Name)) { vwar.service.host.APIKey currentKey = keyMan.GetKeyByKey(Key); currentKey.Usage = context.Server.HtmlEncode(Description); if (keyMan.UpdateKey(currentKey)) { ht["Message"] = System.Configuration.ConfigurationManager.AppSettings["ProfilePage_KeyUpdateSuccess"]; ht["Usage"] = currentKey.Usage; } else { ht["Message"] = System.Configuration.ConfigurationManager.AppSettings["ProfilePage_KeyUpdateError"]; } } else { ht["Message"] = System.Configuration.ConfigurationManager.AppSettings["ProfilePage_KeyUpdateError"]; } } catch { ht["Message"] = System.Configuration.ConfigurationManager.AppSettings["ProfilePage_KeyUpdateError"]; } } else { context.Response.StatusCode = 401; } return(ht); }
//Add a key to the database private bool InsertKey(APIKey key) { using (var command = mConnection.CreateCommand()) { command.CommandText = "{CALL InsertKey(?,?,?,?)}"; command.CommandType = System.Data.CommandType.StoredProcedure; command.Parameters.AddWithValue("newEmail", key.Email); command.Parameters.AddWithValue("newKeyText", key.Key); command.Parameters.AddWithValue("newUsage", key.Usage); command.Parameters.AddWithValue("newState", key.State); command.ExecuteScalar(); } return true; }
//Add a key to the database public bool UpdateKey(APIKey key) { //you must give a valid key - also, you cannot change a keycode APIKey oldkey = GetKeyByKey(key.Key); if (oldkey == null) return false; //you cannot modify the email registerd to a key if (key.Email != oldkey.Email) return false; using (var command = mConnection.CreateCommand()) { command.CommandText = "{CALL UpdateKey(?,?,?,?)}"; command.CommandType = System.Data.CommandType.StoredProcedure; command.Parameters.AddWithValue("newEmail", key.Email); command.Parameters.AddWithValue("newKeyText", key.Key); command.Parameters.AddWithValue("newUsage", key.Usage); command.Parameters.AddWithValue("newState", key.State); command.ExecuteScalar(); } return true; }
//Get the user a key is registered to public string GetUserByKey(string key) { CheckConnection(); APIKey newkey = null; using (var command = mConnection.CreateCommand()) { command.CommandText = "{CALL GetByKey(?)}"; command.CommandType = System.Data.CommandType.StoredProcedure; command.Parameters.AddWithValue("newkey", key); using (var resultSet = command.ExecuteReader()) { while (resultSet.Read()) { newkey = new APIKey(); newkey.Email = resultSet["Email"].ToString(); newkey.Key = resultSet["KeyText"].ToString(); newkey.Usage = resultSet["UsageText"].ToString(); newkey.State = (APIKeyState)(System.Convert.ToInt16(resultSet["State"].ToString())); } } } if (newkey != null) return newkey.Email; else return null; }
//Get all keys registered to a user public List<APIKey> GetKeysByUser(string email) { CheckConnection(); List<APIKey> results = new List<APIKey>(); using (var command = mConnection.CreateCommand()) { command.CommandText = "{CALL GetByUser(?)}"; command.CommandType = System.Data.CommandType.StoredProcedure; command.Parameters.AddWithValue("newEmail", email); using (var resultSet = command.ExecuteReader()) { while (resultSet.Read()) { APIKey newkey = new APIKey(); newkey.Email = resultSet["Email"].ToString(); newkey.Key = resultSet["KeyText"].ToString(); newkey.Usage = resultSet["UsageText"].ToString(); newkey.State = (APIKeyState)(System.Convert.ToInt16(resultSet["State"].ToString())); results.Add(newkey); } } } return results; }
//Create a new key for a user public APIKey CreateKey(string email, string usage) { CheckConnection(); APIKey key = new APIKey(); key.Email = email; key.Usage = usage; key.Key = GetNewKey( email, usage); InsertKey(key); return key; }