-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathAuthenticationSnippets.cs
More file actions
44 lines (39 loc) · 1.29 KB
/
AuthenticationSnippets.cs
File metadata and controls
44 lines (39 loc) · 1.29 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
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.DependencyInjection;
using ServiceComposer.AspNetCore;
namespace Snippets.Authentication;
// begin-snippet: multiple-handlers-different-auth-requirements
public class SalesHandler : ICompositionRequestsHandler
{
[Authorize]
[HttpGet("/product/{id}")]
public Task Handle(HttpRequest request) { /* ... */ return Task.CompletedTask; }
}
public class InventoryHandler : ICompositionRequestsHandler
{
[Authorize(Policy = "WarehouseStaff")]
[HttpGet("/product/{id}")]
public Task Handle(HttpRequest request) { /* ... */ return Task.CompletedTask; }
}
// end-snippet
static class AuthSetupSnippets
{
static void ShowSetup()
{
// begin-snippet: auth-middleware-setup
var builder = WebApplication.CreateBuilder();
builder.Services.AddViewModelComposition();
builder.Services.AddAuthentication(); // configure your scheme here
builder.Services.AddAuthorization();
var app = builder.Build();
app.UseAuthentication();
app.UseAuthorization();
app.MapCompositionHandlers();
app.Run();
// end-snippet
}
}