Last active
April 22, 2023 18:46
-
-
Save thivi/480a828e20b335e2c7d01ecd6d025209 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[Authorize] | |
public async Task<IActionResult> Secure() | |
{ | |
var accessToken = await HttpContext.GetTokenAsync("access_token"); | |
var idToken = await HttpContext.GetTokenAsync("id_token"); | |
var refreshToken = await HttpContext.GetTokenAsync("refresh_token"); | |
string displayName = UserUtils.GetDisplayName(User); | |
IEnumerable<System.Security.Claims.Claim> claims = User.Claims; | |
// Getting the profile picture URL from the userinfo endpoint | |
// to demonstrate how an API request can be dispatched to a | |
// protected endpoint using the access token. | |
var httpClient = new HttpClient(); | |
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken); | |
string userinfoEndpoint = $"https://api.asgardeo.io/t/{_configuration["Asgardeo:Tenant"]}/oauth2/userinfo"; | |
using var response = await httpClient.GetAsync(userinfoEndpoint); | |
string profilePic = "https://img.freepik.com/free-psd/3d-illustration-person-with-sunglasses_23-2149436188.jpg"; | |
if (response.IsSuccessStatusCode) | |
{ | |
string content = await response.Content.ReadAsStringAsync(); | |
dynamic json = JsonConvert.DeserializeObject(content)!; | |
profilePic = json.profile; | |
} | |
return View(new Secure { | |
Claims = claims, | |
AccessToken = accessToken, | |
DisplayName = displayName, | |
IdToken = idToken, | |
RefreshToken = refreshToken, | |
ProfileURL = profilePic | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment