-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
48 lines (38 loc) · 1.72 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
using ExampleProject;
using ExampleProject.Models;
using ExampleProject.Permissions;
using MicroPermissions;
using Microsoft.AspNetCore.Authentication.Negotiate;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddControllers();
builder.Services.AddRazorPages();
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
builder.Services.AddAuthentication(NegotiateDefaults.AuthenticationScheme)
.AddNegotiate();
// Register MicroPermissions
builder.Services.AddMicroPermissions<MyPermissionContext>(new PermissionControllerOptions
{
ThrowIfUnhandled = true, // when call permissionController it ensures permission is granted explicitly, otherwise throw exception
});
builder.Services.AddHttpContextAccessor(); // we're adding IHttpAncestor for demo purposes, required for our custom MyPermissionContext.
builder.Services.AddScoped<MyPermissionContext>(); // add permission context to IoC
builder.Services.AddSingleton<TaskRepository>();
// register handlers & filters
builder.Services.AddTransient<IPermissionHandler<MyPermissionContext, IncrementRequest>, IncrementRequestPermissionHandler>();
builder.Services.AddTransient<IPermissionHandler<MyPermissionContext, CreateTaskRequest>, CreateTaskPermissionHandler>();
builder.Services.AddTransient<IPermissionFilter<MyPermissionContext, IQueryable<TaskEntity>>, TaskPermissionFilter>();
builder.Services.AddAuthorization(options => { options.FallbackPolicy = options.DefaultPolicy; });
var app = builder.Build();
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseAuthentication();
app.UseAuthorization();
app.MapRazorPages();
app.MapControllers();
app.Run();