Merge pull request #149 from dotnet/auth2

Fix auth sample
pull/150/head
Rick Anderson 2023-05-30 13:29:24 -10:00 committed by GitHub
commit 1b45afc4e4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 4 additions and 5 deletions

View File

@ -1,6 +1,5 @@
using AuthRequirementsData.Authorization;
using Microsoft.AspNetCore.Mvc;
using System.Security.Claims;
namespace AuthRequirementsData.Controllers;
@ -10,6 +9,6 @@ public class GreetingsController : Controller
{
[MinimumAgeAuthorize(16)]
[HttpGet("hello")]
public string Hello(ClaimsPrincipal user) =>
$"Hello {(user.Identity?.Name ?? "world")}!";
public string Hello() => $"Hello {(HttpContext.User.Identity?.Name ?? "world")}!";
}

View File

@ -5,11 +5,11 @@ var builder = WebApplication.CreateBuilder();
builder.Services.AddAuthentication().AddJwtBearer();
builder.Services.AddAuthorization();
builder.Services.AddControllers();
builder.Services.AddSingleton<IAuthorizationHandler, MinimumAgeAuthorizationHandler>();
builder.Services.AddControllers();
var app = builder.Build();
app.MapControllers();
app.Run();
app.Run();