Added IdentityServer.Wrapper
parent
7b420d432f
commit
d2730bc123
|
@ -0,0 +1,9 @@
|
||||||
|
namespace IdentityServer.Wrapper.Constants
|
||||||
|
{
|
||||||
|
internal struct ApiRoutes
|
||||||
|
{
|
||||||
|
public const string
|
||||||
|
Authentication = "identity/authenticate?UserName={0}&Password={1}",
|
||||||
|
Authorization = "identity/authorize?Token={0}";
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,15 @@
|
||||||
|
using IdentityServer.Wrapper.Models;
|
||||||
|
using IdentityServer.Wrapper.Services;
|
||||||
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
|
|
||||||
|
namespace IdentityServer.Wrapper
|
||||||
|
{
|
||||||
|
public static class DependencyInjectionExtension
|
||||||
|
{
|
||||||
|
public static void UseIdentityService(this IServiceCollection services, string baseAddress)
|
||||||
|
{
|
||||||
|
services.AddSingleton(new ServiceConfiguration(baseAddress));
|
||||||
|
services.AddHttpClient<IIdentityService, IdentityService>();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,14 @@
|
||||||
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
|
<PropertyGroup>
|
||||||
|
<TargetFramework>netstandard2.0</TargetFramework>
|
||||||
|
</PropertyGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<PackageReference Include="IdentityServer.PublishedLanguage" Version="1.0.0" />
|
||||||
|
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="3.1.3" />
|
||||||
|
<PackageReference Include="Microsoft.Extensions.Http" Version="3.1.3" />
|
||||||
|
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
</Project>
|
|
@ -0,0 +1,12 @@
|
||||||
|
namespace IdentityServer.Wrapper.Models
|
||||||
|
{
|
||||||
|
internal class ServiceConfiguration
|
||||||
|
{
|
||||||
|
public string BaseAddress { get; }
|
||||||
|
|
||||||
|
public ServiceConfiguration(string baseAddress)
|
||||||
|
{
|
||||||
|
BaseAddress = baseAddress;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,11 @@
|
||||||
|
using IdentityServer.PublishedLanguage.Dto;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace IdentityServer.Wrapper.Services
|
||||||
|
{
|
||||||
|
public interface IIdentityService
|
||||||
|
{
|
||||||
|
Task<Token> Authenticate(string userName, string password);
|
||||||
|
Task<User> Authorize(string token);
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,80 @@
|
||||||
|
using IdentityServer.PublishedLanguage.Dto;
|
||||||
|
using IdentityServer.Wrapper.Constants;
|
||||||
|
using IdentityServer.Wrapper.Models;
|
||||||
|
using Newtonsoft.Json;
|
||||||
|
using System;
|
||||||
|
using System.Net.Http;
|
||||||
|
using System.Net.Http.Headers;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace IdentityServer.Wrapper.Services
|
||||||
|
{
|
||||||
|
internal class IdentityService : IIdentityService
|
||||||
|
{
|
||||||
|
private const string _contentType = "application/json";
|
||||||
|
private readonly HttpClient _httpClient;
|
||||||
|
|
||||||
|
public IdentityService(HttpClient httpClient, ServiceConfiguration configuration)
|
||||||
|
{
|
||||||
|
httpClient.BaseAddress = new Uri(configuration.BaseAddress);
|
||||||
|
httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(_contentType));
|
||||||
|
|
||||||
|
_httpClient = httpClient;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<Token> Authenticate(string userName, string password)
|
||||||
|
{
|
||||||
|
var route = string.Format(ApiRoutes.Authentication, userName, password);
|
||||||
|
var result = await ExecutePostRequest<Token>(route);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<User> Authorize(string token)
|
||||||
|
{
|
||||||
|
var route = string.Format(ApiRoutes.Authorization, token);
|
||||||
|
var result = await ExecutePostRequest<User>(route);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
#region Private methods
|
||||||
|
private async Task<T> ExecuteGetRequest<T>(string route) where T : class
|
||||||
|
{
|
||||||
|
using (var response = await _httpClient.GetAsync(route))
|
||||||
|
{
|
||||||
|
if (!response.IsSuccessStatusCode)
|
||||||
|
throw new Exception($"Error: StatusCode: {response.StatusCode}; Reason: {response.ReasonPhrase}");
|
||||||
|
|
||||||
|
var result = JsonConvert.DeserializeObject<T>(await response.Content.ReadAsStringAsync());
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private async Task<R> ExecutePostRequest<R>(string route) where R : class
|
||||||
|
{
|
||||||
|
HttpContent content = new StringContent(JsonConvert.SerializeObject(string.Empty), Encoding.UTF8, _contentType);
|
||||||
|
using (var response = await _httpClient.PostAsync(route, content))
|
||||||
|
{
|
||||||
|
if (!response.IsSuccessStatusCode)
|
||||||
|
throw new Exception($"Error: StatusCode: {response.StatusCode}; Reason: {response.ReasonPhrase}");
|
||||||
|
|
||||||
|
var result = JsonConvert.DeserializeObject<R>(await response.Content.ReadAsStringAsync());
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private async Task<R> ExecutePostRequest<R, B>(string route, B body) where R : class where B : class
|
||||||
|
{
|
||||||
|
HttpContent content = new StringContent(JsonConvert.SerializeObject(body), Encoding.UTF8, _contentType);
|
||||||
|
using (var response = await _httpClient.PostAsync(route, content))
|
||||||
|
{
|
||||||
|
if (!response.IsSuccessStatusCode)
|
||||||
|
throw new Exception($"Error: StatusCode: {response.StatusCode}; Reason: {response.ReasonPhrase}");
|
||||||
|
|
||||||
|
var result = JsonConvert.DeserializeObject<R>(await response.Content.ReadAsStringAsync());
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#endregion
|
||||||
|
}
|
||||||
|
}
|
|
@ -26,6 +26,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "IdentityServer.Domain.Data"
|
||||||
EndProject
|
EndProject
|
||||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "IdentityServer.PublishedLanguage", "IdentityServer.PublishedLanguage\IdentityServer.PublishedLanguage.csproj", "{67B4D1FF-D02E-4DA6-9FB8-F71667360448}"
|
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "IdentityServer.PublishedLanguage", "IdentityServer.PublishedLanguage\IdentityServer.PublishedLanguage.csproj", "{67B4D1FF-D02E-4DA6-9FB8-F71667360448}"
|
||||||
EndProject
|
EndProject
|
||||||
|
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "IdentityServer.Wrapper", "IdentityServer.Wrapper\IdentityServer.Wrapper.csproj", "{F6FEC33B-C79E-4484-B356-9C7F1A5E5D95}"
|
||||||
|
EndProject
|
||||||
Global
|
Global
|
||||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||||
Debug|Any CPU = Debug|Any CPU
|
Debug|Any CPU = Debug|Any CPU
|
||||||
|
@ -52,6 +54,10 @@ Global
|
||||||
{67B4D1FF-D02E-4DA6-9FB8-F71667360448}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
{67B4D1FF-D02E-4DA6-9FB8-F71667360448}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||||
{67B4D1FF-D02E-4DA6-9FB8-F71667360448}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
{67B4D1FF-D02E-4DA6-9FB8-F71667360448}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||||
{67B4D1FF-D02E-4DA6-9FB8-F71667360448}.Release|Any CPU.Build.0 = Release|Any CPU
|
{67B4D1FF-D02E-4DA6-9FB8-F71667360448}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||||
|
{F6FEC33B-C79E-4484-B356-9C7F1A5E5D95}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||||
|
{F6FEC33B-C79E-4484-B356-9C7F1A5E5D95}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||||
|
{F6FEC33B-C79E-4484-B356-9C7F1A5E5D95}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||||
|
{F6FEC33B-C79E-4484-B356-9C7F1A5E5D95}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||||
EndGlobalSection
|
EndGlobalSection
|
||||||
GlobalSection(SolutionProperties) = preSolution
|
GlobalSection(SolutionProperties) = preSolution
|
||||||
HideSolutionNode = FALSE
|
HideSolutionNode = FALSE
|
||||||
|
@ -62,6 +68,7 @@ Global
|
||||||
{5890B079-3CB0-4AD6-8809-BB2E081590B1} = {5A8FF505-3E4D-4258-BC3E-CACD74A7B98C}
|
{5890B079-3CB0-4AD6-8809-BB2E081590B1} = {5A8FF505-3E4D-4258-BC3E-CACD74A7B98C}
|
||||||
{CE81A435-49AC-4544-A381-FAC91BEB3C49} = {5A8FF505-3E4D-4258-BC3E-CACD74A7B98C}
|
{CE81A435-49AC-4544-A381-FAC91BEB3C49} = {5A8FF505-3E4D-4258-BC3E-CACD74A7B98C}
|
||||||
{67B4D1FF-D02E-4DA6-9FB8-F71667360448} = {5A8FF505-3E4D-4258-BC3E-CACD74A7B98C}
|
{67B4D1FF-D02E-4DA6-9FB8-F71667360448} = {5A8FF505-3E4D-4258-BC3E-CACD74A7B98C}
|
||||||
|
{F6FEC33B-C79E-4484-B356-9C7F1A5E5D95} = {5A8FF505-3E4D-4258-BC3E-CACD74A7B98C}
|
||||||
EndGlobalSection
|
EndGlobalSection
|
||||||
GlobalSection(ExtensibilityGlobals) = postSolution
|
GlobalSection(ExtensibilityGlobals) = postSolution
|
||||||
SolutionGuid = {E93DC46D-9C55-4A05-B299-497CDD90747E}
|
SolutionGuid = {E93DC46D-9C55-4A05-B299-497CDD90747E}
|
||||||
|
|
Loading…
Reference in New Issue