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