LoginModel.OnPostAsync C# (CSharp) Method

OnPostAsync() public method

public OnPostAsync ( ) : Task
return Task
    public async Task<IActionResult> OnPostAsync()
    {
        var deviceCode = HttpContext.Session.GetString("DeviceCode");
        var interval = HttpContext.Session.GetInt32("Interval");

        if(interval.GetValueOrDefault() <= 0)
        {
            interval = 5;
        }

        var tokenresponse = await _deviceFlowService.RequestTokenAsync(deviceCode, interval.Value);

        if (tokenresponse.IsError)
        {
            ModelState.AddModelError(string.Empty, "Invalid login attempt.");
            return Page();
        }

        var claims = GetClaims(tokenresponse.IdentityToken);

        var claimsIdentity = new ClaimsIdentity(
            claims, 
            CookieAuthenticationDefaults.AuthenticationScheme, 
            "name", 
            "user");

        var authProperties = new AuthenticationProperties();

        // save the tokens in the cookie
        authProperties.StoreTokens(new List<AuthenticationToken>
        {
            new AuthenticationToken
            {
                Name = "access_token",
                Value = tokenresponse.AccessToken
            },
            new AuthenticationToken
            {
                Name = "id_token",
                Value = tokenresponse.IdentityToken
            }
        });

        await HttpContext.SignInAsync(
            CookieAuthenticationDefaults.AuthenticationScheme,
            new ClaimsPrincipal(claimsIdentity),
            authProperties);

        return Redirect("/Index");
    }

Usage Example

示例#1
0
        public async Task OnLogin_WhenPassSkipProfileUsers_RedirectToApiKey()
        {
            var portFreightUser = new PortFreightUser()
            {
                UserName     = "******",
                Email        = "*****@*****.**",
                PasswordHash = "TestTest1!",
                SenderId     = "T12345"
            };

            portFreightUsersList = new List <PortFreightUser>();
            portFreightUsersList.Add(portFreightUser);

            loginModel.Input = new LoginModel.InputModel
            {
                Email      = portFreightUser.Email,
                Password   = portFreightUser.PasswordHash,
                RememberMe = true
            };

            mockConfig.Setup(x => x[It.IsAny <string>()]).Returns("T12345");

            mockfakeSignInManager.Setup(x => x.PasswordSignInAsync(loginModel.Input.Email, loginModel.Input.Password, loginModel.Input.RememberMe, true)).ReturnsAsync(Microsoft.AspNetCore.Identity.SignInResult.Success);

            mockUserManager.Setup(u => u.FindByEmailAsync(It.IsAny <string>())).ReturnsAsync(portFreightUsersList.FirstOrDefault());

            var result = (RedirectToPageResult)await loginModel.OnPostAsync();

            Assert.IsNotNull(result);
            Assert.AreEqual("/ApiKey/ApiKey", result.PageName);
            Assert.IsInstanceOfType(result, typeof(RedirectToPageResult));
        }
All Usage Examples Of LoginModel::OnPostAsync