Get User Id from Web API
This commit is contained in:
@ -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
|
||||
|
@ -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
|
||||
{
|
||||
|
@ -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;
|
||||
|
||||
|
@ -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
|
||||
|
@ -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()
|
||||
{
|
||||
|
@ -8,7 +8,7 @@ using Yavsc.ViewModels;
|
||||
using Yavsc.Models;
|
||||
using System.Linq;
|
||||
|
||||
namespace Yavsc.Helpers
|
||||
namespace Yavsc.Api.Helpers
|
||||
{
|
||||
public static class RequestHelpers
|
||||
{
|
17
src/Api/Helpers/UserHelpers.cs
Normal file
17
src/Api/Helpers/UserHelpers.cs
Normal 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");
|
||||
}
|
||||
}
|
||||
}
|
@ -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();
|
||||
};
|
||||
}
|
||||
;
|
||||
|
||||
|
||||
|
||||
|
@ -1,7 +1,7 @@
|
||||
|
||||
using System;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using Yavsc.Helpers;
|
||||
using Yavsc.Server.Helpers;
|
||||
|
||||
namespace Yavsc.Attributes.Validation
|
||||
{
|
||||
|
@ -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
|
||||
{
|
||||
|
@ -1,4 +1,4 @@
|
||||
namespace Yavsc.Helpers
|
||||
namespace Yavsc.Abstract.Helpers
|
||||
{
|
||||
public enum ErrorCode {
|
||||
NotFound,
|
||||
|
@ -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
|
||||
{
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
{
|
||||
|
@ -9,7 +9,6 @@ namespace Yavsc.Helpers
|
||||
{
|
||||
public static class UserHelpers
|
||||
{
|
||||
|
||||
public static string GetUserId(this ClaimsPrincipal user)
|
||||
{
|
||||
return user.FindFirstValue("sub");
|
||||
|
@ -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
|
||||
{
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
@ -1,6 +1,7 @@
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Yavsc.Helpers;
|
||||
using Yavsc.Server.Helpers;
|
||||
|
||||
namespace Yavsc.Controllers
|
||||
{
|
||||
|
@ -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>()
|
||||
;
|
||||
|
10
src/Yavsc/Helpers/UserHelpers.cs
Normal file
10
src/Yavsc/Helpers/UserHelpers.cs
Normal file
@ -0,0 +1,10 @@
|
||||
using System.Security.Claims;
|
||||
|
||||
namespace Yavsc.Helpers
|
||||
{
|
||||
public static class UserHelpers
|
||||
{
|
||||
|
||||
|
||||
}
|
||||
}
|
@ -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;
|
||||
|
@ -8,6 +8,7 @@ using Yavsc.ViewModels;
|
||||
using Yavsc.ViewModels.Gen;
|
||||
using Yavsc.Services;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Yavsc.Server.Helpers;
|
||||
|
||||
namespace Yavsc.ViewComponents
|
||||
{
|
||||
|
@ -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
|
||||
|
@ -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>
|
@ -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;
|
||||
});
|
||||
|
||||
|
Reference in New Issue
Block a user