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 System.IO;
using Yavsc.Exceptions;
using Yavsc.Server.Helpers;
using Yavsc.Abstract.Helpers;
[Authorize,Route("api/fs")]
public partial class FileSystemApiController : Controller

View File

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

View File

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

View File

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

View File

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

View File

@ -8,7 +8,7 @@ using Yavsc.ViewModels;
using Yavsc.Models;
using System.Linq;
namespace Yavsc.Helpers
namespace Yavsc.Api.Helpers
{
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

@ -32,7 +32,7 @@ internal class Program
// accepts any access token issued by identity server
// adds an authorization policy for scope 'scope1'
services
.AddAuthorization(options =>
{
@ -40,7 +40,7 @@ internal class Program
{
policy
.RequireAuthenticatedUser()
.RequireClaim(JwtClaimTypes.Scope, new string [] {"scope2"});
.RequireClaim(JwtClaimTypes.Scope, new string[] { "scope2" });
});
})
.AddCors(options =>
@ -63,21 +63,21 @@ internal class Program
options.Authority = "https://localhost:5001";
options.TokenValidationParameters =
new() { ValidateAudience = false };
});
services.AddDbContext<ApplicationDbContext>(options =>
options.UseNpgsql(builder.Configuration.GetConnectionString("Default")));
services.AddTransient<ITrueEmailSender, MailSender>()
.AddTransient<IBillingService, BillingService>()
.AddTransient<ICalendarManager, CalendarManager>();
/*
services.AddSingleton<IConnexionManager, HubConnectionManager>();
services.AddSingleton<ILiveProcessor, LiveProcessor>();
services.AddTransient<IFileSystemAuthManager, FileSystemAuthManager>();
services.AddIdentityApiEndpoints<ApplicationUser>();
services.AddSession();
*/
});
services.AddDbContext<ApplicationDbContext>(options =>
options.UseNpgsql(builder.Configuration.GetConnectionString("DefaultConnection")));
services.AddTransient<ITrueEmailSender, MailSender>()
.AddTransient<IBillingService, BillingService>()
.AddTransient<ICalendarManager, CalendarManager>();
/*
services.AddSingleton<IConnexionManager, HubConnectionManager>();
services.AddSingleton<ILiveProcessor, LiveProcessor>();
services.AddTransient<IFileSystemAuthManager, FileSystemAuthManager>();
services.AddIdentityApiEndpoints<ApplicationUser>();
services.AddSession();
*/
using (var app = builder.Build())
{
if (app.Environment.IsDevelopment())
@ -88,22 +88,23 @@ internal class Program
.UseAuthentication()
.UseAuthorization()
.UseCors("default")
/* .UseEndpoints(endpoints =>
{
endpoints.MapDefaultControllerRoute()
.RequireAuthorization();
})*/
/* .UseEndpoints(endpoints =>
{
endpoints.MapDefaultControllerRoute()
.RequireAuthorization();
})*/
;
// app.MapIdentityApi<ApplicationUser>().RequireAuthorization("ApiScope");
// app.MapIdentityApi<ApplicationUser>().RequireAuthorization("ApiScope");
app.MapDefaultControllerRoute();
app.MapGet("/identity", (HttpContext context) =>
new JsonResult(context?.User?.Claims.Select(c => new { c.Type, c.Value }))
);
// app.UseSession();
// app.UseSession();
await app.RunAsync();
};
}
;

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -3,6 +3,7 @@ using Microsoft.AspNetCore.Mvc;
using System.Threading.Tasks;
using Yavsc.Helpers;
using Yavsc.Models;
using Yavsc.Server.Helpers;
using Yavsc.ViewModels.UserFiles;
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 Microsoft.AspNetCore.Authentication;
JwtSecurityTokenHandler.DefaultMapInboundClaims = false;
@ -36,7 +37,10 @@ builder.Services
options.Scope.Add("openid");
options.Scope.Add("profile");
options.Scope.Add("scope2");
options.MapInboundClaims = true;
options.ClaimActions.MapUniqueJsonKey("preferred_username","preferred_username");
options.ClaimActions.MapUniqueJsonKey("gender", "gender");
options.SaveTokens = true;
});