-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathProgram.cs
190 lines (173 loc) · 7.59 KB
/
Program.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
using System.Security.Claims;
using System.Security.Cryptography.X509Certificates;
using GSS.Authentication.CAS;
using GSS.Authentication.CAS.AspNetCore;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Authentication.OpenIdConnect;
using Microsoft.IdentityModel.Protocols.OpenIdConnect;
using Sustainsys.Saml2;
using Sustainsys.Saml2.AspNetCore2;
using Sustainsys.Saml2.Configuration;
using Sustainsys.Saml2.Metadata;
var builder = WebApplication.CreateBuilder(args);
var singleLogout = builder.Configuration.GetValue("CAS:SingleLogout", false);
if (singleLogout)
{
builder.Services.AddDistributedMemoryCache();
var redisConfiguration = builder.Configuration.GetConnectionString("Redis");
if (!string.IsNullOrWhiteSpace(redisConfiguration))
{
builder.Services.AddStackExchangeRedisCache(options => options.Configuration = redisConfiguration);
}
builder.Services.AddSingleton<ITicketStore, DistributedCacheTicketStore>();
builder.Services.AddOptions<CookieAuthenticationOptions>(CookieAuthenticationDefaults.AuthenticationScheme)
.Configure<ITicketStore>((o, t) => o.SessionStore = t);
}
builder.Services.AddRazorPages();
builder.Services.AddAuthorization(options =>
{
// Globally Require Authenticated Users
options.FallbackPolicy = options.DefaultPolicy;
});
builder.Services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
.AddCookie()
.AddCAS(options =>
{
options.CasServerUrlBase = builder.Configuration["CAS:ServerUrlBase"]!;
// required for CasSingleLogoutMiddleware
options.SaveTokens = singleLogout || builder.Configuration.GetValue("CAS:SaveTokens", false);
options.Events.OnCreatingTicket = context =>
{
if (context.Identity == null)
return Task.CompletedTask;
// Map claims from assertion
var assertion = context.Assertion;
context.Identity.AddClaim(new Claim("auth_scheme", CasDefaults.AuthenticationType));
context.Identity.AddClaim(new Claim(ClaimTypes.NameIdentifier, assertion.PrincipalName));
if (assertion.Attributes.TryGetValue("display_name", out var displayName) &&
!string.IsNullOrWhiteSpace(displayName))
{
context.Identity.AddClaim(new Claim(ClaimTypes.Name, displayName!));
}
if (assertion.Attributes.TryGetValue("cn", out var fullName) &&
!string.IsNullOrWhiteSpace(fullName))
{
context.Identity.AddClaim(new Claim(ClaimTypes.Name, fullName!));
}
if (assertion.Attributes.TryGetValue("email", out var email) && !string.IsNullOrWhiteSpace(email))
{
context.Identity.AddClaim(new Claim(ClaimTypes.Email, email!));
}
return Task.CompletedTask;
};
options.Events.OnRemoteFailure = context =>
{
var failure = context.Failure;
if (!string.IsNullOrWhiteSpace(failure?.Message))
{
var logger = context.HttpContext.RequestServices
.GetRequiredService<ILogger<CasAuthenticationHandler>>();
logger.LogError(failure, "{Exception}", failure.Message);
}
context.Response.Redirect("/Account/ExternalLoginFailure");
context.HandleResponse();
return Task.CompletedTask;
};
})
.AddOpenIdConnect(options =>
{
options.ClientId = builder.Configuration["OIDC:ClientId"];
options.ClientSecret = builder.Configuration["OIDC:ClientSecret"];
options.Authority = builder.Configuration["OIDC:Authority"];
options.RequireHttpsMetadata = !builder.Environment.IsDevelopment();
// required for single logout
options.SaveTokens = builder.Configuration.GetValue("OIDC:SaveTokens", false);
options.ResponseType = OpenIdConnectResponseType.Code;
var scope = builder.Configuration["OIDC:Scope"];
if (!string.IsNullOrWhiteSpace(scope))
{
scope.Split(" ", StringSplitOptions.RemoveEmptyEntries).ToList().ForEach(s => options.Scope.Add(s));
}
options.TokenValidationParameters.NameClaimType =
builder.Configuration.GetValue("OIDC:NameClaimType", "name");
options.Events.OnTokenValidated = context =>
{
if (context.Principal?.Identity is ClaimsIdentity claimIdentity)
{
claimIdentity.AddClaim(new Claim("auth_scheme", OpenIdConnectDefaults.AuthenticationScheme));
}
return Task.CompletedTask;
};
options.Events.OnRemoteFailure = context =>
{
var failure = context.Failure;
if (!string.IsNullOrWhiteSpace(failure?.Message))
{
var logger = context.HttpContext.RequestServices.GetRequiredService<ILogger<OpenIdConnectHandler>>();
logger.LogError(failure, "{Exception}", failure.Message);
}
context.Response.Redirect("/Account/ExternalLoginFailure");
context.HandleResponse();
return Task.CompletedTask;
};
})
.AddSaml2(options =>
{
options.SPOptions.EntityId = new EntityId(builder.Configuration["SAML2:SP:EntityId"]);
options.SPOptions.AuthenticateRequestSigningBehavior = SigningBehavior.Never;
var signingCertPath = builder.Configuration["SAML2:SP:SigningCertificate:Path"];
if (!string.IsNullOrWhiteSpace(signingCertPath) && File.Exists(signingCertPath))
{
// required for single logout
options.SPOptions.ServiceCertificates.Add(new ServiceCertificate
{
Use = CertificateUse.Signing,
Certificate = X509CertificateLoader.LoadPkcs12FromFile(
signingCertPath,
builder.Configuration["SAML2:SP:SigningCertificate:Pass"],
X509KeyStorageFlags.Exportable | X509KeyStorageFlags.PersistKeySet)
});
options.SPOptions.AuthenticateRequestSigningBehavior = SigningBehavior.IfIdpWantAuthnRequestsSigned;
}
options.SPOptions.TokenValidationParametersTemplate.NameClaimType = ClaimTypes.NameIdentifier;
options.IdentityProviders.Add(
new IdentityProvider(new EntityId(builder.Configuration["SAML2:IdP:EntityId"]), options.SPOptions)
{
MetadataLocation = builder.Configuration["SAML2:IdP:MetadataLocation"],
});
options.Notifications.AcsCommandResultCreated = (result,_) =>
{
if (result.Principal?.Identity is ClaimsIdentity claimIdentity)
{
claimIdentity.AddClaim(new Claim("auth_scheme", Saml2Defaults.Scheme));
}
};
options.Notifications.MetadataCreated = (metadata, _) =>
{
var ssoDescriptor = metadata.RoleDescriptors.OfType<SpSsoDescriptor>().First();
ssoDescriptor.WantAssertionsSigned = true;
};
// Avoid browsers downloading metadata as file
options.Notifications.MetadataCommandResultCreated = result =>
{
result.ContentType = "application/xml";
result.Headers.Remove("Content-Disposition");
};
});
var app = builder.Build();
// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Error");
}
app.UseHttpsRedirection();
app.MapStaticAssets();
app.UseRouting();
if (singleLogout)
{
app.UseCasSingleLogout();
}
app.UseAuthentication();
app.UseAuthorization();
app.MapRazorPages().WithStaticAssets();
app.Run();