Implemented NDB.Security.Authentication.Identity

master
Tudor Stanciu 2020-12-21 23:09:47 +02:00
parent f9f35a3781
commit 584525cf87
5 changed files with 10 additions and 67 deletions

View File

@ -1,55 +0,0 @@
using IdentityServer.PublishedLanguage.Dto;
using IdentityServer.Wrapper.Services;
using Microsoft.AspNetCore.Authentication;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using System.Net.Http.Headers;
using System.Security.Claims;
using System.Text.Encodings.Web;
using System.Threading.Tasks;
namespace NetworkResurrector.Api.Authentication
{
public class BasicAuthenticationHandler : AuthenticationHandler<AuthenticationSchemeOptions>
{
private readonly IIdentityService _identityService;
public BasicAuthenticationHandler(IOptionsMonitor<AuthenticationSchemeOptions> options, ILoggerFactory logger, UrlEncoder encoder, ISystemClock clock, IIdentityService identityService)
: base(options, logger, encoder, clock)
{
_identityService = identityService;
}
protected override async Task<AuthenticateResult> HandleAuthenticateAsync()
{
if (!Request.Headers.ContainsKey("Authorization"))
return AuthenticateResult.Fail("Missing Authorization Header");
User user;
try
{
var authorizationHeader = AuthenticationHeaderValue.Parse(Request.Headers["Authorization"]);
var token = authorizationHeader.Parameter;
user = await _identityService.Authorize(token);
}
catch
{
return AuthenticateResult.Fail("Invalid Authorization Header");
}
if (user == null)
return AuthenticateResult.Fail("Invalid Username or Password");
var claims = new[] {
new Claim(ClaimTypes.NameIdentifier, user.UserId.ToString()),
new Claim(ClaimTypes.Name, user.UserName),
};
var identity = new ClaimsIdentity(claims, Scheme.Name);
var principal = new ClaimsPrincipal(identity);
var ticket = new AuthenticationTicket(principal, Scheme.Name);
return AuthenticateResult.Success(ticket);
}
}
}

View File

@ -5,14 +5,13 @@
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="IdentityServer.Wrapper" Version="1.0.1" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="3.1.4" /> <PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="3.1.4" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="$(MicrosoftExtensionsPackageVersion)" /> <PackageReference Include="Microsoft.Extensions.Configuration" Version="$(MicrosoftExtensionsPackageVersion)" />
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="$(MicrosoftExtensionsPackageVersion)" /> <PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="$(MicrosoftExtensionsPackageVersion)" />
<PackageReference Include="Microsoft.Extensions.Configuration.FileExtensions" Version="$(MicrosoftExtensionsPackageVersion)" /> <PackageReference Include="Microsoft.Extensions.Configuration.FileExtensions" Version="$(MicrosoftExtensionsPackageVersion)" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="$(MicrosoftExtensionsPackageVersion)" /> <PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="$(MicrosoftExtensionsPackageVersion)" />
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.9.5" />
<PackageReference Include="NDB.Extensions.Swagger" Version="$(NDBExtensionsPackageVersion)" /> <PackageReference Include="NDB.Extensions.Swagger" Version="$(NDBExtensionsPackageVersion)" />
<PackageReference Include="NDB.Security.Authentication.Identity" Version="$(NDBSecurityAuthenticationPackageVersion)" />
<PackageReference Include="Serilog.AspNetCore" Version="$(SerilogPackageVersion)" /> <PackageReference Include="Serilog.AspNetCore" Version="$(SerilogPackageVersion)" />
<PackageReference Include="Serilog.Extensions.Logging" Version="$(SerilogExtensionsPackageVersion)" /> <PackageReference Include="Serilog.Extensions.Logging" Version="$(SerilogExtensionsPackageVersion)" />
<PackageReference Include="Serilog.Sinks.Console" Version="$(SerilogSinksConsolePackageVersion)" /> <PackageReference Include="Serilog.Sinks.Console" Version="$(SerilogSinksConsolePackageVersion)" />

View File

@ -1,15 +1,13 @@
using AutoMapper; using AutoMapper;
using IdentityServer.Wrapper;
using MediatR; using MediatR;
using MediatR.Pipeline; using MediatR.Pipeline;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Hosting;
using NDB.Extensions.Swagger; using NDB.Extensions.Swagger;
using NetworkResurrector.Api.Authentication; using NDB.Security.Authentication.Identity;
using NetworkResurrector.Api.Extensions; using NetworkResurrector.Api.Extensions;
using NetworkResurrector.Application; using NetworkResurrector.Application;
using Newtonsoft.Json; using Newtonsoft.Json;
@ -32,9 +30,8 @@ namespace NetworkResurrector.Api
services.AddControllers() services.AddControllers()
.AddNewtonsoftJson(o => o.SerializerSettings.DateTimeZoneHandling = DateTimeZoneHandling.Utc); .AddNewtonsoftJson(o => o.SerializerSettings.DateTimeZoneHandling = DateTimeZoneHandling.Utc);
// configure basic authentication // Add basic authentication
services.AddAuthentication("BasicAuthentication") services.AddBasicAuthentication(_configuration.GetSection("IdentityServer")["BaseAddress"]);
.AddScheme<AuthenticationSchemeOptions, BasicAuthenticationHandler>("BasicAuthentication", null);
// MediatR // MediatR
services.AddMediatR(GetMediatRAssemblies()); services.AddMediatR(GetMediatRAssemblies());
@ -45,9 +42,6 @@ namespace NetworkResurrector.Api
services.AddAutoMapper( services.AddAutoMapper(
typeof(Application.Mappings.MappingProfile).Assembly); typeof(Application.Mappings.MappingProfile).Assembly);
// Identity server
services.UseIdentityServices(_configuration.GetSection("IdentityServer")["BaseAddress"]);
// Swagger // Swagger
services.AddSwagger("NetworkResurrector API"); services.AddSwagger("NetworkResurrector API");

View File

@ -7,3 +7,6 @@ dotnet publish --configuration Release --runtime win7-x64
Create windows service: Create windows service:
sc create NetworkResurrector.Api binPath= "<path_to_the_service_executable>" sc create NetworkResurrector.Api binPath= "<path_to_the_service_executable>"
####################################################################################################################################################### #######################################################################################################################################################
Multiple Directory.Build.props:
https://docs.microsoft.com/en-us/visualstudio/msbuild/customize-your-build?view=vs-2019

View File

@ -9,7 +9,9 @@
<AutoMapperExtensionsPackageVersion>7.0.0</AutoMapperExtensionsPackageVersion> <AutoMapperExtensionsPackageVersion>7.0.0</AutoMapperExtensionsPackageVersion>
<MediatRPackageVersion>6.0.0</MediatRPackageVersion> <MediatRPackageVersion>6.0.0</MediatRPackageVersion>
<SwashbucklePackageVersion>5.3.1</SwashbucklePackageVersion> <SwashbucklePackageVersion>5.3.1</SwashbucklePackageVersion>
<EntityFrameworkCorePackageVersion>3.1.3</EntityFrameworkCorePackageVersion>
<NDBExtensionsPackageVersion>1.0.0</NDBExtensionsPackageVersion> <NDBExtensionsPackageVersion>1.0.0</NDBExtensionsPackageVersion>
<NDBApplicationPackageVersion>1.0.0</NDBApplicationPackageVersion> <NDBApplicationPackageVersion>1.0.0</NDBApplicationPackageVersion>
<NDBSecurityAuthenticationPackageVersion>1.0.0</NDBSecurityAuthenticationPackageVersion>
</PropertyGroup> </PropertyGroup>
</Project> </Project>