Ejemplo n.º 1
0
        public async Task <string> Login(Credential credential)
        {
            await DatabaseFunctions.InitializeStaticStorage(_db).ConfigureAwait(false);

            if (credential is null)
            {
                return("false");
            }

            credential.CredentialsUsername = credential.CredentialsUsername.ToLower();
            var credentialQueryingList = await DatabaseFunctions.GetCredentials(_db, credential).ConfigureAwait(false);

            if (credentialQueryingList.Count == 0)
            {
                return("false");
            }

            var passwordSalt = await DatabaseFunctions.GetPasswordSalt(_db, credential).ConfigureAwait(false);

            var passwordInDatabase = await DatabaseFunctions.GetPasswordFromDb(_db, credential).ConfigureAwait(false);

            var decryptPassword = HashServices.Decrypt(passwordSalt, credential.CredentialsPassword);

            return(!decryptPassword.Equals(passwordInDatabase) ? "false" : GenerateToken.Generate(credential.CredentialsUsername, _jwtSettings));
        }
Ejemplo n.º 2
0
        public async Task <bool> ChangePassword(ChangePasswordInfo changePasswordInfo)
        {
            await DatabaseFunctions.InitializeStaticStorage(_db).ConfigureAwait(false);

            try
            {
                changePasswordInfo.CredentialUsername = changePasswordInfo.CredentialUsername.ToLower();
                var credential = new Credential()
                {
                    CredentialsPassword = changePasswordInfo.OldPassword,
                    CredentialsUsername = changePasswordInfo.CredentialUsername
                };

                var passwordSalt = await DatabaseFunctions.GetPasswordSalt(_db, credential).ConfigureAwait(false);

                var passwordInDatabase = await DatabaseFunctions.GetPasswordFromDb(_db, credential).ConfigureAwait(false);

                var decryptPassword = HashServices.Decrypt(passwordSalt, credential.CredentialsPassword);

                if (!decryptPassword.Equals(passwordInDatabase))
                {
                    return(false);
                }

                await DatabaseFunctions.ChangePasswordInDb(changePasswordInfo.CredentialUsername, changePasswordInfo.NewPassword,
                                                           _db);

                return(true);
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                return(false);
            }
        }
Ejemplo n.º 3
0
        public async Task <bool> ResetPcCredentialPassword(Credential credential)
        {
            await DatabaseFunctions.InitializeStaticStorage(_db).ConfigureAwait(false);

            try
            {
                credential.CredentialsUsername = credential.CredentialsUsername.ToLower();
                var credentialFromDb = await _db.Credentials
                                       .Where(c => c.CredentialsUsername.Equals(credential.CredentialsUsername)).FirstOrDefaultAsync()
                                       .ConfigureAwait(false);

                if (credentialFromDb == null)
                {
                    return(false);
                }

                var passwordSalt = await DatabaseFunctions.GetPasswordSalt(_db, credential).ConfigureAwait(false);

                var passwordInDatabase = await DatabaseFunctions.GetPasswordFromDb(_db, credential).ConfigureAwait(false);

                var decryptPassword = HashServices.Decrypt(passwordSalt, credential.CredentialsPassword);

                if (!decryptPassword.Equals(passwordInDatabase))
                {
                    return(false);
                }

                credentialFromDb.PcCredentialPassword = ModelCreation.GenerateRandomString();

                StaticStorageServices.AdminMapper[credential.CredentialsUsername] = credentialFromDb.PcCredentialPassword;

                await EmailServices.SendEmail(credential.CredentialsUsername, $"New Pc Credential Password: {credentialFromDb.PcCredentialPassword}");

                await _db.SaveChangesAsync();

                return(true);
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                return(false);
            }
        }