Ejemplo n.º 1
0
        public async Task <IActionResult> OnPostAsync(string returnUrl = null)
        {
            returnUrl = returnUrl ?? Url.Content("~/");

            if (ModelState.IsValid)
            {
                // This doesn't count login failures towards account lockout
                // To enable password failures to trigger account lockout, set lockoutOnFailure: true
                var result = await _signInManager.PasswordSignInAsync(Input.Email, Input.Password, Input.RememberMe, lockoutOnFailure : true);

                if (result.Succeeded)
                {
                    var credViewModel = new Models.CredentialsViewModel()
                    {
                        UserName = Input.Email, Password = Input.Password
                    };
                    HttpClient          client   = _httpService.GetHttpClientInstance();
                    HttpResponseMessage response = await client.PostAsJsonAsync(_httpService.GetBaseURL() + "auth/login", credViewModel);

                    if (response.IsSuccessStatusCode)
                    {
                        var tokenDetails = await response.Content.ReadAsAsync <string>();

                        var tokenObj = JsonConvert.DeserializeObject <AuthTokenModel>(tokenDetails);
                        _tokenDTService.SetToken(tokenObj.Auth_Token);
                    }

                    _logger.LogInformation("User logged in.");
                    return(LocalRedirect(returnUrl));
                }
                if (result.RequiresTwoFactor)
                {
                    return(RedirectToPage("./LoginWith2fa", new { ReturnUrl = returnUrl, RememberMe = Input.RememberMe }));
                }
                if (result.IsLockedOut)
                {
                    _logger.LogWarning("User account locked out.");
                    return(RedirectToPage("./Lockout"));
                }
                else
                {
                    ModelState.AddModelError(string.Empty, "Invalid login attempt.");
                    return(Page());
                }
            }

            // If we got this far, something failed, redisplay form
            return(Page());
        }
Ejemplo n.º 2
0
        public async Task <IActionResult> OnPostAsync(string returnUrl = null)
        {
            returnUrl = returnUrl ?? Url.Content("~/");
            if (ModelState.IsValid)
            {
                var user = new IdentityUser {
                    UserName = Input.Email, Email = Input.Email
                };
                //var result = await _userManager.CreateAsync(user, Input.Password);
                dynamic userWithPassword = new System.Dynamic.ExpandoObject();
                userWithPassword.user     = user;
                userWithPassword.password = Input.Password;

                HttpClient          client   = _httpService.GetHttpClientInstance();
                HttpResponseMessage response = await client.PostAsJsonAsync(_httpService.GetBaseURL() + "auth/CreateAsync", new { user, Input.Password });

                bool result = false;
                if (response.IsSuccessStatusCode)
                {
                    result = await response.Content.ReadAsAsync <bool>();
                }

                if (result)//result.Succeeded
                {
                    _logger.LogInformation("User 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>.");

                    //await _signInManager.SignInAsync(user, isPersistent: false);
                    var res = await _signInManager.PasswordSignInAsync(Input.Email, Input.Password, false, lockoutOnFailure : true);

                    if (res.Succeeded)
                    {
                        var credViewModel = new Models.CredentialsViewModel()
                        {
                            UserName = Input.Email, Password = Input.Password
                        };
                        HttpClient          client1   = _httpService.GetHttpClientInstance();
                        HttpResponseMessage response1 = await client.PostAsJsonAsync(_httpService.GetBaseURL() + "auth/login", credViewModel);

                        if (response.IsSuccessStatusCode)
                        {
                            var tokenDetails = await response1.Content.ReadAsAsync <string>();

                            var tokenObj = JsonConvert.DeserializeObject <AuthTokenModel>(tokenDetails);
                            _tokenDTService.SetToken(tokenObj.Auth_Token);
                        }

                        _logger.LogInformation("User logged in.");
                        return(LocalRedirect(returnUrl));
                    }

                    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());
        }