-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathEasyAuthAuthenticationHandler.cs
43 lines (35 loc) · 1.45 KB
/
EasyAuthAuthenticationHandler.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
using System.Text.Encodings.Web;
using Microsoft.AspNetCore.Authentication;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
namespace EasyAuth.Handlers;
public class EasyAuthAuthenticationHandler(IOptionsMonitor<EasyAuthAuthenticationOptions> options, ILoggerFactory logger, UrlEncoder encoder)
: AuthenticationHandler<EasyAuthAuthenticationOptions>(options, logger, encoder)
{
public const string EASY_AUTH_SCHEME_NAME = "EasyAuth";
protected override async Task<AuthenticateResult> HandleAuthenticateAsync()
{
try
{
var easyAuthProvider = Context.Request.Headers["X-MS-CLIENT-PRINCIPAL-IDP"].FirstOrDefault() ?? "aad";
var encoded = Context.Request.Headers["X-MS-CLIENT-PRINCIPAL"].FirstOrDefault();
if (string.IsNullOrWhiteSpace(encoded) == true)
{
return AuthenticateResult.NoResult();
}
var principal = await MsClientPrincipal.ParseClaimsPrincipal(encoded!).ConfigureAwait(false);
if (principal == null)
{
return AuthenticateResult.NoResult();
}
var ticket = new AuthenticationTicket(principal, easyAuthProvider);
var success = AuthenticateResult.Success(ticket);
this.Context.User = principal;
return success;
}
catch (Exception ex)
{
return AuthenticateResult.Fail(ex);
}
}
}