-
Notifications
You must be signed in to change notification settings - Fork 11
/
AddAuthenticationExtensions.cs
60 lines (54 loc) · 2.43 KB
/
AddAuthenticationExtensions.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
55
56
57
58
59
60
using System;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Authentication.OpenIdConnect;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
namespace PingOne.Core.Configuration.Extensions
{
public static class AddAuthenticationExtensions
{
public static IServiceCollection AddPingOneAuthentication(
this IServiceCollection services,
string authenticationScheme,
PingOneConfigurationAuthentication configuration)
{
if (services == null)
{
throw new ArgumentNullException(nameof(services));
}
PingOneConfigurationValidator.ValidateAuthenticationConfiguration(configuration);
services.AddAuthentication(options =>
{
options.DefaultAuthenticateScheme = CookieAuthenticationDefaults.AuthenticationScheme;
options.DefaultSignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = CookieAuthenticationDefaults.AuthenticationScheme;
})
.AddCookie()
.AddOpenIdConnect(authenticationScheme, options =>
{
options.ClaimsIssuer = authenticationScheme;
options.Authority = $"{configuration.AuthBaseUrl}/{configuration.EnvironmentId}/as";
options.ClientId = configuration.ClientId;
options.ClientSecret = configuration.Secret;
options.CallbackPath = new PathString(configuration.RedirectPath);
options.SaveTokens = true;
options.ResponseType = configuration.ResponseType;
options.Scope.Clear();
foreach (var scope in configuration.Scopes)
{
options.Scope.Add(scope);
}
options.Events = new OpenIdConnectEvents
{
OnRedirectToIdentityProviderForSignOut = context =>
{
context.ProtocolMessage.PostLogoutRedirectUri = configuration.PostSignOffRedirectUrl;
return Task.FromResult(0);
},
};
});
return services;
}
}
}