using System.Net.Http;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Authentication.OpenIdConnect;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.IdentityModel.Protocols.OpenIdConnect;
public async Task UserInfo()
{
var accessToken = await HttpContext.GetTokenAsync(
OpenIdConnectParameterNames.AccessToken);
var client = new HttpClient();
client.SetBearerToken(accessToken);
var response = await client.GetAsync("https://adfs.example.com/userinfo");
if (response.IsSuccessStatusCode)
{
var json = await response.Content.ReadAsStringAsync();
// parse user information from JSON and store in application
}
else if (response.StatusCode == System.Net.HttpStatusCode.Unauthorized)
{
// redirect to ADFS login page to refresh token
var properties = await HttpContext
.GetAuthenticationPropertiesAsync(
OpenIdConnectDefaults.AuthenticationScheme);
properties.RedirectUri = "/UserInfo";
await HttpContext
.ChallengeAsync(OpenIdConnectDefaults.AuthenticationScheme,
properties);
}
else
{
return StatusCode((int)response.StatusCode);
}
return View();
}
上一篇:ADFS外网智能锁定的启用方法