PageRenderTime 48ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/App_Code/Account/KeyUser.cs

https://github.com/mailekah/AgapeConnect1
C# | 233 lines | 187 code | 35 blank | 11 comment | 17 complexity | 17b82a63766ca18506ca12c95b3f5eef MD5 | raw file
Possible License(s): BSD-3-Clause, Apache-2.0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Security.Cryptography;
  6. using System.Text;
  7. using System.Xml.Linq;
  8. using System.Net;
  9. using System.IO;
  10. using AgapeEncryption;
  11. using Account;
  12. namespace KeyUser
  13. {
  14. public class KeyUser
  15. {
  16. #region Constructors
  17. public KeyUser(string username, string password, string device)
  18. {
  19. this._username = username;
  20. this._password = password;
  21. this._device = device;
  22. _loggedSuccessful = Login(this._username, this._password, this._device);
  23. }
  24. public KeyUser(Guid keyGuid, Guid mobilePasscode)
  25. {
  26. this._keyGuid = keyGuid;
  27. this._mobilePasscode = mobilePasscode;
  28. _loggedSuccessful = Login(_keyGuid, _mobilePasscode);
  29. }
  30. #endregion
  31. #region Properties
  32. private int _id;
  33. private string _username;
  34. private string _password;
  35. private Guid _keyGuid;
  36. private Guid _mobilePasscode;
  37. private string _pgt;
  38. private string _device;
  39. private DateTime _dataAdded;
  40. private DateTime _lastDateModified;
  41. private bool _active;
  42. private bool _loggedSuccessful;
  43. public string KeyGuid
  44. {
  45. get { return _keyGuid.ToString(); }
  46. }
  47. public string MobilePasscode
  48. {
  49. get { return _mobilePasscode.ToString(); }
  50. }
  51. public string ProxyTicket
  52. {
  53. get { return _pgt; }
  54. }
  55. public string Device
  56. {
  57. get { return _device; }
  58. set { _device = value; }
  59. }
  60. public DateTime DateAdded
  61. {
  62. get { return _dataAdded; }
  63. set { _dataAdded = value; }
  64. }
  65. public DateTime LastDateModified
  66. {
  67. get { return _lastDateModified; }
  68. set { _lastDateModified = value; }
  69. }
  70. public bool Active
  71. {
  72. get { return _active; }
  73. set { _active = value; }
  74. }
  75. public bool LogginSuccessful
  76. {
  77. get { return _loggedSuccessful; }
  78. }
  79. #endregion
  80. #region Methods
  81. ////First time login, for Key proxy
  82. public bool Login(string username, string password, string device)
  83. {
  84. if (Authenticate(username, password))
  85. {
  86. //insert those into the DB, with Key credentials along with the device and date fields
  87. var checkDb = new KeyEntities().AP_KeyCredentials.Where(k => ((k.KeyGuid == _keyGuid) && (k.Device == device)));
  88. if (checkDb.Count() == 0)
  89. {
  90. _mobilePasscode = Guid.NewGuid();
  91. StoreKeyCredentials(username, password, device, _keyGuid, _mobilePasscode, DateTime.Now, DateTime.Now, true);
  92. }
  93. else
  94. {
  95. if (checkDb.First().MobilePasscode != null)
  96. {
  97. _mobilePasscode = new Guid(checkDb.First().MobilePasscode.ToString());
  98. }
  99. }
  100. return true;
  101. }
  102. else
  103. {
  104. _keyGuid = Guid.Empty;
  105. _mobilePasscode = Guid.Empty;
  106. return false;
  107. }
  108. }
  109. ////Subsequent logins using "mobile" credentials
  110. public bool Login(Guid keyGuid, Guid mobilePasscode)
  111. {
  112. if (GetKeyCredentials(keyGuid, mobilePasscode))
  113. {
  114. //mobile credentials found
  115. //result of authentication return true if authenticated... will need to get tickets from this
  116. return Authenticate(_username, _password);
  117. }
  118. else
  119. {
  120. //mobile credentials not found
  121. _keyGuid = Guid.Empty;
  122. _pgt = string.Empty;
  123. return false;
  124. }
  125. }
  126. private bool GetKeyCredentials(Guid keyGuid, Guid mobilePasscode)
  127. {
  128. //Using ssocode and mobilepasscode, get username and password
  129. var e = new KeyEntities().AP_KeyCredentials.Where(k => ((k.KeyGuid == keyGuid) && (k.MobilePasscode == mobilePasscode)));
  130. if (e.Count<AP_KeyCredentials>() > 0)
  131. {
  132. //If found, set the values
  133. if (!string.IsNullOrEmpty(e.First<AP_KeyCredentials>().Username))
  134. { _username = ADCEncrypt.Decrypt(e.First<AP_KeyCredentials>().Username); }
  135. else
  136. { _username = string.Empty; }
  137. if (!string.IsNullOrEmpty(e.First<AP_KeyCredentials>().Password))
  138. { _password = ADCEncrypt.Decrypt(e.First<AP_KeyCredentials>().Password); }
  139. else
  140. { _username = string.Empty; }
  141. _device = e.First<AP_KeyCredentials>().Device;
  142. return true;
  143. }
  144. else
  145. {
  146. //if not found, return false
  147. _username = string.Empty;
  148. _password = string.Empty;
  149. return true;
  150. }
  151. }
  152. private bool Authenticate(string username, string password)
  153. {
  154. KeyAuthentication keyAuth = new KeyAuthentication(username, password);
  155. if (string.IsNullOrEmpty(keyAuth.KeyGuid))
  156. {
  157. _pgt = string.Empty;
  158. return false;
  159. }
  160. else
  161. {
  162. _keyGuid = new Guid(keyAuth.KeyGuid);
  163. _pgt = keyAuth.TicketGrantingTicket;
  164. return true;
  165. }
  166. }
  167. private void StoreKeyCredentials(string username, string password, string device, Guid keyGuid, Guid mobilePasscode, DateTime dateAdded, DateTime lastModified, bool active)
  168. {
  169. string EncryptedUserName = ADCEncrypt.Encrypt(username);
  170. string EncryptedPassword = ADCEncrypt.Encrypt(password);
  171. AP_KeyCredentials keyCred = new AP_KeyCredentials();
  172. keyCred.Username = EncryptedUserName;
  173. keyCred.Password = EncryptedPassword;
  174. keyCred.KeyGuid = keyGuid;
  175. keyCred.MobilePasscode = mobilePasscode;
  176. keyCred.Device = device;
  177. keyCred.LastModified = lastModified;
  178. keyCred.DateAdded = dateAdded;
  179. keyCred.IsActive = active;
  180. StoreKeyCredentials(keyCred);
  181. }
  182. private static void StoreKeyCredentials(AP_KeyCredentials keyCred)
  183. {
  184. KeyEntities acEntity = new KeyEntities();
  185. try
  186. {
  187. acEntity.AddToAP_KeyCredentials(keyCred);
  188. acEntity.SaveChanges();
  189. }
  190. catch (Exception e)
  191. {
  192. }
  193. }
  194. //public static bool DeviceStatus(bool activated) { return true; }
  195. //public static void DeactivateDevice() { }
  196. #endregion
  197. }
  198. }