diff --git a/src/CSharp/EasyMicroservices.Cores.AspCoreApi/Authorizations/AspCoreAuthorization.cs b/src/CSharp/EasyMicroservices.Cores.AspCoreApi/Authorizations/AspCoreAuthorization.cs index 67ffdea..1c6d5ed 100644 --- a/src/CSharp/EasyMicroservices.Cores.AspCoreApi/Authorizations/AspCoreAuthorization.cs +++ b/src/CSharp/EasyMicroservices.Cores.AspCoreApi/Authorizations/AspCoreAuthorization.cs @@ -7,6 +7,7 @@ using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Http.Features; using Microsoft.AspNetCore.Mvc.Controllers; +using Microsoft.Extensions.Primitives; using System; using System.Collections.Concurrent; using System.Collections.Generic; @@ -112,7 +113,14 @@ MessageContract IsAnonymousMethodCalling(HttpContext httpContext) return (FailedReasonType.Nothing, "controllerActionDescriptor is null or empty, did you sent correct route to me?"); if (controllerActionDescriptor.ControllerTypeInfo.GetCustomAttributes(typeof(AllowAnonymousAttribute)).Any() || controllerActionDescriptor.MethodInfo.GetCustomAttributes(typeof(AllowAnonymousAttribute)).Any()) - return true; + return true; + + if (httpContext.Request.Headers.TryGetValue("Authorization", out StringValues authAvalue)) + { + var ownerPat = BaseUnitOfWork.GetFullAccessPersonalAccessToken(); + if (authAvalue.Any(x => x.Replace("Bearer ", "").Equals(ownerPat))) + return true; + } return false; } diff --git a/src/CSharp/EasyMicroservices.Cores.AspCoreApi/EasyMicroservices.Cores.AspCoreApi.csproj b/src/CSharp/EasyMicroservices.Cores.AspCoreApi/EasyMicroservices.Cores.AspCoreApi.csproj index 3f03677..791f2c7 100644 --- a/src/CSharp/EasyMicroservices.Cores.AspCoreApi/EasyMicroservices.Cores.AspCoreApi.csproj +++ b/src/CSharp/EasyMicroservices.Cores.AspCoreApi/EasyMicroservices.Cores.AspCoreApi.csproj @@ -4,7 +4,7 @@ netstandard2.1;net6.0;net7.0;net8.0 AnyCPU;x64;x86 EasyMicroservices - 0.0.1.42 + 0.0.1.43 asp core servces. EasyMicroservices@gmail.com core,cores,base,database,services,asp,aspnet diff --git a/src/CSharp/EasyMicroservices.Cores.AspEntityFrameworkCoreApi/EasyMicroservices.Cores.AspEntityFrameworkCoreApi.csproj b/src/CSharp/EasyMicroservices.Cores.AspEntityFrameworkCoreApi/EasyMicroservices.Cores.AspEntityFrameworkCoreApi.csproj index 2607982..f41b3d7 100644 --- a/src/CSharp/EasyMicroservices.Cores.AspEntityFrameworkCoreApi/EasyMicroservices.Cores.AspEntityFrameworkCoreApi.csproj +++ b/src/CSharp/EasyMicroservices.Cores.AspEntityFrameworkCoreApi/EasyMicroservices.Cores.AspEntityFrameworkCoreApi.csproj @@ -4,7 +4,7 @@ net6.0;net7.0;net8.0 AnyCPU;x64;x86 EasyMicroservices - 0.0.1.42 + 0.0.1.43 asp core servces. EasyMicroservices@gmail.com core,cores,base,database,services,asp,aspnet,aspcore,efcore diff --git a/src/CSharp/EasyMicroservices.Cores.AspEntityFrameworkCoreApi/Managers/WhiteLabelManager.cs b/src/CSharp/EasyMicroservices.Cores.AspEntityFrameworkCoreApi/Managers/WhiteLabelManager.cs index c8b9013..8fdeb8f 100644 --- a/src/CSharp/EasyMicroservices.Cores.AspEntityFrameworkCoreApi/Managers/WhiteLabelManager.cs +++ b/src/CSharp/EasyMicroservices.Cores.AspEntityFrameworkCoreApi/Managers/WhiteLabelManager.cs @@ -1,14 +1,17 @@ using EasyMicroservices.Cores.AspEntityFrameworkCoreApi; +using EasyMicroservices.Cores.AspEntityFrameworkCoreApi.Interfaces; using EasyMicroservices.Cores.Database.Managers; using EasyMicroservices.Cores.Models; using EasyMicroservices.Database.Interfaces; using EasyMicroservices.ServiceContracts; using Microsoft.AspNetCore.Http; using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.DependencyInjection; using System; using System.Collections.Generic; using System.Linq; using System.Net.Http; +using System.Text; using System.Threading; using System.Threading.Tasks; using WhiteLables.GeneratedServices; @@ -64,6 +67,7 @@ string GetDefaultUniqueIdentity(ICollection whiteLables, lon } static SemaphoreSlim SemaphoreSlim { get; set; } = new SemaphoreSlim(1); + static HttpClient WhiteLabelHttpClient = new HttpClient(); /// /// /// @@ -84,16 +88,18 @@ public async Task Initialize(IHttpContextAccessor httpContext, s Console.WriteLine($"WhiteLabelManager Initialize! {microserviceName} {whiteLableRoute}"); if (dbContextTypes.IsEmpty()) return CurrentWhiteLabel; - HttpClient httpClient = new HttpClient(); - if (httpContext?.HttpContext != null && httpContext.HttpContext.Request.Headers.TryGetValue("Authorization", out var authorizationHeader)) - { - httpClient.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", authorizationHeader.ToString().Replace("Bearer ", "")); - } - var whiteLabelClient = new WhiteLables.GeneratedServices.WhiteLabelClient(whiteLableRoute, httpClient); + var unitOfWork = _serviceProvider.GetService(); + var ownerPat = unitOfWork.GetFullAccessPersonalAccessToken(); + if (ownerPat.HasValue()) + WhiteLabelHttpClient.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", ownerPat); + else if (httpContext?.HttpContext != null && httpContext.HttpContext.Request.Headers.TryGetValue("Authorization", out var authorizationHeader)) + WhiteLabelHttpClient.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", authorizationHeader.ToString().Replace("Bearer ", "")); + + var whiteLabelClient = new WhiteLables.GeneratedServices.WhiteLabelClient(whiteLableRoute, WhiteLabelHttpClient); var whiteLabels = await whiteLabelClient.GetAllAsync().AsCheckedResult(x => x.Result).ConfigureAwait(false); var defaultUniqueIdentity = GetDefaultUniqueIdentity(whiteLabels, null); - var microserviceClient = new WhiteLables.GeneratedServices.MicroserviceClient(whiteLableRoute, httpClient); + var microserviceClient = new WhiteLables.GeneratedServices.MicroserviceClient(whiteLableRoute, WhiteLabelHttpClient); var microservices = await microserviceClient.GetAllAsync().ConfigureAwait(false); var foundMicroservice = microservices.Result.FirstOrDefault(x => x.Name.Equals(microserviceName, StringComparison.OrdinalIgnoreCase)); if (foundMicroservice == null) @@ -117,7 +123,7 @@ public async Task Initialize(IHttpContextAccessor httpContext, s var uniqueIdentityManager = new UnitOfWork(_serviceProvider).GetUniqueIdentityManager() as DefaultUniqueIdentityManager; - var microserviceContextTableClient = new WhiteLables.GeneratedServices.MicroserviceContextTableClient(whiteLableRoute, httpClient); + var microserviceContextTableClient = new WhiteLables.GeneratedServices.MicroserviceContextTableClient(whiteLableRoute, WhiteLabelHttpClient); var microserviceContextTables = await microserviceContextTableClient.GetAllAsync().ConfigureAwait(false); HashSet addedInWhitLabels = new HashSet(); @@ -129,7 +135,7 @@ public async Task Initialize(IHttpContextAccessor httpContext, s foreach (var contextType in dbContextTypes) { - var contextTableClient = new WhiteLables.GeneratedServices.ContextTableClient(whiteLableRoute, httpClient); + var contextTableClient = new WhiteLables.GeneratedServices.ContextTableClient(whiteLableRoute, WhiteLabelHttpClient); var contextTables = await contextTableClient.GetAllAsync().ConfigureAwait(false); using var instanceOfContext = _serviceProvider.GetService(contextType) as DbContext; string contextName = uniqueIdentityManager.GetContextName(contextType); diff --git a/src/CSharp/EasyMicroservices.Cores.AspEntityFrameworkCoreApi/UnitOfWork.cs b/src/CSharp/EasyMicroservices.Cores.AspEntityFrameworkCoreApi/UnitOfWork.cs index 333d167..6609ffb 100644 --- a/src/CSharp/EasyMicroservices.Cores.AspEntityFrameworkCoreApi/UnitOfWork.cs +++ b/src/CSharp/EasyMicroservices.Cores.AspEntityFrameworkCoreApi/UnitOfWork.cs @@ -1,4 +1,5 @@ -using EasyMicroservices.Cores.AspCoreApi.Interfaces; +using Authentications.GeneratedServices; +using EasyMicroservices.Cores.AspCoreApi.Interfaces; using EasyMicroservices.Cores.AspCoreApi.Managers; using EasyMicroservices.Cores.AspEntityFrameworkCoreApi.Interfaces; using EasyMicroservices.Cores.Database.Interfaces; @@ -15,6 +16,7 @@ using EasyMicroservices.Mapper.SerializerMapper.Providers; using EasyMicroservices.Serialization.Interfaces; using EasyMicroservices.Serialization.Newtonsoft.Json.Providers; +using EasyMicroservices.ServiceContracts; using Microsoft.AspNetCore.Http; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; @@ -22,6 +24,7 @@ using System; using System.Collections.Generic; using System.Linq; +using System.Net.Http; using System.Security.Claims; using System.Text; using System.Threading.Tasks; @@ -528,6 +531,30 @@ public virtual Task HasUniqueIdentityRole() return auth.HasUnlimitedPermission(ServiceProvider.GetService()?.HttpContext); } + + /// + /// + /// + /// + public string GetFullAccessPersonalAccessToken() + { + var config = GetConfiguration(); + var section = config?.GetSection("Authorization"); + if (section == null) + return null; + return section.GetValue("FullAccessPAT"); + } + + /// + /// + /// + /// + /// + public T GetService() + { + return ServiceProvider.GetService(); + } + /// /// /// diff --git a/src/CSharp/EasyMicroservices.Cores.Clients/EasyMicroservices.Cores.Clients.csproj b/src/CSharp/EasyMicroservices.Cores.Clients/EasyMicroservices.Cores.Clients.csproj index d3a87a7..7713e19 100644 --- a/src/CSharp/EasyMicroservices.Cores.Clients/EasyMicroservices.Cores.Clients.csproj +++ b/src/CSharp/EasyMicroservices.Cores.Clients/EasyMicroservices.Cores.Clients.csproj @@ -4,7 +4,7 @@ netstandard2.0;netstandard2.1;net6.0;net8.0 AnyCPU;x64;x86 EasyMicroservices - 0.0.1.42 + 0.0.1.43 core of database. EasyMicroservices@gmail.com core,cores,base,client,clients diff --git a/src/CSharp/EasyMicroservices.Cores.Contracts/EasyMicroservices.Cores.Contracts.csproj b/src/CSharp/EasyMicroservices.Cores.Contracts/EasyMicroservices.Cores.Contracts.csproj index 8689912..d43e37d 100644 --- a/src/CSharp/EasyMicroservices.Cores.Contracts/EasyMicroservices.Cores.Contracts.csproj +++ b/src/CSharp/EasyMicroservices.Cores.Contracts/EasyMicroservices.Cores.Contracts.csproj @@ -4,7 +4,7 @@ netstandard2.0;netstandard2.1;net45;net6.0;net8.0 AnyCPU;x64;x86 EasyMicroservices - 0.0.1.42 + 0.0.1.43 core contracts. EasyMicroservices@gmail.com core,cores,base,contract,contracts,dto,dtos diff --git a/src/CSharp/EasyMicroservices.Cores.Database/EasyMicroservices.Cores.Database.csproj b/src/CSharp/EasyMicroservices.Cores.Database/EasyMicroservices.Cores.Database.csproj index 9a2c385..5253481 100644 --- a/src/CSharp/EasyMicroservices.Cores.Database/EasyMicroservices.Cores.Database.csproj +++ b/src/CSharp/EasyMicroservices.Cores.Database/EasyMicroservices.Cores.Database.csproj @@ -4,7 +4,7 @@ netstandard2.0;netstandard2.1;net45;net6.0;net8.0 AnyCPU;x64;x86 EasyMicroservices - 0.0.1.42 + 0.0.1.43 core of database. EasyMicroservices@gmail.com core,cores,base,database diff --git a/src/CSharp/EasyMicroservices.Cores.Database/Interfaces/IBaseUnitOfWork.cs b/src/CSharp/EasyMicroservices.Cores.Database/Interfaces/IBaseUnitOfWork.cs index 1cecad8..d2d68c4 100644 --- a/src/CSharp/EasyMicroservices.Cores.Database/Interfaces/IBaseUnitOfWork.cs +++ b/src/CSharp/EasyMicroservices.Cores.Database/Interfaces/IBaseUnitOfWork.cs @@ -44,6 +44,18 @@ public interface IBaseUnitOfWork : IDisposable /// /// IUniqueIdentityManager GetUniqueIdentityManager(); + /// + /// + /// + /// + string GetFullAccessPersonalAccessToken(); + /// + /// + /// + /// + /// + T GetService(); + /// /// /// diff --git a/src/CSharp/EasyMicroservices.Cores.EntityFrameworkCore/EasyMicroservices.Cores.EntityFrameworkCore.csproj b/src/CSharp/EasyMicroservices.Cores.EntityFrameworkCore/EasyMicroservices.Cores.EntityFrameworkCore.csproj index 7f4cc03..44fe2a9 100644 --- a/src/CSharp/EasyMicroservices.Cores.EntityFrameworkCore/EasyMicroservices.Cores.EntityFrameworkCore.csproj +++ b/src/CSharp/EasyMicroservices.Cores.EntityFrameworkCore/EasyMicroservices.Cores.EntityFrameworkCore.csproj @@ -4,7 +4,7 @@ netstandard2.1;net6.0;net8.0 AnyCPU;x64;x86 EasyMicroservices - 0.0.1.42 + 0.0.1.43 ef core of database. EasyMicroservices@gmail.com core,cores,base,database,ef,efcore diff --git a/src/CSharp/EasyMicroservices.Cores.Relational.EntityFrameworkCore/EasyMicroservices.Cores.Relational.EntityFrameworkCore.csproj b/src/CSharp/EasyMicroservices.Cores.Relational.EntityFrameworkCore/EasyMicroservices.Cores.Relational.EntityFrameworkCore.csproj index d28cba0..f5ac325 100644 --- a/src/CSharp/EasyMicroservices.Cores.Relational.EntityFrameworkCore/EasyMicroservices.Cores.Relational.EntityFrameworkCore.csproj +++ b/src/CSharp/EasyMicroservices.Cores.Relational.EntityFrameworkCore/EasyMicroservices.Cores.Relational.EntityFrameworkCore.csproj @@ -4,7 +4,7 @@ netstandard2.1;net6.0;net7.0;net8.0 AnyCPU;x64;x86 EasyMicroservices - 0.0.1.42 + 0.0.1.43 ef core of Relational database. EasyMicroservices@gmail.com core,cores,base,database,ef,efcore,Relational