From 31359d4c40382ecf9bee072bdb984dba50311112 Mon Sep 17 00:00:00 2001 From: Sumit Ghosh Date: Thu, 21 Oct 2021 19:52:21 +0530 Subject: [PATCH] Included file scope namespaces for Webhookclient --- .../Controllers/AccountController.cs | 49 ++-- .../Controllers/WebhooksReceivedController.cs | 77 +++-- src/Web/WebhookClient/HeaderNames.cs | 9 +- ...ttpClientAuthorizationDelegatingHandler.cs | 63 ++--- .../WebhookClient/Models/WebHookReceived.cs | 13 +- src/Web/WebhookClient/Models/WebhookData.cs | 13 +- .../WebhookClient/Models/WebhookResponse.cs | 13 +- .../Models/WebhookSubscriptionRequest.cs | 15 +- src/Web/WebhookClient/Program.cs | 6 +- .../Services/IHooksRepository.cs | 13 +- .../WebhookClient/Services/IWebhooksClient.cs | 11 +- .../Services/InMemoryHooksRepository.cs | 32 +-- .../WebhookClient/Services/WebhooksClient.cs | 44 ++- src/Web/WebhookClient/Settings.cs | 19 +- src/Web/WebhookClient/Startup.cs | 264 ++++++++---------- 15 files changed, 279 insertions(+), 362 deletions(-) diff --git a/src/Web/WebhookClient/Controllers/AccountController.cs b/src/Web/WebhookClient/Controllers/AccountController.cs index 461e3261d..f67de6d61 100644 --- a/src/Web/WebhookClient/Controllers/AccountController.cs +++ b/src/Web/WebhookClient/Controllers/AccountController.cs @@ -1,37 +1,28 @@ -using Microsoft.AspNetCore.Authentication; -using Microsoft.AspNetCore.Authentication.Cookies; -using Microsoft.AspNetCore.Authentication.OpenIdConnect; -using Microsoft.AspNetCore.Authorization; -using Microsoft.AspNetCore.Mvc; -using System.Security.Claims; -using System.Threading.Tasks; +namespace WebhookClient.Controllers; -namespace WebhookClient.Controllers +[Authorize] +public class AccountController : Controller { - [Authorize] - public class AccountController : Controller + public async Task SignIn(string returnUrl) { - public async Task SignIn(string returnUrl) + var user = User as ClaimsPrincipal; + + var token = await HttpContext.GetTokenAsync("access_token"); + + if (token != null) { - var user = User as ClaimsPrincipal; - - var token = await HttpContext.GetTokenAsync("access_token"); - - if (token != null) - { - ViewData["access_token"] = token; - } - - return RedirectToPage("/Index"); + ViewData["access_token"] = token; } - public async Task Signout() - { - await HttpContext.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme); - await HttpContext.SignOutAsync(OpenIdConnectDefaults.AuthenticationScheme); - var homeUrl = Url.Page("/Index"); - return new SignOutResult(OpenIdConnectDefaults.AuthenticationScheme, - new AuthenticationProperties { RedirectUri = homeUrl }); - } + return RedirectToPage("/Index"); + } + + public async Task Signout() + { + await HttpContext.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme); + await HttpContext.SignOutAsync(OpenIdConnectDefaults.AuthenticationScheme); + var homeUrl = Url.Page("/Index"); + return new SignOutResult(OpenIdConnectDefaults.AuthenticationScheme, + new AuthenticationProperties { RedirectUri = homeUrl }); } } diff --git a/src/Web/WebhookClient/Controllers/WebhooksReceivedController.cs b/src/Web/WebhookClient/Controllers/WebhooksReceivedController.cs index 985031106..db7a2c6c0 100644 --- a/src/Web/WebhookClient/Controllers/WebhooksReceivedController.cs +++ b/src/Web/WebhookClient/Controllers/WebhooksReceivedController.cs @@ -1,53 +1,44 @@ -using Microsoft.AspNetCore.Mvc; -using Microsoft.Extensions.Logging; -using Microsoft.Extensions.Options; -using System.Linq; -using System.Threading.Tasks; -using WebhookClient.Models; -using WebhookClient.Services; +namespace WebhookClient.Controllers; -namespace WebhookClient.Controllers +[ApiController] +[Route("webhook-received")] +public class WebhooksReceivedController : Controller { - [ApiController] - [Route("webhook-received")] - public class WebhooksReceivedController : Controller + + private readonly Settings _settings; + private readonly ILogger _logger; + private readonly IHooksRepository _hooksRepository; + + public WebhooksReceivedController(IOptions settings, ILogger logger, IHooksRepository hooksRepository) { + _settings = settings.Value; + _logger = logger; + _hooksRepository = hooksRepository; + } - private readonly Settings _settings; - private readonly ILogger _logger; - private readonly IHooksRepository _hooksRepository; + [HttpPost] + public async Task NewWebhook(WebhookData hook) + { + var header = Request.Headers[HeaderNames.WebHookCheckHeader]; + var token = header.FirstOrDefault(); - public WebhooksReceivedController(IOptions settings, ILogger logger, IHooksRepository hooksRepository) + _logger.LogInformation("Received hook with token {Token}. My token is {MyToken}. Token validation is set to {ValidateToken}", token, _settings.Token, _settings.ValidateToken); + + if (!_settings.ValidateToken || _settings.Token == token) { - _settings = settings.Value; - _logger = logger; - _hooksRepository = hooksRepository; - } - - [HttpPost] - public async Task NewWebhook(WebhookData hook) - { - var header = Request.Headers[HeaderNames.WebHookCheckHeader]; - var token = header.FirstOrDefault(); - - _logger.LogInformation("Received hook with token {Token}. My token is {MyToken}. Token validation is set to {ValidateToken}", token, _settings.Token, _settings.ValidateToken); - - if (!_settings.ValidateToken || _settings.Token == token) + _logger.LogInformation("Received hook is going to be processed"); + var newHook = new WebHookReceived() { - _logger.LogInformation("Received hook is going to be processed"); - var newHook = new WebHookReceived() - { - Data = hook.Payload, - When = hook.When, - Token = token - }; - await _hooksRepository.AddNew(newHook); - _logger.LogInformation("Received hook was processed."); - return Ok(newHook); - } - - _logger.LogInformation("Received hook is NOT processed - Bad Request returned."); - return BadRequest(); + Data = hook.Payload, + When = hook.When, + Token = token + }; + await _hooksRepository.AddNew(newHook); + _logger.LogInformation("Received hook was processed."); + return Ok(newHook); } + + _logger.LogInformation("Received hook is NOT processed - Bad Request returned."); + return BadRequest(); } } diff --git a/src/Web/WebhookClient/HeaderNames.cs b/src/Web/WebhookClient/HeaderNames.cs index 6a7f57577..7b4b3ac75 100644 --- a/src/Web/WebhookClient/HeaderNames.cs +++ b/src/Web/WebhookClient/HeaderNames.cs @@ -1,7 +1,6 @@ -namespace WebhookClient +namespace WebhookClient; + +static class HeaderNames { - static class HeaderNames - { - public const string WebHookCheckHeader = "X-eshop-whtoken"; - } + public const string WebHookCheckHeader = "X-eshop-whtoken"; } diff --git a/src/Web/WebhookClient/HttpClientAuthorizationDelegatingHandler.cs b/src/Web/WebhookClient/HttpClientAuthorizationDelegatingHandler.cs index d32dff972..6ba7ddd60 100644 --- a/src/Web/WebhookClient/HttpClientAuthorizationDelegatingHandler.cs +++ b/src/Web/WebhookClient/HttpClientAuthorizationDelegatingHandler.cs @@ -1,49 +1,40 @@ -using Microsoft.AspNetCore.Authentication; -using Microsoft.AspNetCore.Http; -using System.Collections.Generic; -using System.Net.Http; -using System.Net.Http.Headers; -using System.Threading; -using System.Threading.Tasks; +namespace WebhookClient; -namespace WebhookClient +public class HttpClientAuthorizationDelegatingHandler + : DelegatingHandler { - public class HttpClientAuthorizationDelegatingHandler - : DelegatingHandler + private readonly IHttpContextAccessor _httpContextAccessor; + + public HttpClientAuthorizationDelegatingHandler(IHttpContextAccessor httpContextAccessor) { - private readonly IHttpContextAccessor _httpContextAccessor; + _httpContextAccessor = httpContextAccessor; + } - public HttpClientAuthorizationDelegatingHandler(IHttpContextAccessor httpContextAccessor) + protected override async Task SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) + { + var authorizationHeader = _httpContextAccessor.HttpContext + .Request.Headers["Authorization"]; + + if (!string.IsNullOrEmpty(authorizationHeader)) { - _httpContextAccessor = httpContextAccessor; + request.Headers.Add("Authorization", new List() { authorizationHeader }); } - protected override async Task SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) + var token = await GetToken(); + + if (token != null) { - var authorizationHeader = _httpContextAccessor.HttpContext - .Request.Headers["Authorization"]; - - if (!string.IsNullOrEmpty(authorizationHeader)) - { - request.Headers.Add("Authorization", new List() { authorizationHeader }); - } - - var token = await GetToken(); - - if (token != null) - { - request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", token); - } - - return await base.SendAsync(request, cancellationToken); + request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", token); } - async Task GetToken() - { - const string ACCESS_TOKEN = "access_token"; + return await base.SendAsync(request, cancellationToken); + } - return await _httpContextAccessor.HttpContext - .GetTokenAsync(ACCESS_TOKEN); - } + async Task GetToken() + { + const string ACCESS_TOKEN = "access_token"; + + return await _httpContextAccessor.HttpContext + .GetTokenAsync(ACCESS_TOKEN); } } diff --git a/src/Web/WebhookClient/Models/WebHookReceived.cs b/src/Web/WebhookClient/Models/WebHookReceived.cs index 3da5bae48..2687f7067 100644 --- a/src/Web/WebhookClient/Models/WebHookReceived.cs +++ b/src/Web/WebhookClient/Models/WebHookReceived.cs @@ -1,13 +1,10 @@ -using System; +namespace WebhookClient.Models; -namespace WebhookClient.Models +public class WebHookReceived { - public class WebHookReceived - { - public DateTime When { get; set; } + public DateTime When { get; set; } - public string Data { get; set; } + public string Data { get; set; } - public string Token { get; set; } - } + public string Token { get; set; } } diff --git a/src/Web/WebhookClient/Models/WebhookData.cs b/src/Web/WebhookClient/Models/WebhookData.cs index c80a8d980..a7f3c39af 100644 --- a/src/Web/WebhookClient/Models/WebhookData.cs +++ b/src/Web/WebhookClient/Models/WebhookData.cs @@ -1,13 +1,10 @@ -using System; +namespace WebhookClient.Models; -namespace WebhookClient.Models +public class WebhookData { - public class WebhookData - { - public DateTime When { get; set; } + public DateTime When { get; set; } - public string Payload { get; set; } + public string Payload { get; set; } - public string Type { get; set; } - } + public string Type { get; set; } } diff --git a/src/Web/WebhookClient/Models/WebhookResponse.cs b/src/Web/WebhookClient/Models/WebhookResponse.cs index 5b1aadd45..d03a13b30 100644 --- a/src/Web/WebhookClient/Models/WebhookResponse.cs +++ b/src/Web/WebhookClient/Models/WebhookResponse.cs @@ -1,11 +1,8 @@ -using System; +namespace WebhookClient.Models; -namespace WebhookClient.Models +public class WebhookResponse { - public class WebhookResponse - { - public DateTime Date { get; set; } - public string DestUrl { get; set; } - public string Token { get; set; } - } + public DateTime Date { get; set; } + public string DestUrl { get; set; } + public string Token { get; set; } } diff --git a/src/Web/WebhookClient/Models/WebhookSubscriptionRequest.cs b/src/Web/WebhookClient/Models/WebhookSubscriptionRequest.cs index c770c27ed..83d997b5a 100644 --- a/src/Web/WebhookClient/Models/WebhookSubscriptionRequest.cs +++ b/src/Web/WebhookClient/Models/WebhookSubscriptionRequest.cs @@ -1,10 +1,9 @@ -namespace WebhookClient.Models +namespace WebhookClient.Models; + +public class WebhookSubscriptionRequest { - public class WebhookSubscriptionRequest - { - public string Url { get; set; } - public string Token { get; set; } - public string Event { get; set; } - public string GrantUrl { get; set; } - } + public string Url { get; set; } + public string Token { get; set; } + public string Event { get; set; } + public string GrantUrl { get; set; } } diff --git a/src/Web/WebhookClient/Program.cs b/src/Web/WebhookClient/Program.cs index ffb12b7b2..db38493ae 100644 --- a/src/Web/WebhookClient/Program.cs +++ b/src/Web/WebhookClient/Program.cs @@ -1,8 +1,4 @@ -using Microsoft.AspNetCore; -using Microsoft.AspNetCore.Hosting; -using WebhookClient; - -CreateWebHostBuilder(args).Build().Run(); +CreateWebHostBuilder(args).Build().Run(); IWebHostBuilder CreateWebHostBuilder(string[] args) => diff --git a/src/Web/WebhookClient/Services/IHooksRepository.cs b/src/Web/WebhookClient/Services/IHooksRepository.cs index 73e89e819..7b4131ec8 100644 --- a/src/Web/WebhookClient/Services/IHooksRepository.cs +++ b/src/Web/WebhookClient/Services/IHooksRepository.cs @@ -1,12 +1,7 @@ -using System.Collections.Generic; -using System.Threading.Tasks; -using WebhookClient.Models; +namespace WebhookClient.Services; -namespace WebhookClient.Services +public interface IHooksRepository { - public interface IHooksRepository - { - Task> GetAll(); - Task AddNew(WebHookReceived hook); - } + Task> GetAll(); + Task AddNew(WebHookReceived hook); } diff --git a/src/Web/WebhookClient/Services/IWebhooksClient.cs b/src/Web/WebhookClient/Services/IWebhooksClient.cs index 20238c0a3..462f8456f 100644 --- a/src/Web/WebhookClient/Services/IWebhooksClient.cs +++ b/src/Web/WebhookClient/Services/IWebhooksClient.cs @@ -1,11 +1,6 @@ -using System.Collections.Generic; -using System.Threading.Tasks; -using WebhookClient.Models; +namespace WebhookClient.Services; -namespace WebhookClient.Services +public interface IWebhooksClient { - public interface IWebhooksClient - { - Task> LoadWebhooks(); - } + Task> LoadWebhooks(); } diff --git a/src/Web/WebhookClient/Services/InMemoryHooksRepository.cs b/src/Web/WebhookClient/Services/InMemoryHooksRepository.cs index 35bcd2bad..504d17795 100644 --- a/src/Web/WebhookClient/Services/InMemoryHooksRepository.cs +++ b/src/Web/WebhookClient/Services/InMemoryHooksRepository.cs @@ -1,25 +1,19 @@ -using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; -using WebhookClient.Models; +namespace WebhookClient.Services; -namespace WebhookClient.Services +public class InMemoryHooksRepository : IHooksRepository { - public class InMemoryHooksRepository : IHooksRepository + private readonly List _data; + + public InMemoryHooksRepository() => _data = new List(); + + public Task AddNew(WebHookReceived hook) { - private readonly List _data; + _data.Add(hook); + return Task.CompletedTask; + } - public InMemoryHooksRepository() => _data = new List(); - - public Task AddNew(WebHookReceived hook) - { - _data.Add(hook); - return Task.CompletedTask; - } - - public Task> GetAll() - { - return Task.FromResult(_data.AsEnumerable()); - } + public Task> GetAll() + { + return Task.FromResult(_data.AsEnumerable()); } } diff --git a/src/Web/WebhookClient/Services/WebhooksClient.cs b/src/Web/WebhookClient/Services/WebhooksClient.cs index dc12aaf37..87f950a34 100644 --- a/src/Web/WebhookClient/Services/WebhooksClient.cs +++ b/src/Web/WebhookClient/Services/WebhooksClient.cs @@ -1,32 +1,24 @@ -using Microsoft.Extensions.Options; -using System.Collections.Generic; -using System.Net.Http; -using System.Threading.Tasks; -using WebhookClient.Models; -using System.Text.Json; +namespace WebhookClient.Services; -namespace WebhookClient.Services +public class WebhooksClient : IWebhooksClient { - public class WebhooksClient : IWebhooksClient - { - private readonly IHttpClientFactory _httpClientFactory; - private readonly Settings _settings; - public WebhooksClient(IHttpClientFactory httpClientFactory, IOptions settings) + private readonly IHttpClientFactory _httpClientFactory; + private readonly Settings _settings; + public WebhooksClient(IHttpClientFactory httpClientFactory, IOptions settings) + { + _httpClientFactory = httpClientFactory; + _settings = settings.Value; + } + public async Task> LoadWebhooks() + { + var client = _httpClientFactory.CreateClient("GrantClient"); + var response = await client.GetAsync(_settings.WebhooksUrl + "/api/v1/webhooks"); + var json = await response.Content.ReadAsStringAsync(); + var subscriptions = JsonSerializer.Deserialize>(json, new JsonSerializerOptions { - _httpClientFactory = httpClientFactory; - _settings = settings.Value; - } - public async Task> LoadWebhooks() - { - var client = _httpClientFactory.CreateClient("GrantClient"); - var response = await client.GetAsync(_settings.WebhooksUrl + "/api/v1/webhooks"); - var json = await response.Content.ReadAsStringAsync(); - var subscriptions = JsonSerializer.Deserialize>(json, new JsonSerializerOptions - { - PropertyNameCaseInsensitive = true - }); - return subscriptions; - } + PropertyNameCaseInsensitive = true + }); + return subscriptions; } } diff --git a/src/Web/WebhookClient/Settings.cs b/src/Web/WebhookClient/Settings.cs index 3077d91b0..14b67dadf 100644 --- a/src/Web/WebhookClient/Settings.cs +++ b/src/Web/WebhookClient/Settings.cs @@ -1,14 +1,13 @@ -namespace WebhookClient +namespace WebhookClient; + +public class Settings { - public class Settings - { - public string Token { get; set; } - public string IdentityUrl { get; set; } - public string CallBackUrl { get; set; } - public string WebhooksUrl { get; set; } - public string SelfUrl { get; set; } + public string Token { get; set; } + public string IdentityUrl { get; set; } + public string CallBackUrl { get; set; } + public string WebhooksUrl { get; set; } + public string SelfUrl { get; set; } - public bool ValidateToken { get; set; } + public bool ValidateToken { get; set; } - } } diff --git a/src/Web/WebhookClient/Startup.cs b/src/Web/WebhookClient/Startup.cs index f45a30b5d..8a3771c99 100644 --- a/src/Web/WebhookClient/Startup.cs +++ b/src/Web/WebhookClient/Startup.cs @@ -1,165 +1,149 @@ -using Microsoft.AspNetCore.Authentication.Cookies; -using Microsoft.AspNetCore.Authentication.OpenIdConnect; -using Microsoft.AspNetCore.Builder; -using Microsoft.AspNetCore.Hosting; -using Microsoft.AspNetCore.Http; -using Microsoft.AspNetCore.Mvc; -using Microsoft.Extensions.Configuration; -using Microsoft.Extensions.DependencyInjection; -using Microsoft.Extensions.Hosting; -using System; -using System.Linq; -using System.Net; -using System.Threading; -using WebhookClient.Services; +namespace WebhookClient; -namespace WebhookClient +public class Startup { - public class Startup + public Startup(IConfiguration configuration) { - public Startup(IConfiguration configuration) + Configuration = configuration; + } + + public IConfiguration Configuration { get; } + + // This method gets called by the runtime. Use this method to add services to the container. + public void ConfigureServices(IServiceCollection services) + { + services.AddSession(opt => + { + opt.Cookie.Name = ".eShopWebhooks.Session"; + }) + .AddConfiguration(Configuration) + .AddHttpClientServices(Configuration) + .AddCustomAuthentication(Configuration) + .AddTransient() + .AddSingleton() + .AddMvc() + .SetCompatibilityVersion(CompatibilityVersion.Version_3_0); + + services.AddControllers(); + } + + // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. + public void Configure(IApplicationBuilder app, IWebHostEnvironment env) + { + + var pathBase = Configuration["PATH_BASE"]; + if (!string.IsNullOrEmpty(pathBase)) { - Configuration = configuration; + app.UsePathBase(pathBase); } - public IConfiguration Configuration { get; } - - // This method gets called by the runtime. Use this method to add services to the container. - public void ConfigureServices(IServiceCollection services) + if (env.IsDevelopment()) { - services.AddSession(opt => - { - opt.Cookie.Name = ".eShopWebhooks.Session"; - }) - .AddConfiguration(Configuration) - .AddHttpClientServices(Configuration) - .AddCustomAuthentication(Configuration) - .AddTransient() - .AddSingleton() - .AddMvc() - .SetCompatibilityVersion(CompatibilityVersion.Version_3_0); - - services.AddControllers(); + app.UseDeveloperExceptionPage(); } - - // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. - public void Configure(IApplicationBuilder app, IWebHostEnvironment env) + else { - - var pathBase = Configuration["PATH_BASE"]; - if (!string.IsNullOrEmpty(pathBase)) + app.UseExceptionHandler("/Error"); + // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts. + } + app.Map("/check", capp => + { + capp.Run(async (context) => { - app.UsePathBase(pathBase); - } - - if (env.IsDevelopment()) - { - app.UseDeveloperExceptionPage(); - } - else - { - app.UseExceptionHandler("/Error"); - // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts. - } - app.Map("/check", capp => - { - capp.Run(async (context) => + if ("OPTIONS".Equals(context.Request.Method, StringComparison.InvariantCultureIgnoreCase)) { - if ("OPTIONS".Equals(context.Request.Method, StringComparison.InvariantCultureIgnoreCase)) + var validateToken = bool.TrueString.Equals(Configuration["ValidateToken"], StringComparison.InvariantCultureIgnoreCase); + var header = context.Request.Headers[HeaderNames.WebHookCheckHeader]; + var value = header.FirstOrDefault(); + var tokenToValidate = Configuration["Token"]; + if (!validateToken || value == tokenToValidate) { - var validateToken = bool.TrueString.Equals(Configuration["ValidateToken"], StringComparison.InvariantCultureIgnoreCase); - var header = context.Request.Headers[HeaderNames.WebHookCheckHeader]; - var value = header.FirstOrDefault(); - var tokenToValidate = Configuration["Token"]; - if (!validateToken || value == tokenToValidate) + if (!string.IsNullOrWhiteSpace(tokenToValidate)) { - if (!string.IsNullOrWhiteSpace(tokenToValidate)) - { - context.Response.Headers.Add(HeaderNames.WebHookCheckHeader, tokenToValidate); - } - context.Response.StatusCode = (int)HttpStatusCode.OK; - } - else - { - await context.Response.WriteAsync("Invalid token"); - context.Response.StatusCode = (int)HttpStatusCode.BadRequest; + context.Response.Headers.Add(HeaderNames.WebHookCheckHeader, tokenToValidate); } + context.Response.StatusCode = (int)HttpStatusCode.OK; } else { + await context.Response.WriteAsync("Invalid token"); context.Response.StatusCode = (int)HttpStatusCode.BadRequest; } - }); + } + else + { + context.Response.StatusCode = (int)HttpStatusCode.BadRequest; + } }); + }); - // Fix samesite issue when running eShop from docker-compose locally as by default http protocol is being used - // Refer to https://github.com/dotnet-architecture/eShopOnContainers/issues/1391 - app.UseCookiePolicy(new CookiePolicyOptions { MinimumSameSitePolicy = SameSiteMode.Lax }); + // Fix samesite issue when running eShop from docker-compose locally as by default http protocol is being used + // Refer to https://github.com/dotnet-architecture/eShopOnContainers/issues/1391 + app.UseCookiePolicy(new CookiePolicyOptions { MinimumSameSitePolicy = SameSiteMode.Lax }); - app.UseStaticFiles(); - app.UseSession(); - app.UseRouting(); - app.UseAuthentication(); - app.UseAuthorization(); - app.UseEndpoints(endpoints => - { - endpoints.MapDefaultControllerRoute(); - endpoints.MapRazorPages(); - }); - } - } - - static class ServiceExtensions - { - public static IServiceCollection AddConfiguration(this IServiceCollection services, IConfiguration configuration) + app.UseStaticFiles(); + app.UseSession(); + app.UseRouting(); + app.UseAuthentication(); + app.UseAuthorization(); + app.UseEndpoints(endpoints => { - services.AddOptions(); - services.Configure(configuration); - return services; - } - public static IServiceCollection AddCustomAuthentication(this IServiceCollection services, IConfiguration configuration) - { - var identityUrl = configuration.GetValue("IdentityUrl"); - var callBackUrl = configuration.GetValue("CallBackUrl"); - - // Add Authentication services - - services.AddAuthentication(options => - { - options.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme; - options.DefaultChallengeScheme = OpenIdConnectDefaults.AuthenticationScheme; - }) - .AddCookie(setup => setup.ExpireTimeSpan = TimeSpan.FromHours(2)) - .AddOpenIdConnect(options => - { - options.SignInScheme = CookieAuthenticationDefaults.AuthenticationScheme; - options.Authority = identityUrl.ToString(); - options.SignedOutRedirectUri = callBackUrl.ToString(); - options.ClientId = "webhooksclient"; - options.ClientSecret = "secret"; - options.ResponseType = "code id_token"; - options.SaveTokens = true; - options.GetClaimsFromUserInfoEndpoint = true; - options.RequireHttpsMetadata = false; - options.Scope.Add("openid"); - options.Scope.Add("webhooks"); - }); - - return services; - } - - public static IServiceCollection AddHttpClientServices(this IServiceCollection services, IConfiguration configuration) - { - services.AddSingleton(); - services.AddTransient(); - services.AddHttpClient("extendedhandlerlifetime").SetHandlerLifetime(Timeout.InfiniteTimeSpan); - - //add http client services - services.AddHttpClient("GrantClient") - .SetHandlerLifetime(TimeSpan.FromMinutes(5)) - .AddHttpMessageHandler(); - - return services; - } + endpoints.MapDefaultControllerRoute(); + endpoints.MapRazorPages(); + }); + } +} + +static class ServiceExtensions +{ + public static IServiceCollection AddConfiguration(this IServiceCollection services, IConfiguration configuration) + { + services.AddOptions(); + services.Configure(configuration); + return services; + } + public static IServiceCollection AddCustomAuthentication(this IServiceCollection services, IConfiguration configuration) + { + var identityUrl = configuration.GetValue("IdentityUrl"); + var callBackUrl = configuration.GetValue("CallBackUrl"); + + // Add Authentication services + + services.AddAuthentication(options => + { + options.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme; + options.DefaultChallengeScheme = OpenIdConnectDefaults.AuthenticationScheme; + }) + .AddCookie(setup => setup.ExpireTimeSpan = TimeSpan.FromHours(2)) + .AddOpenIdConnect(options => + { + options.SignInScheme = CookieAuthenticationDefaults.AuthenticationScheme; + options.Authority = identityUrl.ToString(); + options.SignedOutRedirectUri = callBackUrl.ToString(); + options.ClientId = "webhooksclient"; + options.ClientSecret = "secret"; + options.ResponseType = "code id_token"; + options.SaveTokens = true; + options.GetClaimsFromUserInfoEndpoint = true; + options.RequireHttpsMetadata = false; + options.Scope.Add("openid"); + options.Scope.Add("webhooks"); + }); + + return services; + } + + public static IServiceCollection AddHttpClientServices(this IServiceCollection services, IConfiguration configuration) + { + services.AddSingleton(); + services.AddTransient(); + services.AddHttpClient("extendedhandlerlifetime").SetHandlerLifetime(Timeout.InfiniteTimeSpan); + + //add http client services + services.AddHttpClient("GrantClient") + .SetHandlerLifetime(TimeSpan.FromMinutes(5)) + .AddHttpMessageHandler(); + + return services; } }