Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions samples/ContractApi/Controllers.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using JsonApiToolkit.Attributes;
using JsonApiToolkit.Controllers;
using JsonApiToolkit.Models.Errors;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;

Expand Down Expand Up @@ -76,6 +77,7 @@ public async Task<IActionResult> UpdateAsync(int id, UpdateArticleDto dto)
}

[HttpDelete("{id:int}")]
[Authorize]
public async Task<IActionResult> DeleteAsync(int id)
{
var article =
Expand Down
9 changes: 9 additions & 0 deletions samples/ContractApi/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@
var builder = WebApplication.CreateBuilder(args);

builder.Services.AddDbContext<ContractDbContext>(o => o.UseInMemoryDatabase("contract-api"));
builder
.Services.AddAuthentication("Test")
.AddScheme<Microsoft.AspNetCore.Authentication.AuthenticationSchemeOptions, TestAuthHandler>(
"Test",
null
);
builder.Services.AddAuthorization();
builder.Services.AddControllers();
builder.Services.AddJsonApiToolkit(o =>
{
Expand All @@ -17,6 +24,8 @@
});

var app = builder.Build();
app.UseAuthentication();
app.UseAuthorization();
app.MapControllers();

using (var scope = app.Services.CreateScope())
Expand Down
22 changes: 22 additions & 0 deletions samples/ContractApi/TestAuthHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System.Security.Claims;
using System.Text.Encodings.Web;
using Microsoft.AspNetCore.Authentication;
using Microsoft.Extensions.Options;

namespace ContractApi;

public class TestAuthHandler(
IOptionsMonitor<AuthenticationSchemeOptions> options,
ILoggerFactory logger,
UrlEncoder encoder
) : AuthenticationHandler<AuthenticationSchemeOptions>(options, logger, encoder)
{
protected override Task<AuthenticateResult> HandleAuthenticateAsync()
{
var claims = new[] { new Claim(ClaimTypes.Name, "test-user") };
var identity = new ClaimsIdentity(claims, "Test");
var principal = new ClaimsPrincipal(identity);
var ticket = new AuthenticationTicket(principal, "Test");
return Task.FromResult(AuthenticateResult.Success(ticket));
}
}