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()
|
||||
{
|
||||
|
28
src/Api/Helpers/RequestHelpers.cs
Normal file
28
src/Api/Helpers/RequestHelpers.cs
Normal file
@ -0,0 +1,28 @@
|
||||
using System.Collections.Generic;
|
||||
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using System.Threading.Tasks;
|
||||
using Newtonsoft.Json;
|
||||
using Yavsc.ViewModels;
|
||||
using Yavsc.Models;
|
||||
using System.Linq;
|
||||
|
||||
namespace Yavsc.Api.Helpers
|
||||
{
|
||||
public static class RequestHelpers
|
||||
{
|
||||
// Check for some apache proxy header, if any
|
||||
public static string ForHost(this HttpRequest request) {
|
||||
string host = request.Headers["X-Forwarded-For"];
|
||||
if (string.IsNullOrEmpty(host)) {
|
||||
host = request.Host.Value;
|
||||
} else { // Using X-Forwarded-For last address
|
||||
host = host.Split(',')
|
||||
.Last()
|
||||
.Trim();
|
||||
}
|
||||
return host;
|
||||
}
|
||||
}
|
||||
}
|
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();
|
||||
};
|
||||
}
|
||||
;
|
||||
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user