From 584525cf87b70c586581d96cb53ef911c0223727 Mon Sep 17 00:00:00 2001 From: Tudor Stanciu Date: Mon, 21 Dec 2020 23:09:47 +0200 Subject: [PATCH] Implemented NDB.Security.Authentication.Identity --- .../BasicAuthenticationHandler.cs | 55 ------------------- .../NetworkResurrector.Api.csproj | 3 +- NetworkResurrector.Api/Startup.cs | 12 +--- Notes.txt | 5 +- dependencies.props | 2 + 5 files changed, 10 insertions(+), 67 deletions(-) delete mode 100644 NetworkResurrector.Api/Authentication/BasicAuthenticationHandler.cs diff --git a/NetworkResurrector.Api/Authentication/BasicAuthenticationHandler.cs b/NetworkResurrector.Api/Authentication/BasicAuthenticationHandler.cs deleted file mode 100644 index 7301dca..0000000 --- a/NetworkResurrector.Api/Authentication/BasicAuthenticationHandler.cs +++ /dev/null @@ -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 - { - private readonly IIdentityService _identityService; - - public BasicAuthenticationHandler(IOptionsMonitor options, ILoggerFactory logger, UrlEncoder encoder, ISystemClock clock, IIdentityService identityService) - : base(options, logger, encoder, clock) - { - _identityService = identityService; - } - - protected override async Task 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); - } - } -} diff --git a/NetworkResurrector.Api/NetworkResurrector.Api.csproj b/NetworkResurrector.Api/NetworkResurrector.Api.csproj index 61cad6f..88d10f8 100644 --- a/NetworkResurrector.Api/NetworkResurrector.Api.csproj +++ b/NetworkResurrector.Api/NetworkResurrector.Api.csproj @@ -5,14 +5,13 @@ - - + diff --git a/NetworkResurrector.Api/Startup.cs b/NetworkResurrector.Api/Startup.cs index 18625ec..341e7c1 100644 --- a/NetworkResurrector.Api/Startup.cs +++ b/NetworkResurrector.Api/Startup.cs @@ -1,15 +1,13 @@ using AutoMapper; -using IdentityServer.Wrapper; using MediatR; using MediatR.Pipeline; -using Microsoft.AspNetCore.Authentication; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using NDB.Extensions.Swagger; -using NetworkResurrector.Api.Authentication; +using NDB.Security.Authentication.Identity; using NetworkResurrector.Api.Extensions; using NetworkResurrector.Application; using Newtonsoft.Json; @@ -32,9 +30,8 @@ namespace NetworkResurrector.Api services.AddControllers() .AddNewtonsoftJson(o => o.SerializerSettings.DateTimeZoneHandling = DateTimeZoneHandling.Utc); - // configure basic authentication - services.AddAuthentication("BasicAuthentication") - .AddScheme("BasicAuthentication", null); + // Add basic authentication + services.AddBasicAuthentication(_configuration.GetSection("IdentityServer")["BaseAddress"]); // MediatR services.AddMediatR(GetMediatRAssemblies()); @@ -45,9 +42,6 @@ namespace NetworkResurrector.Api services.AddAutoMapper( typeof(Application.Mappings.MappingProfile).Assembly); - // Identity server - services.UseIdentityServices(_configuration.GetSection("IdentityServer")["BaseAddress"]); - // Swagger services.AddSwagger("NetworkResurrector API"); diff --git a/Notes.txt b/Notes.txt index 0478467..3985645 100644 --- a/Notes.txt +++ b/Notes.txt @@ -6,4 +6,7 @@ dotnet publish --configuration Release --runtime win7-x64 Create windows service: sc create NetworkResurrector.Api binPath= "" -####################################################################################################################################################### \ No newline at end of file +####################################################################################################################################################### + +Multiple Directory.Build.props: +https://docs.microsoft.com/en-us/visualstudio/msbuild/customize-your-build?view=vs-2019 \ No newline at end of file diff --git a/dependencies.props b/dependencies.props index d2e9092..8cf27b0 100644 --- a/dependencies.props +++ b/dependencies.props @@ -9,7 +9,9 @@ 7.0.0 6.0.0 5.3.1 + 3.1.3 1.0.0 1.0.0 + 1.0.0 \ No newline at end of file