Skip to content

Commit

Permalink
Add Bearer Auth to GetContentClient
Browse files Browse the repository at this point in the history
  • Loading branch information
Ali-YousefiTelori committed Jan 30, 2024
1 parent f5d6765 commit 128e48d
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<TargetFrameworks>netstandard2.1;net6.0;net7.0;net8.0</TargetFrameworks>
<Platforms>AnyCPU;x64;x86</Platforms>
<Authors>EasyMicroservices</Authors>
<Version>0.0.1.41</Version>
<Version>0.0.1.42</Version>
<Description>asp core servces.</Description>
<Copyright>[email protected]</Copyright>
<PackageTags>core,cores,base,database,services,asp,aspnet</PackageTags>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<TargetFrameworks>net6.0;net7.0;net8.0</TargetFrameworks>
<Platforms>AnyCPU;x64;x86</Platforms>
<Authors>EasyMicroservices</Authors>
<Version>0.0.1.41</Version>
<Version>0.0.1.42</Version>
<Description>asp core servces.</Description>
<Copyright>[email protected]</Copyright>
<PackageTags>core,cores,base,database,services,asp,aspnet,aspcore,efcore</PackageTags>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
using EasyMicroservices.ContentsMicroservice.Clients.Helpers;
using Contents.GeneratedServices;
using EasyMicroservices.ContentsMicroservice.Clients.Helpers;
using EasyMicroservices.Cores.AspCoreApi.Authorizations;
using EasyMicroservices.Cores.AspCoreApi.Interfaces;
using EasyMicroservices.Cores.AspCoreApi.Managers;
using EasyMicroservices.Cores.AspEntityFrameworkCoreApi.Interfaces;
using EasyMicroservices.Cores.AspEntityFrameworkCoreApi.Middlewares;
using EasyMicroservices.Cores.Clients;
using EasyMicroservices.Cores.Database.Interfaces;
using EasyMicroservices.Cores.Database.Logics;
using EasyMicroservices.Cores.Database.Managers;
Expand All @@ -14,6 +16,7 @@
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Cors.Infrastructure;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.IdentityModel.Tokens;
Expand Down Expand Up @@ -86,12 +89,29 @@ public static IServiceCollection Builder<TContext>(this IServiceCollection servi
{
option.ExceptionHandler = AppAuthorizationMiddleware.ExceptionHandler;
});
services.AddScoped<Contents.GeneratedServices.ContentClient>(service => new Contents.GeneratedServices.ContentClient(GetContentAddress(service)?.Address, new HttpClient()));
services.AddScoped<Contents.GeneratedServices.ContentClient>(service => GetContentClient(service));
services.AddScoped<ContentLanguageHelper>();
services.AddScoped<IContentResolver, InternalContentResolver>();
return services;
}

static HttpClient ContentClientCttpClient = new HttpClient();
static T SetToken<T>(IServiceProvider serviceProvider, T coreSwaggerClientBase)
where T : CoreSwaggerClientBase
{
var _httpContextAccessor = serviceProvider.GetService<IHttpContextAccessor>();
if (_httpContextAccessor?.HttpContext != null && _httpContextAccessor.HttpContext.Request.Headers.TryGetValue("Authorization", out var authorizationHeader))
{
coreSwaggerClientBase.SetBearerToken(authorizationHeader.ToString().Replace("Bearer ", ""));
}
return coreSwaggerClientBase;
}

static ContentClient GetContentClient(IServiceProvider serviceProvider)
{
return SetToken(serviceProvider, new Contents.GeneratedServices.ContentClient(GetContentAddress(serviceProvider)?.Address, ContentClientCttpClient));
}

/// <summary>
///
/// </summary>
Expand Down Expand Up @@ -143,7 +163,7 @@ public static IServiceCollection UseActivityChangeLog(this IServiceCollection se
}


static void UseSwaggerUI(IConfiguration config,Func<Action<SwaggerUIOptions>,IApplicationBuilder> swagger)
static void UseSwaggerUI(IConfiguration config, Func<Action<SwaggerUIOptions>, IApplicationBuilder> swagger)
{
var ui = config.GetSection("Swagger:SwaggerUI").Get<SwaggerUIConfigInfo>();
swagger(so =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<TargetFrameworks>netstandard2.0;netstandard2.1;net6.0;net8.0</TargetFrameworks>
<Platforms>AnyCPU;x64;x86</Platforms>
<Authors>EasyMicroservices</Authors>
<Version>0.0.1.41</Version>
<Version>0.0.1.42</Version>
<Description>core of database.</Description>
<Copyright>[email protected]</Copyright>
<PackageTags>core,cores,base,client,clients</PackageTags>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<TargetFrameworks>netstandard2.0;netstandard2.1;net45;net6.0;net8.0</TargetFrameworks>
<Platforms>AnyCPU;x64;x86</Platforms>
<Authors>EasyMicroservices</Authors>
<Version>0.0.1.41</Version>
<Version>0.0.1.42</Version>
<Description>core contracts.</Description>
<Copyright>[email protected]</Copyright>
<PackageTags>core,cores,base,contract,contracts,dto,dtos</PackageTags>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<TargetFrameworks>netstandard2.0;netstandard2.1;net45;net6.0;net8.0</TargetFrameworks>
<Platforms>AnyCPU;x64;x86</Platforms>
<Authors>EasyMicroservices</Authors>
<Version>0.0.1.41</Version>
<Version>0.0.1.42</Version>
<Description>core of database.</Description>
<Copyright>[email protected]</Copyright>
<PackageTags>core,cores,base,database</PackageTags>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<TargetFrameworks>netstandard2.1;net6.0;net8.0</TargetFrameworks>
<Platforms>AnyCPU;x64;x86</Platforms>
<Authors>EasyMicroservices</Authors>
<Version>0.0.1.41</Version>
<Version>0.0.1.42</Version>
<Description>ef core of database.</Description>
<Copyright>[email protected]</Copyright>
<PackageTags>core,cores,base,database,ef,efcore</PackageTags>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<TargetFrameworks>netstandard2.1;net6.0;net7.0;net8.0</TargetFrameworks>
<Platforms>AnyCPU;x64;x86</Platforms>
<Authors>EasyMicroservices</Authors>
<Version>0.0.1.41</Version>
<Version>0.0.1.42</Version>
<Description>ef core of Relational database.</Description>
<Copyright>[email protected]</Copyright>
<PackageTags>core,cores,base,database,ef,efcore,Relational</PackageTags>
Expand Down

0 comments on commit 128e48d

Please sign in to comment.