Get User Id from Web API

This commit is contained in:
Paul Schneider
2025-02-17 23:56:28 +00:00
parent 84e58bb9eb
commit 45cc299866
26 changed files with 83 additions and 60 deletions

View File

@ -14,6 +14,8 @@ namespace Yavsc.ApiControllers
using Yavsc.Attributes.Validation; using Yavsc.Attributes.Validation;
using System.IO; using System.IO;
using Yavsc.Exceptions; using Yavsc.Exceptions;
using Yavsc.Server.Helpers;
using Yavsc.Abstract.Helpers;
[Authorize,Route("api/fs")] [Authorize,Route("api/fs")]
public partial class FileSystemApiController : Controller public partial class FileSystemApiController : Controller

View File

@ -8,6 +8,7 @@ using Yavsc.Models;
using Yavsc.Models.Messaging; using Yavsc.Models.Messaging;
using Yavsc.Services; using Yavsc.Services;
using Microsoft.AspNetCore.SignalR; using Microsoft.AspNetCore.SignalR;
using Yavsc.Server.Helpers;
namespace Yavsc.ApiControllers namespace Yavsc.ApiControllers
{ {

View File

@ -6,7 +6,7 @@ using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using Yavsc.Helpers; using Yavsc.Api.Helpers;
using Yavsc.Models; using Yavsc.Models;
using Yavsc.Models.Workflow; using Yavsc.Models.Workflow;

View File

@ -15,6 +15,7 @@ namespace Yavsc.ApiControllers
using Microsoft.Extensions.Options; using Microsoft.Extensions.Options;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using Yavsc.ViewModels.Auth; using Yavsc.ViewModels.Auth;
using Yavsc.Server.Helpers;
[Route("api/bill"), Authorize] [Route("api/bill"), Authorize]
public class BillingController : Controller public class BillingController : Controller

View File

@ -1,15 +1,11 @@
using Microsoft.AspNetCore.Identity; using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using System.Security.Claims;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using Yavsc.Models; using Yavsc.Models;
using Yavsc.Models.Account; using Yavsc.Api.Helpers;
using Yavsc.ViewModels.Account; using Yavsc.Server.Helpers;
using Yavsc.Helpers;
using Yavsc.Abstract.Identity;
using System.Diagnostics;
namespace Yavsc.WebApi.Controllers namespace Yavsc.WebApi.Controllers
{ {
@ -27,8 +23,6 @@ namespace Yavsc.WebApi.Controllers
_dbContext = dbContext; _dbContext = dbContext;
} }
[HttpGet("me")] [HttpGet("me")]
public async Task<IActionResult> Me() public async Task<IActionResult> Me()
{ {

View File

@ -8,7 +8,7 @@ using Yavsc.ViewModels;
using Yavsc.Models; using Yavsc.Models;
using System.Linq; using System.Linq;
namespace Yavsc.Helpers namespace Yavsc.Api.Helpers
{ {
public static class RequestHelpers public static class RequestHelpers
{ {

View File

@ -0,0 +1,17 @@
using System.Security.Claims;
using System.Collections.Generic;
using System.Linq;
using Microsoft.EntityFrameworkCore;
using Yavsc.Models;
using Yavsc.Models.Blog;
namespace Yavsc.Api.Helpers
{
public static class UserHelpers
{
public static string GetUserId(this ClaimsPrincipal user)
{
return user.FindFirstValue("http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier");
}
}
}

View File

@ -40,7 +40,7 @@ internal class Program
{ {
policy policy
.RequireAuthenticatedUser() .RequireAuthenticatedUser()
.RequireClaim(JwtClaimTypes.Scope, new string [] {"scope2"}); .RequireClaim(JwtClaimTypes.Scope, new string[] { "scope2" });
}); });
}) })
.AddCors(options => .AddCors(options =>
@ -65,19 +65,19 @@ internal class Program
new() { ValidateAudience = false }; new() { ValidateAudience = false };
}); });
services.AddDbContext<ApplicationDbContext>(options => services.AddDbContext<ApplicationDbContext>(options =>
options.UseNpgsql(builder.Configuration.GetConnectionString("Default"))); options.UseNpgsql(builder.Configuration.GetConnectionString("DefaultConnection")));
services.AddTransient<ITrueEmailSender, MailSender>() services.AddTransient<ITrueEmailSender, MailSender>()
.AddTransient<IBillingService, BillingService>() .AddTransient<IBillingService, BillingService>()
.AddTransient<ICalendarManager, CalendarManager>(); .AddTransient<ICalendarManager, CalendarManager>();
/* /*
services.AddSingleton<IConnexionManager, HubConnectionManager>(); services.AddSingleton<IConnexionManager, HubConnectionManager>();
services.AddSingleton<ILiveProcessor, LiveProcessor>(); services.AddSingleton<ILiveProcessor, LiveProcessor>();
services.AddTransient<IFileSystemAuthManager, FileSystemAuthManager>(); services.AddTransient<IFileSystemAuthManager, FileSystemAuthManager>();
services.AddIdentityApiEndpoints<ApplicationUser>(); services.AddIdentityApiEndpoints<ApplicationUser>();
services.AddSession(); services.AddSession();
*/ */
using (var app = builder.Build()) using (var app = builder.Build())
{ {
if (app.Environment.IsDevelopment()) if (app.Environment.IsDevelopment())
@ -88,22 +88,23 @@ internal class Program
.UseAuthentication() .UseAuthentication()
.UseAuthorization() .UseAuthorization()
.UseCors("default") .UseCors("default")
/* .UseEndpoints(endpoints => /* .UseEndpoints(endpoints =>
{ {
endpoints.MapDefaultControllerRoute() endpoints.MapDefaultControllerRoute()
.RequireAuthorization(); .RequireAuthorization();
})*/ })*/
; ;
// app.MapIdentityApi<ApplicationUser>().RequireAuthorization("ApiScope"); // app.MapIdentityApi<ApplicationUser>().RequireAuthorization("ApiScope");
app.MapDefaultControllerRoute(); app.MapDefaultControllerRoute();
app.MapGet("/identity", (HttpContext context) => app.MapGet("/identity", (HttpContext context) =>
new JsonResult(context?.User?.Claims.Select(c => new { c.Type, c.Value })) new JsonResult(context?.User?.Claims.Select(c => new { c.Type, c.Value }))
); );
// app.UseSession(); // app.UseSession();
await app.RunAsync(); await app.RunAsync();
}; }
;

View File

@ -1,7 +1,7 @@
using System; using System;
using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations;
using Yavsc.Helpers; using Yavsc.Server.Helpers;
namespace Yavsc.Attributes.Validation namespace Yavsc.Attributes.Validation
{ {

View File

@ -4,7 +4,7 @@ using System.Linq;
using System.Text; using System.Text;
using Yavsc.ViewModels.UserFiles; using Yavsc.ViewModels.UserFiles;
namespace Yavsc.Helpers namespace Yavsc.Server.Helpers
{ {
public static class AbstractFileSystemHelpers public static class AbstractFileSystemHelpers
{ {

View File

@ -1,4 +1,4 @@
namespace Yavsc.Helpers namespace Yavsc.Abstract.Helpers
{ {
public enum ErrorCode { public enum ErrorCode {
NotFound, NotFound,

View File

@ -2,7 +2,7 @@ using System;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using Yavsc.Abstract.FileSystem; using Yavsc.Abstract.FileSystem;
using Yavsc.Helpers; using Yavsc.Server.Helpers;
namespace Yavsc.ViewModels.UserFiles namespace Yavsc.ViewModels.UserFiles
{ {

View File

@ -1,10 +1,7 @@
using System.Collections.Generic;
using System.Globalization; using System.Globalization;
using System.IO;
using System.Linq;
using Yavsc.Abstract.FileSystem;
using Yavsc.Billing; using Yavsc.Billing;
using Yavsc.Models.Billing; using Yavsc.Models.Billing;
using Yavsc.Server.Helpers;
using Yavsc.Services; using Yavsc.Services;
namespace Yavsc.Helpers namespace Yavsc.Helpers

View File

@ -11,8 +11,9 @@ using SixLabors.ImageSharp;
using SixLabors.ImageSharp.Processing; using SixLabors.ImageSharp.Processing;
using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Http;
using Yavsc.Exceptions; using Yavsc.Exceptions;
using Yavsc.Helpers;
namespace Yavsc.Helpers using Yavsc.Abstract.Helpers;
namespace Yavsc.Server.Helpers
{ {
public static class FileSystemHelpers public static class FileSystemHelpers
{ {

View File

@ -9,7 +9,6 @@ namespace Yavsc.Helpers
{ {
public static class UserHelpers public static class UserHelpers
{ {
public static string GetUserId(this ClaimsPrincipal user) public static string GetUserId(this ClaimsPrincipal user)
{ {
return user.FindFirstValue("sub"); return user.FindFirstValue("sub");

View File

@ -10,6 +10,7 @@ using Newtonsoft.Json;
using Yavsc.Helpers; using Yavsc.Helpers;
using Yavsc.Models; using Yavsc.Models;
using Yavsc.Models.FileSystem; using Yavsc.Models.FileSystem;
using Yavsc.Server.Helpers;
namespace Yavsc.ViewModels.Streaming namespace Yavsc.ViewModels.Streaming
{ {

View File

@ -12,6 +12,7 @@ namespace Yavsc.Controllers
using Models.Billing; using Models.Billing;
using Models.Workflow; using Models.Workflow;
using ViewModels.Auth; using ViewModels.Auth;
using Yavsc.Server.Helpers;
[Authorize] [Authorize]
public class EstimateController : Controller public class EstimateController : Controller

View File

@ -10,6 +10,7 @@ namespace Yavsc.Controllers
using Microsoft.Extensions.Localization; using Microsoft.Extensions.Localization;
using Models; using Models;
using ViewModels.FrontOffice; using ViewModels.FrontOffice;
using Yavsc.Server.Helpers;
using Yavsc.Services; using Yavsc.Services;
public class FrontOfficeController : Controller public class FrontOfficeController : Controller

View File

@ -1,6 +1,7 @@
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using Yavsc.Helpers; using Yavsc.Helpers;
using Yavsc.Server.Helpers;
namespace Yavsc.Controllers namespace Yavsc.Controllers
{ {

View File

@ -29,6 +29,7 @@ using Yavsc.Models.Workflow;
using Yavsc.Services; using Yavsc.Services;
using Yavsc.Settings; using Yavsc.Settings;
using Yavsc.ViewModels.Auth; using Yavsc.ViewModels.Auth;
using Yavsc.Server.Helpers;
namespace Yavsc.Extensions; namespace Yavsc.Extensions;
@ -335,7 +336,6 @@ public static class HostingExtensions
.AddInMemoryIdentityResources(Config.IdentityResources) .AddInMemoryIdentityResources(Config.IdentityResources)
.AddInMemoryClients(Config.Clients) .AddInMemoryClients(Config.Clients)
.AddInMemoryApiScopes(Config.ApiScopes) .AddInMemoryApiScopes(Config.ApiScopes)
.AddAspNetIdentity<ApplicationUser>() .AddAspNetIdentity<ApplicationUser>()
.AddProfileService<ProfileService>() .AddProfileService<ProfileService>()
; ;

View File

@ -0,0 +1,10 @@
using System.Security.Claims;
namespace Yavsc.Helpers
{
public static class UserHelpers
{
}
}

View File

@ -2,6 +2,7 @@ using Microsoft.AspNetCore.Authorization;
using Microsoft.Extensions.Localization; using Microsoft.Extensions.Localization;
using Microsoft.Extensions.Options; using Microsoft.Extensions.Options;
using Yavsc.Helpers; using Yavsc.Helpers;
using Yavsc.Server.Helpers;
using Yavsc.Settings; using Yavsc.Settings;
namespace Yavsc; namespace Yavsc;

View File

@ -8,6 +8,7 @@ using Yavsc.ViewModels;
using Yavsc.ViewModels.Gen; using Yavsc.ViewModels.Gen;
using Yavsc.Services; using Yavsc.Services;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using Yavsc.Server.Helpers;
namespace Yavsc.ViewComponents namespace Yavsc.ViewComponents
{ {

View File

@ -3,6 +3,7 @@ using Microsoft.AspNetCore.Mvc;
using System.Threading.Tasks; using System.Threading.Tasks;
using Yavsc.Helpers; using Yavsc.Helpers;
using Yavsc.Models; using Yavsc.Models;
using Yavsc.Server.Helpers;
using Yavsc.ViewModels.UserFiles; using Yavsc.ViewModels.UserFiles;
namespace Yavsc.ViewComponents namespace Yavsc.ViewComponents

View File

@ -1,11 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-9.0.0.0" newVersion="9.0.0.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>

View File

@ -11,6 +11,7 @@
*/ */
using System.IdentityModel.Tokens.Jwt; using System.IdentityModel.Tokens.Jwt;
using Microsoft.AspNetCore.Authentication;
JwtSecurityTokenHandler.DefaultMapInboundClaims = false; JwtSecurityTokenHandler.DefaultMapInboundClaims = false;
@ -36,6 +37,9 @@ builder.Services
options.Scope.Add("openid"); options.Scope.Add("openid");
options.Scope.Add("profile"); options.Scope.Add("profile");
options.Scope.Add("scope2"); options.Scope.Add("scope2");
options.MapInboundClaims = true;
options.ClaimActions.MapUniqueJsonKey("preferred_username","preferred_username");
options.ClaimActions.MapUniqueJsonKey("gender", "gender");
options.SaveTokens = true; options.SaveTokens = true;
}); });