AgapeConnect1 /App_Code/Account/KeyUser.cs

Language C# Lines 233
MD5 Hash 17b82a63766ca18506ca12c95b3f5eef
Repository https://github.com/mailekah/AgapeConnect1.git View Raw File
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Security.Cryptography;
using System.Text;
using System.Xml.Linq;
using System.Net;
using System.IO;
using AgapeEncryption;
using Account;

namespace KeyUser
{
    public class KeyUser
    {
        #region Constructors
        public KeyUser(string username, string password, string device)
        {
            this._username = username;
            this._password = password;
            this._device = device;


            _loggedSuccessful = Login(this._username, this._password, this._device);
        }

        public KeyUser(Guid keyGuid, Guid mobilePasscode)
        {
            this._keyGuid = keyGuid;
            this._mobilePasscode = mobilePasscode;

            _loggedSuccessful = Login(_keyGuid, _mobilePasscode);
        }
        #endregion

        #region Properties
        private int _id;
        private string _username;
        private string _password;
        private Guid _keyGuid;
        private Guid _mobilePasscode;
        private string _pgt;
        private string _device;
        private DateTime _dataAdded;
        private DateTime _lastDateModified;
        private bool _active;
        private bool _loggedSuccessful;

        public string KeyGuid
        {
            get { return _keyGuid.ToString(); }
        }

        public string MobilePasscode
        {
            get { return _mobilePasscode.ToString(); }
        }

        public string ProxyTicket
        {
            get { return _pgt; }
        }

        public string Device
        {
            get { return _device; }
            set { _device = value; }
        }

        public DateTime DateAdded
        {
            get { return _dataAdded; }
            set { _dataAdded = value; }
        }

        public DateTime LastDateModified
        {
            get { return _lastDateModified; }
            set { _lastDateModified = value; }
        }

        public bool Active
        {
            get { return _active; }
            set { _active = value; }
        }

        public bool LogginSuccessful
        {
            get { return _loggedSuccessful; }
        }
        #endregion

        #region Methods
        ////First time login, for Key proxy
        public bool Login(string username, string password, string device) 
        {
            if (Authenticate(username, password))
            {
                //insert those into the DB, with Key credentials along with the device and date fields
                var checkDb = new KeyEntities().AP_KeyCredentials.Where(k => ((k.KeyGuid == _keyGuid) && (k.Device == device)));

                if (checkDb.Count() == 0)
                {
                    _mobilePasscode = Guid.NewGuid();
                    StoreKeyCredentials(username, password, device, _keyGuid, _mobilePasscode, DateTime.Now, DateTime.Now, true);
                }
                else
                {
                    if (checkDb.First().MobilePasscode != null)
                    {
                        _mobilePasscode = new Guid(checkDb.First().MobilePasscode.ToString());
                    }
                }

                return true;
            }
            else 
            {
                _keyGuid = Guid.Empty;
                _mobilePasscode = Guid.Empty;
                return false;
            }
        }
        
        ////Subsequent logins using "mobile" credentials
        public bool Login(Guid keyGuid, Guid mobilePasscode) 
        {
            if (GetKeyCredentials(keyGuid, mobilePasscode))
            {
                //mobile credentials found
                //result of authentication return true if authenticated... will need to get tickets from this
                return Authenticate(_username, _password);
            }
            else
            {
                //mobile credentials not found
                _keyGuid = Guid.Empty;
                _pgt = string.Empty;
                return false;
            }
        }

        private bool GetKeyCredentials(Guid keyGuid, Guid mobilePasscode) 
        { 
            //Using ssocode and mobilepasscode, get username and password

            var e = new KeyEntities().AP_KeyCredentials.Where(k => ((k.KeyGuid == keyGuid) && (k.MobilePasscode == mobilePasscode)));

            if (e.Count<AP_KeyCredentials>() > 0)
            {
                //If found, set the values
                if (!string.IsNullOrEmpty(e.First<AP_KeyCredentials>().Username))
                { _username = ADCEncrypt.Decrypt(e.First<AP_KeyCredentials>().Username); }
                else
                { _username = string.Empty; }

                if (!string.IsNullOrEmpty(e.First<AP_KeyCredentials>().Password))
                { _password = ADCEncrypt.Decrypt(e.First<AP_KeyCredentials>().Password); }
                else
                { _username = string.Empty; }

                _device = e.First<AP_KeyCredentials>().Device;

                return true;
            }
            else
            {
                //if not found, return false
                _username = string.Empty;
                _password = string.Empty;
                return true;
            }
        }

        private bool Authenticate(string username, string password)
        {
            KeyAuthentication keyAuth = new KeyAuthentication(username, password);

            if (string.IsNullOrEmpty(keyAuth.KeyGuid))
            {
                _pgt = string.Empty;
                return false;
            }
            else
            {
                _keyGuid = new Guid(keyAuth.KeyGuid);
                _pgt = keyAuth.TicketGrantingTicket;

                return true;
            }
        }

        private void StoreKeyCredentials(string username, string password, string device, Guid keyGuid, Guid mobilePasscode, DateTime dateAdded, DateTime lastModified, bool active)
        {
            string EncryptedUserName = ADCEncrypt.Encrypt(username);
            string EncryptedPassword = ADCEncrypt.Encrypt(password);

            AP_KeyCredentials keyCred = new AP_KeyCredentials();

            keyCred.Username = EncryptedUserName;
            keyCred.Password = EncryptedPassword;
            keyCred.KeyGuid = keyGuid;
            keyCred.MobilePasscode = mobilePasscode;
            keyCred.Device = device;
            keyCred.LastModified = lastModified;
            keyCred.DateAdded = dateAdded;
            keyCred.IsActive = active;

            StoreKeyCredentials(keyCred);
        }

        private static void StoreKeyCredentials(AP_KeyCredentials keyCred)
        {
            KeyEntities acEntity = new KeyEntities();

            try
            {
                acEntity.AddToAP_KeyCredentials(keyCred);
                acEntity.SaveChanges();
            }
            catch (Exception e)
            {

            }
        }

        //public static bool DeviceStatus(bool activated) { return true; }
        //public static void DeactivateDevice() { }
        #endregion
    }
}
Back to Top