public static async Task LogoutAsync(string sessionToken)
        {
            Authorization.CheckAuthorization(sessionToken, MethodBase.GetCurrentMethod());

            try
            {
                using var sessionCtrl = new Controllers.Persistence.Account.LoginSessionController(Factory.CreateContext())
                      {
                          SessionToken = Authorization.SystemAuthorizationToken
                      };
                var session = sessionCtrl.ExecuteQuery(e => e.SessionToken.Equals(sessionToken))
                              .ToList()
                              .FirstOrDefault(e => e.IsActive);

                if (session != null)
                {
                    session.LogoutTime = DateTime.Now;

                    await sessionCtrl.UpdateAsync(session).ConfigureAwait(false);

                    await sessionCtrl.SaveChangesAsync().ConfigureAwait(false);
                }
            }
            catch (LogicException ex)
            {
                System.Diagnostics.Debug.WriteLine($"Error in {MethodBase.GetCurrentMethod().Name}: {ex.Message}");
            }
        }
예제 #2
0
        internal static async Task <LoginSession> QueryLoginByEmailAsync(string email, string password)
        {
            email.CheckArgument(nameof(email));
            password.CheckArgument(nameof(password));

            var result       = default(LoginSession);
            var querySession = await QueryAliveSessionAsync(email, password).ConfigureAwait(false);

            if (querySession == null)
            {
                using var identityCtrl = new Controllers.Persistence.Account.IdentityController(Factory.CreateContext())
                      {
                          SessionToken = Authorization.SystemAuthorizationToken,
                      };
                var identity = identityCtrl.ExecuteQuery(e => e.State == Contracts.Modules.Common.State.Active &&
                                                         e.AccessFailedCount < 4 &&
                                                         e.Email.ToLower() == email.ToLower()).FirstOrDefault();

                if (identity != null && VerifyPasswordHash(password, identity.PasswordHash, identity.PasswordSalt))
                {
                    using var sessionCtrl = new Controllers.Persistence.Account.LoginSessionController(identityCtrl);
                    var session = new LoginSession();

                    session.Identity = identity;
                    session.Roles.AddRange(await QueryIdentityRolesAsync(sessionCtrl, identity.Id).ConfigureAwait(false));
                    session.JsonWebToken = JsonWebToken.GenerateToken(new Claim[]
                    {
                        new Claim(ClaimTypes.Email, identity.Email),
                        new Claim(ClaimTypes.System, nameof(QuickNSmart)),
                    }.Union(session.Roles.Select(e => new Claim(ClaimTypes.Role, e.Designation))));
                    var entity = await sessionCtrl.ExecuteInsertAsync(session).ConfigureAwait(false);

                    if (identity.AccessFailedCount > 0)
                    {
                        identity.AccessFailedCount = 0;
                        await identityCtrl.ExecuteUpdateAsync(identity).ConfigureAwait(false);
                    }
                    await sessionCtrl.SaveChangesAsync().ConfigureAwait(false);

                    result = new LoginSession();
                    result.CopyProperties(session);
                    LoginSessions.Add(session);
                }
            }
            else if (VerifyPasswordHash(password, querySession.PasswordHash, querySession.PasswordSalt))
            {
                result = new LoginSession();

                result.CopyProperties(querySession);
            }

            return(result);
        }
예제 #3
0
        internal static async Task <LoginSession> QueryLoginByEmailAsync(string email, string password, string optionalInfo)
        {
            email.CheckArgument(nameof(email));
            password.CheckArgument(nameof(password));

            var result       = default(LoginSession);
            var querySession = await QueryAliveSessionAsync(email, password).ConfigureAwait(false);

            if (querySession == null)
            {
                using var identityCtrl = new Controllers.Persistence.Account.IdentityController(Factory.CreateContext())
                      {
                          SessionToken = Authorization.SystemAuthorizationToken,
                      };
                var identity = await identityCtrl.GetValidIdentityByEmail(email)
                               .ConfigureAwait(false);

                if (identity != null &&
                    VerifyPasswordHash(password, identity.PasswordHash, identity.PasswordSalt))
                {
                    var session = new LoginSession();
                    using var sessionCtrl = new Controllers.Persistence.Account.LoginSessionController(identityCtrl);

                    session.Identity = identity;
                    session.Roles.AddRange(identity.IdentityXRoles.Select(e => Role.Create(e.Role)));
                    session.JsonWebToken = JsonWebToken.GenerateToken(new Claim[]
                    {
                        new Claim(ClaimTypes.Email, identity.Email),
                        new Claim(ClaimTypes.System, nameof(QnSProjectAward)),
                    }.Union(session.Roles.Select(e => new Claim(ClaimTypes.Role, e.Designation))));
                    session.OptionalInfo = optionalInfo;

                    var entity = await sessionCtrl.ExecuteInsertEntityAsync(session).ConfigureAwait(false);

                    if (identity.AccessFailedCount > 0)
                    {
                        identity.AccessFailedCount = 0;
                        await identityCtrl.ExecuteUpdateAsync(identity).ConfigureAwait(false);
                    }
                    await sessionCtrl.SaveChangesAsync().ConfigureAwait(false);

                    result = new LoginSession();
                    result.CopyProperties(session);
                    LoginSessions.Add(session);
                }
            }
            else if (VerifyPasswordHash(password, querySession.PasswordHash, querySession.PasswordSalt))
            {
                result = new LoginSession();
                result.CopyProperties(querySession);
            }
            return(result);
        }
        internal static async Task <LoginSession> QueryLoginAsync(string email, byte[] calculatedHash)
        {
            email.CheckArgument(nameof(email));
            calculatedHash.CheckArgument(nameof(calculatedHash));

            var result = await QueryAliveSessionAsync(email, calculatedHash).ConfigureAwait(false);

            if (result == null)
            {
                using var identityCtrl = new Controllers.Persistence.Account.IdentityController(Factory.CreateContext())
                      {
                          SessionToken = Authorization.SystemAuthorizationToken,
                      };
                var identity = identityCtrl.ExecuteQuery(e => e.State == Contracts.Modules.Common.State.Active &&
                                                         e.AccessFailedCount < 4 &&
                                                         e.Email.ToLower() == email.ToLower() &&
                                                         e.PasswordHash == calculatedHash).FirstOrDefault();

                if (identity != null)
                {
                    using var sessionCtrl = new Controllers.Persistence.Account.LoginSessionController(identityCtrl);
                    var session = new LoginSession();

                    session.IdentityId = identity.Id;
                    session.Name       = identity.Name;
                    session.Email      = identity.Email;
                    session.Roles.AddRange(await QueryIdentityRolesAsync(sessionCtrl, identity.Id).ConfigureAwait(false));
                    var entity = await sessionCtrl.ExecuteInsertAsync(session).ConfigureAwait(false);

                    if (identity.AccessFailedCount > 0)
                    {
                        identity.AccessFailedCount = 0;
                        await identityCtrl.ExecuteUpdateAsync(identity).ConfigureAwait(false);
                    }
                    await sessionCtrl.SaveChangesAsync().ConfigureAwait(false);

                    result = new LoginSession();
                    result.CopyProperties(session);
                    result.Name  = identity.Name;
                    result.Email = identity.Email;
                    result.Roles.AddRange(session.Roles);
                    result.JsonWebToken = JsonWebToken.GenerateToken(new Claim[]
                    {
                        new Claim(ClaimTypes.Email, identity.Email),
                        new Claim(ClaimTypes.System, nameof(QnSHolidayCalendar)),
                    }.Union(result.Roles.Select(e => new Claim(ClaimTypes.Role, e.Designation))));
                    LoginSessions.Add(result);
                }
            }
            return(result);
        }
        private static void UpdateSession()
        {
            while (true)
            {
                Task.Run(async() =>
                {
                    try
                    {
                        using var sessionCtrl = new Controllers.Persistence.Account.LoginSessionController(Factory.CreateContext())
                              {
                                  SessionToken = Authorization.SystemAuthorizationToken,
                              };
                        bool saveChanges    = false;
                        var dbSessions      = sessionCtrl.ExecuteQuery(e => e.LogoutTime.HasValue == false).ToList();
                        var uncheckSessions = LoginSessions.Where(i => dbSessions.Any() == false ||
                                                                  dbSessions.Any(e => e.Id != i.Id));

                        foreach (var dbItem in dbSessions)
                        {
                            var itemUpdate    = false;
                            var memItemRemove = false;
                            var memItem       = LoginSessions.FirstOrDefault(e => e.Id == dbItem.Id);

                            if (memItem != null && memItem.HasChanged)
                            {
                                itemUpdate         = true;
                                memItem.HasChanged = false;
                                dbItem.LastAccess  = memItem.LastAccess;
                            }
                            if (dbItem.IsTimeout)
                            {
                                itemUpdate = true;
                                if (memItem != null)
                                {
                                    memItemRemove = true;
                                }
                                if (dbItem.LogoutTime.HasValue == false)
                                {
                                    dbItem.LogoutTime = DateTime.Now;
                                }
                            }
                            if (itemUpdate)
                            {
                                saveChanges = true;
                                await sessionCtrl.ExecuteUpdateAsync(dbItem).ConfigureAwait(false);
                            }
                            if (memItemRemove)
                            {
                                LoginSessions.Remove(memItem);
                            }
                        }
                        if (saveChanges)
                        {
                            await sessionCtrl.SaveChangesAsync().ConfigureAwait(false);
                        }
                        foreach (var memItem in uncheckSessions)
                        {
                            var dbItem = sessionCtrl.ExecuteQueryById(memItem.Id);

                            if (dbItem != null)
                            {
                                memItem.LastAccess = dbItem.LastAccess;
                                memItem.LogoutTime = dbItem.LogoutTime;
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        System.Diagnostics.Debug.WriteLine($"Error in {MethodBase.GetCurrentMethod().Name}: {ex.Message}");
                    }
                });
                Thread.Sleep(UpdateDelay);
            }
        }