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);
        }
Exemple #2
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);
        }