예제 #1
0
        private async Task <OCloudUser> Authenticate(LoginModel login)
        {
            if (string.IsNullOrEmpty(login.Username) || string.IsNullOrEmpty(login.Password))
            {
                return(await Task.FromResult <OCloudUser>(null));
            }

            OCloudUser user = await _userManager.FindByNameAsync(login.Username);

            bool?isPwdCorrect = null;

            if (user != null)
            {
                isPwdCorrect = await _userManager.CheckPasswordAsync(user, login.Password);
            }

            if (!isPwdCorrect.HasValue || !isPwdCorrect.Value)
            {
                if (Request.IsRequestFromLocalHost())
                {
                    if (login.Username.Equals(_config["LocalAdmin:username"]) &&
                        login.Password.Equals(_config["LocalAdmin:password"]))
                    {
                        user = new OCloudUser(_config["LocalAdmin:username"]);
                    }
                }
            }

            return(user);
        }
예제 #2
0
        private string BuildToken(OCloudUser user)
        {
            var key   = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(_config["Jwt:Key"]));
            var creds = new SigningCredentials(key, SecurityAlgorithms.HmacSha256);

            var token = new JwtSecurityToken(_config["Jwt:Issuer"],
                                             _config["Jwt:Issuer"],
                                             expires: DateTime.Now.AddMinutes(30),
                                             signingCredentials: creds);

            return(new JwtSecurityTokenHandler().WriteToken(token));
        }
예제 #3
0
        public async Task <IActionResult> OnPostAsync(string returnUrl = null)
        {
            returnUrl = returnUrl ?? Url.Content("~/");
            if (ModelState.IsValid)
            {
                var user = new OCloudUser(LoginInfo.UserName);

                var result = await _userManager.CreateAsync(user, LoginInfo.Password);

                if (result.Succeeded)
                {
                    _logger.LogInformation($"User '{LoginInfo.UserName}' created a new account with password.");

                    //var code = await _userManager.GenerateEmailConfirmationTokenAsync(user);
                    //var callbackUrl = Url.Page(
                    //    "/Account/ConfirmEmail",
                    //    pageHandler: null,
                    //    values: new { userId = user.Id, code = code },
                    //    protocol: Request.Scheme);

                    //await _emailSender.SendEmailAsync(Input.Email, "Confirm your email",
                    //    $"Please confirm your account by <a href='{HtmlEncoder.Default.Encode(callbackUrl)}'>clicking here</a>.");

                    if (LoginInfo.LoginAfterCreation)
                    {
                        await _signInManager.SignInAsync(user, isPersistent : false);
                    }
                    return(LocalRedirect(returnUrl));
                }
                foreach (var error in result.Errors)
                {
                    ModelState.AddModelError(string.Empty, error.Description);
                }
            }

            // If we got this far, something failed, redisplay form
            return(Page());
        }