-
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathWebApplicationExtensions.cs
More file actions
78 lines (68 loc) · 2.84 KB
/
WebApplicationExtensions.cs
File metadata and controls
78 lines (68 loc) · 2.84 KB
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
using Microsoft.AspNetCore.Authentication.Cookies;
using Scalar.AspNetCore;
namespace Microsoft.Extensions.DependencyInjection;
public static class WebApplicationExtensions
{
/// <summary>
/// Registers one OpenAPI document per API version.
/// The generated <c>ApiVersionOpenApiProvider</c> assigns endpoints to the
/// correct version document based on <c>ApiVersionMetadata</c>.
/// </summary>
public static IServiceCollection AddOpenApiDocs(this IServiceCollection services, params string[] versions)
{
foreach (var version in versions)
{
var docName = version.StartsWith("v", StringComparison.OrdinalIgnoreCase) ? version : "v" + version;
services.AddOpenApi(docName);
}
return services;
}
/// <summary>
/// Maps OpenAPI endpoints and Scalar API reference for all version documents.
/// The latest version (last in the array) is shown by default at /scalar.
/// </summary>
public static WebApplication MapOpenApiDocs(this WebApplication app, string title, params string[] versions)
{
app.MapOpenApi();
app.MapScalarApiReference(options =>
{
options.WithTitle(title);
options.SortTagsAlphabetically();
for (int i = 0; i < versions.Length; i++)
{
var docName = versions[i].StartsWith("v", StringComparison.OrdinalIgnoreCase) ? versions[i] : "v" + versions[i];
var isLatest = i == versions.Length - 1;
options.AddDocument(docName, isDefault: isLatest);
}
});
return app;
}
/// <summary>
/// Adds simple cookie authentication for the sample application.
/// Returns 401/403 JSON responses instead of redirecting to a login page.
/// </summary>
public static IServiceCollection AddDemoAuthentication(this IServiceCollection services)
{
services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
.AddCookie(options =>
{
options.Cookie.Name = "ModularMonolith.Auth";
options.Cookie.HttpOnly = true;
options.Cookie.SameSite = SameSiteMode.Strict;
options.ExpireTimeSpan = TimeSpan.FromHours(8);
options.SlidingExpiration = true;
options.Events.OnRedirectToLogin = context =>
{
context.Response.StatusCode = StatusCodes.Status401Unauthorized;
return Task.CompletedTask;
};
options.Events.OnRedirectToAccessDenied = context =>
{
context.Response.StatusCode = StatusCodes.Status403Forbidden;
return Task.CompletedTask;
};
});
services.AddAuthorization();
return services;
}
}