From 3f51463ffdd28317975ff8e0d2c0e769ff6bbf2a Mon Sep 17 00:00:00 2001 From: MRmlik12 Date: Sun, 9 Jan 2022 14:32:30 +0100 Subject: [PATCH] Add usefull extensions --- src/Vulder.SharedKernel/Extensions.cs | 44 +++++++++++++++++++ src/Vulder.SharedKernel/Regexes.cs | 9 ---- .../Vulder.SharedKernel.csproj | 7 ++- 3 files changed, 49 insertions(+), 11 deletions(-) create mode 100644 src/Vulder.SharedKernel/Extensions.cs delete mode 100755 src/Vulder.SharedKernel/Regexes.cs diff --git a/src/Vulder.SharedKernel/Extensions.cs b/src/Vulder.SharedKernel/Extensions.cs new file mode 100644 index 0000000..f74f04d --- /dev/null +++ b/src/Vulder.SharedKernel/Extensions.cs @@ -0,0 +1,44 @@ +using System.Text; +using Microsoft.AspNetCore.Authentication.JwtBearer; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.IdentityModel.Tokens; + +namespace Vulder.SharedKernel; + +public static class Extensions +{ + public static void AddDefaultJwtConfiguration(this IServiceCollection services, IConfiguration configuration) + { + var authSection = configuration.GetSection("Jwt"); + services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme) + .AddJwtBearer(JwtBearerDefaults.AuthenticationScheme, options => + { + options.SaveToken = true; + options.TokenValidationParameters = new TokenValidationParameters + { + ValidateIssuerSigningKey = true, + IssuerSigningKey = + new SymmetricSecurityKey(Encoding.UTF8.GetBytes(authSection.GetValue("Key"))), + ValidateIssuer = true, + ValidIssuer = authSection.GetValue("Issuer"), + ValidateAudience = true, + ValidAudience = authSection.GetValue("Audience"), + RequireExpirationTime = true, + ValidateLifetime = true + }; + }); + } + + public static void AddDefaultCorsPolicy(this IServiceCollection services) + { + services.AddCors(options => + { + options.AddPolicy("CORS", corsPolicyBuilder => + { + corsPolicyBuilder.AllowAnyOrigin() + .AllowAnyHeader() + .AllowAnyMethod(); + }); + }); + } +} \ No newline at end of file diff --git a/src/Vulder.SharedKernel/Regexes.cs b/src/Vulder.SharedKernel/Regexes.cs deleted file mode 100755 index ec8d5b1..0000000 --- a/src/Vulder.SharedKernel/Regexes.cs +++ /dev/null @@ -1,9 +0,0 @@ -namespace Vulder.SharedKernel -{ - public static class Regexes - { - public const string Email = "^\\w+([-+.']\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$"; - public const string Url = - "^(ht|f)tp(s?)\\:\\/\\/[0-9a-zA-Z]([-.\\w]*[0-9a-zA-Z])*(:(0-9)*)*(\\/?)([a-zA-Z0-9\\-\\.\\?\\,\'\\/\\\\+&%\\$#_]*)?$"; - } -} \ No newline at end of file diff --git a/src/Vulder.SharedKernel/Vulder.SharedKernel.csproj b/src/Vulder.SharedKernel/Vulder.SharedKernel.csproj index a3e60d4..d76f744 100644 --- a/src/Vulder.SharedKernel/Vulder.SharedKernel.csproj +++ b/src/Vulder.SharedKernel/Vulder.SharedKernel.csproj @@ -1,16 +1,19 @@ - net5.0 + net6.0 + + + - net5.0 + net6.0 0.1.6 Vulder.SharedKernel MRmlik12