LoginModel.OnGetAsync C# (CSharp) Method

OnGetAsync() public method

public OnGetAsync ( ) : Task
return Task
    public async Task OnGetAsync()
    {
        HttpContext.Session.SetString("DeviceCode", string.Empty);

        await HttpContext.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme);

        var deviceAuthorizationResponse = await _deviceFlowService.RequestDeviceCode();
        AuthenticatorUri = deviceAuthorizationResponse.VerificationUri;
        UserCode = deviceAuthorizationResponse.UserCode;

        if (string.IsNullOrEmpty(HttpContext.Session.GetString("DeviceCode")))
        {
            HttpContext.Session.SetString("DeviceCode", deviceAuthorizationResponse.DeviceCode);
            HttpContext.Session.SetInt32("Interval", deviceAuthorizationResponse.Interval);
        }
    }

Usage Example

示例#1
0
        public async Task OnGetAsync_WithError()
        {
            // Arrange
            var httpContext   = new DefaultHttpContext();
            var modelState    = new ModelStateDictionary();
            var actionContext = new ActionContext(
                httpContext: httpContext,
                routeData: new RouteData(),
                actionDescriptor: new PageActionDescriptor(),
                modelState: modelState);
            var viewData = new ViewDataDictionary(
                metadataProvider: new EmptyModelMetadataProvider(),
                modelState: modelState);
            var login = new LoginModel(_signInManager.Object, _userManager.Object, _logger.Object)
            {
                PageContext = new PageContext(actionContext)
                {
                    ViewData    = viewData,
                    HttpContext = httpContext
                },
                Url          = new UrlHelper(actionContext),
                ErrorMessage = "Error"
            };

            // Act
            var get = await login.OnGetAsync().ConfigureAwait(false);

            // Assert
            Assert.IsType <PageResult>(get);
            Assert.Equal(login.Url.Content("~/"), login.ReturnUrl);
            Assert.Equal(1, login.ModelState.ErrorCount);
        }
All Usage Examples Of LoginModel::OnGetAsync