-
Notifications
You must be signed in to change notification settings - Fork 1k
/
Copy pathProfileController.cs
55 lines (47 loc) · 1.81 KB
/
ProfileController.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.Identity.Client;
using Microsoft.Identity.Web;
using Microsoft.Graph;
using Microsoft.Graph.Models;
namespace TodoListBFF.Controllers;
[Authorize(AuthenticationSchemes = CookieAuthenticationDefaults.AuthenticationScheme)]
[Route("api/[controller]")]
[ApiController]
public class ProfileController : Controller
{
private readonly GraphServiceClient _graphServiceClient;
public ProfileController(GraphServiceClient graphServiceClient)
{
_graphServiceClient = graphServiceClient;
}
[HttpGet]
public async Task<ActionResult<User>> GetProfile()
{
try
{
User? profile = await _graphServiceClient.Me
.GetAsync();
return Ok(profile);
}
catch (ServiceException svcex)
when (svcex.InnerException is MicrosoftIdentityWebChallengeUserException)
{
return Unauthorized("MicrosoftIdentityWebChallengeUserException occurred\n" + svcex.Message);
}
catch (ServiceException svcex)
when (svcex.Message.Contains("Continuous access evaluation"))
{
string claimsChallenge = WwwAuthenticateParameters
.GetClaimChallengeFromResponseHeaders(svcex.ResponseHeaders);
// Set the claims challenge string to session, which will be used during the next login request
HttpContext.Session.SetString("claimsChallenge", claimsChallenge);
return Unauthorized("Continuous access evaluation resulted in claims challenge\n" + svcex.Message);
}
catch (Exception ex)
{
return BadRequest("An error occurred while calling the downstream API\n" + ex.Message);
}
}
}