コード例 #1
0
        public static void Set(AppSettings settings)
        {
            Log.Debug($"Saving settings to file: '{JsonConvert.SerializeObject(settings)}'");
            var configuration = ConfigurationManager.
                                OpenExeConfiguration(Assembly.GetExecutingAssembly().Location);

            UpdateSetting(nameof(AppSettings.ApiUrl), settings.ApiUrl);
            UpdateSetting(nameof(AppSettings.IdentityUrl), settings.IdentityUrl);

            var encryptedClientSecret = StringEncryptor.EncryptString(settings.ClientSecret);

            UpdateSetting(nameof(AppSettings.ClientSecret), encryptedClientSecret);

            configuration.Save();
            ConfigurationManager.RefreshSection("appSettings");

            void UpdateSetting(string key, string value)
            {
                if (configuration.AppSettings.Settings[key] == null)
                {
                    configuration.AppSettings.Settings.Add(key, value);
                }
                else
                {
                    configuration.AppSettings.Settings[key].Value = value;
                }
            }
        }
コード例 #2
0
        public void WrongPasswordTest()
        {
            var sr = new StringEncryptor
            {
                Password = "******"
            };

            var encrypted = sr.EncryptString(input);

            Assert.AreNotEqual(input, encrypted);

            sr.Password = "******";
            sr.DecryptString(encrypted); // throws a cryptographic exception.
        }
コード例 #3
0
        public void RoundTripTest()
        {
            var sr = new StringEncryptor
            {
                Password = "******"
            };

            var encrypted = sr.EncryptString(input);

            Assert.AreNotEqual(input, encrypted);

            var decrypted = sr.DecryptString(encrypted);

            Assert.AreEqual(input, decrypted);
        }
コード例 #4
0
        public void WrongSeedTest()
        {
            var sr = new StringEncryptor
            {
                Password = "******"
            };

            var encrypted = sr.EncryptString(input);

            Assert.AreNotEqual(input, encrypted);

            sr.Seed = Guid.NewGuid();

            var decrypted = sr.DecryptString(encrypted);

            Assert.AreNotEqual(input, decrypted);
        }
コード例 #5
0
        }     // End OnGetAsync

        /*
         * Name: OnPostAsync
         * Parameter: returnUrl(string)
         * Description: The login to this system.
         */
        public async Task <IActionResult> OnPostAsync(string returnUrl = null)
        {
            try
            {
                _logger.LogTrace("Start login on post.");
                returnUrl ??= Url.Content("~/");
                ViewData["URL"] = $"{this.Request.Scheme}://{this.Request.Host}{this.Request.PathBase}";
                if (ModelState.IsValid)
                {
                    _logger.LogTrace("Signing in with password.");
                    var result = await _signInManager.PasswordSignInAsync(Input.Email, Input.Password, Input.RememberMe, lockoutOnFailure : false);

                    if (result.Succeeded)
                    {
                        ApplicationUser user = await _userManager.FindByEmailAsync(Input.Email.ToString());

                        if (user.acc_IsActive == 'N')
                        {
                            await _manageUser.DeleteUser(user.Id);

                            _logger.LogInformation("Change status Inactive to active user.");
                        } // End check status
                        _logger.LogInformation("User logged in successfully.");

                        string nameCookies = StringEncryptor.EncryptString("usermanagementsystem2020", "remembermeums");
                        if (Input.RememberMe)
                        {
                            CookieOptions option = new CookieOptions
                            {
                                Expires  = DateTime.Now.AddDays(14),
                                Path     = $"{this.Request.Scheme}://{this.Request.Host}{this.Request.PathBase}/Identity/Account/Login",
                                HttpOnly = true,
                                SameSite = SameSiteMode.Lax
                            };
                            string cookies = StringEncryptor.EncryptString("usermanagementsystem2020", "UMS.Cookies%" + Input.Email.ToString() + "%" + Input.Password.ToString());
                            Response.Cookies.Delete(nameCookies.ToString());
                            HttpContext.Response.Cookies.Append(nameCookies.ToString(), cookies, option);
                            _logger.LogInformation("Adding cookie into the browser.");
                        } // Remember email and password

                        _logger.LogTrace("End login on post.");
                        return(LocalRedirect(returnUrl));
                    } // If user logged in successfully
                    else
                    {
                        _logger.LogWarning("Your email or password is not valid.");
                        ModelState.AddModelError(string.Empty, "Your email or password is not valid.");
                        TempData["ExceptionInValid"] = "InValid"; // Send alert to home pages
                        _logger.LogTrace("End login on post.");
                        return(Page());
                    } // If Loged out
                }     // End if check model state
                _logger.LogTrace("End login on post.");
                return(Page());
            }
            catch (Exception e)
            {
                _logger.LogError(e.Message.ToString());
                TempData["Exception"] = @"Swal.fire({ icon: 'error', title: 'Error !', text: `" + e.Message.Replace("\\", "/").Replace("`", "'") + @"`, showConfirmButton: true })";
                _logger.LogTrace("End login on post.");
                return(Page());
            } // End try catch
        }     // End OnPostAsync