mirror of https://github.com/abpframework/abp.git
11 changed files with 143 additions and 47 deletions
@ -0,0 +1,12 @@ |
|||
using Volo.Abp.Modularity; |
|||
|
|||
namespace Volo.Abp.Http.Client.IdentityModel |
|||
{ |
|||
[DependsOn( |
|||
typeof(AbpHttpClientModule) |
|||
)] |
|||
public class AbpHttpClientIdentityModelModule : AbpModule |
|||
{ |
|||
|
|||
} |
|||
} |
|||
@ -0,0 +1,44 @@ |
|||
using System.Net.Http.Headers; |
|||
using System.Threading.Tasks; |
|||
using Microsoft.AspNetCore.Authentication; |
|||
using Microsoft.AspNetCore.Http; |
|||
using Volo.Abp.DependencyInjection; |
|||
using Volo.Abp.Http.Client.Authentication; |
|||
|
|||
namespace NewFolder.Abp.Http.Client.IdentityModel |
|||
{ |
|||
[Dependency(ReplaceServices = true)] |
|||
public class IdentityModelHttpClientAuthenticator : IHttpClientAuthenticator, ITransientDependency |
|||
{ |
|||
public IHttpContextAccessor HttpContextAccessor { get; set; } |
|||
|
|||
public async Task Authenticate(HttpClientAuthenticateContext context) |
|||
{ |
|||
var accessToken = await GetAccessTokenFromHttpContextOrNullAsync() ?? |
|||
await GetAccessTokenFromServerOrNullAsync(context); |
|||
|
|||
if (accessToken != null) |
|||
{ |
|||
//TODO: "Bearer" should be configurable
|
|||
context.Client.DefaultRequestHeaders.Authorization |
|||
= new AuthenticationHeaderValue("Bearer", accessToken); |
|||
} |
|||
} |
|||
|
|||
protected virtual Task<string> GetAccessTokenFromServerOrNullAsync(HttpClientAuthenticateContext context) |
|||
{ |
|||
return Task.FromResult((string) null); |
|||
} |
|||
|
|||
protected virtual async Task<string> GetAccessTokenFromHttpContextOrNullAsync() |
|||
{ |
|||
var httpContext = HttpContextAccessor?.HttpContext; |
|||
if (httpContext == null) |
|||
{ |
|||
return null; |
|||
} |
|||
|
|||
return await httpContext.GetTokenAsync("access_token"); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,20 @@ |
|||
<Project Sdk="Microsoft.NET.Sdk"> |
|||
|
|||
<Import Project="..\..\..\common.props" /> |
|||
|
|||
<PropertyGroup> |
|||
<TargetFramework>netstandard2.0</TargetFramework> |
|||
<AssemblyName>Volo.Abp.Http.Client.IdentityModel</AssemblyName> |
|||
<PackageId>Volo.Abp.Http.Client.IdentityModel</PackageId> |
|||
<AssetTargetFallback>$(AssetTargetFallback);portable-net45+win8+wp8+wpa81;</AssetTargetFallback> |
|||
<GenerateAssemblyConfigurationAttribute>false</GenerateAssemblyConfigurationAttribute> |
|||
<GenerateAssemblyCompanyAttribute>false</GenerateAssemblyCompanyAttribute> |
|||
<GenerateAssemblyProductAttribute>false</GenerateAssemblyProductAttribute> |
|||
<RootNamespace /> |
|||
</PropertyGroup> |
|||
|
|||
<ItemGroup> |
|||
<ProjectReference Include="..\Volo.Abp.Http.Client\Volo.Abp.Http.Client.csproj" /> |
|||
</ItemGroup> |
|||
|
|||
</Project> |
|||
@ -0,0 +1,23 @@ |
|||
using System.Net.Http; |
|||
|
|||
namespace Volo.Abp.Http.Client.Authentication |
|||
{ |
|||
public class HttpClientAuthenticateContext |
|||
{ |
|||
public HttpClient Client { get; } |
|||
|
|||
public HttpRequestMessage Request { get; } |
|||
|
|||
public string RemoteServiceName { get; } |
|||
|
|||
public HttpClientAuthenticateContext( |
|||
HttpClient client, |
|||
HttpRequestMessage request, |
|||
string remoteServiceName) |
|||
{ |
|||
Client = client; |
|||
Request = request; |
|||
RemoteServiceName = remoteServiceName; |
|||
} |
|||
} |
|||
} |
|||
@ -1,24 +0,0 @@ |
|||
using System.Threading.Tasks; |
|||
using Microsoft.AspNetCore.Authentication; |
|||
using Microsoft.AspNetCore.Http; |
|||
using Volo.Abp.DependencyInjection; |
|||
using Volo.Abp.Http.Client.DynamicProxying; |
|||
|
|||
namespace Volo.Abp.Http.Client.Authentication |
|||
{ |
|||
public class HttpContextAccessTokenProvider : IAccessTokenProvider, ISingletonDependency |
|||
{ |
|||
public IHttpContextAccessor HttpContextAccessor { get; set; } |
|||
|
|||
public async Task<string> GetOrNullAsync(DynamicHttpClientProxyConfig config) |
|||
{ |
|||
var httpContext = HttpContextAccessor?.HttpContext; |
|||
if (httpContext == null) |
|||
{ |
|||
return null; |
|||
} |
|||
|
|||
return await httpContext.GetTokenAsync("access_token"); |
|||
} |
|||
} |
|||
} |
|||
@ -1,10 +0,0 @@ |
|||
using System.Threading.Tasks; |
|||
using Volo.Abp.Http.Client.DynamicProxying; |
|||
|
|||
namespace Volo.Abp.Http.Client.Authentication |
|||
{ |
|||
public interface IAccessTokenProvider |
|||
{ |
|||
Task<string> GetOrNullAsync(DynamicHttpClientProxyConfig config); |
|||
} |
|||
} |
|||
@ -0,0 +1,9 @@ |
|||
using System.Threading.Tasks; |
|||
|
|||
namespace Volo.Abp.Http.Client.Authentication |
|||
{ |
|||
public interface IHttpClientAuthenticator |
|||
{ |
|||
Task Authenticate(HttpClientAuthenticateContext context); |
|||
} |
|||
} |
|||
@ -0,0 +1,14 @@ |
|||
using System.Threading.Tasks; |
|||
using Volo.Abp.DependencyInjection; |
|||
|
|||
namespace Volo.Abp.Http.Client.Authentication |
|||
{ |
|||
[Dependency(TryRegister = true)] |
|||
public class NullHttpClientAuthenticator : IHttpClientAuthenticator, ISingletonDependency |
|||
{ |
|||
public Task Authenticate(HttpClientAuthenticateContext context) |
|||
{ |
|||
return Task.CompletedTask; |
|||
} |
|||
} |
|||
} |
|||
Loading…
Reference in new issue