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