From a934952816597531bef7ea06bc4cde2cbeb8d35e Mon Sep 17 00:00:00 2001 From: HiveBeats Date: Sun, 13 Oct 2024 01:02:51 +0700 Subject: [PATCH] wip: JWT --- Auths.sln | 22 ++++++++++++ Jwt.Server/Jwt.Server.csproj | 14 ++++++++ Jwt.Server/Jwt.Server.http | 6 ++++ Jwt.Server/Program.cs | 44 +++++++++++++++++++++++ Jwt.Server/Properties/launchSettings.json | 41 +++++++++++++++++++++ Jwt.Server/appsettings.Development.json | 8 +++++ Jwt.Server/appsettings.json | 9 +++++ RsaKeyLoader/Class1.cs | 5 +++ RsaKeyLoader/KeyLoader.cs | 13 +++++++ RsaKeyLoader/RsaKeyLoader.csproj | 9 +++++ 10 files changed, 171 insertions(+) create mode 100644 Jwt.Server/Jwt.Server.csproj create mode 100644 Jwt.Server/Jwt.Server.http create mode 100644 Jwt.Server/Program.cs create mode 100644 Jwt.Server/Properties/launchSettings.json create mode 100644 Jwt.Server/appsettings.Development.json create mode 100644 Jwt.Server/appsettings.json create mode 100644 RsaKeyLoader/Class1.cs create mode 100644 RsaKeyLoader/KeyLoader.cs create mode 100644 RsaKeyLoader/RsaKeyLoader.csproj diff --git a/Auths.sln b/Auths.sln index e6a3afd..dd66bc1 100644 --- a/Auths.sln +++ b/Auths.sln @@ -6,6 +6,14 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Cookie.Basic", "Cookie.Basi EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Cookie.BuiltIn", "Cookie.BuiltIn\Cookie.BuiltIn.csproj", "{63CE5DC9-F976-430B-8B12-50FB3DA3F872}" EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Cookies", "Cookies", "{A7B682CD-CF61-4684-977B-82E48A4051D8}" +EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Jwts", "Jwts", "{29AFF1AF-FE18-491A-AFE1-CE2786187166}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Jwt.Server", "Jwt.Server\Jwt.Server.csproj", "{BAD4810F-B60A-42F1-8176-649855B93794}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "RsaKeyLoader", "RsaKeyLoader\RsaKeyLoader.csproj", "{579C8783-7E95-40F3-96F4-BEBFB40F2D38}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -24,5 +32,19 @@ Global {63CE5DC9-F976-430B-8B12-50FB3DA3F872}.Debug|Any CPU.Build.0 = Debug|Any CPU {63CE5DC9-F976-430B-8B12-50FB3DA3F872}.Release|Any CPU.ActiveCfg = Release|Any CPU {63CE5DC9-F976-430B-8B12-50FB3DA3F872}.Release|Any CPU.Build.0 = Release|Any CPU + {BAD4810F-B60A-42F1-8176-649855B93794}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {BAD4810F-B60A-42F1-8176-649855B93794}.Debug|Any CPU.Build.0 = Debug|Any CPU + {BAD4810F-B60A-42F1-8176-649855B93794}.Release|Any CPU.ActiveCfg = Release|Any CPU + {BAD4810F-B60A-42F1-8176-649855B93794}.Release|Any CPU.Build.0 = Release|Any CPU + {579C8783-7E95-40F3-96F4-BEBFB40F2D38}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {579C8783-7E95-40F3-96F4-BEBFB40F2D38}.Debug|Any CPU.Build.0 = Debug|Any CPU + {579C8783-7E95-40F3-96F4-BEBFB40F2D38}.Release|Any CPU.ActiveCfg = Release|Any CPU + {579C8783-7E95-40F3-96F4-BEBFB40F2D38}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(NestedProjects) = preSolution + {3684B6DC-9301-496C-9832-FFC26084C496} = {A7B682CD-CF61-4684-977B-82E48A4051D8} + {63CE5DC9-F976-430B-8B12-50FB3DA3F872} = {A7B682CD-CF61-4684-977B-82E48A4051D8} + {BAD4810F-B60A-42F1-8176-649855B93794} = {29AFF1AF-FE18-491A-AFE1-CE2786187166} + {579C8783-7E95-40F3-96F4-BEBFB40F2D38} = {29AFF1AF-FE18-491A-AFE1-CE2786187166} EndGlobalSection EndGlobal diff --git a/Jwt.Server/Jwt.Server.csproj b/Jwt.Server/Jwt.Server.csproj new file mode 100644 index 0000000..48e39a5 --- /dev/null +++ b/Jwt.Server/Jwt.Server.csproj @@ -0,0 +1,14 @@ + + + + net8.0 + enable + enable + + + + + + + + diff --git a/Jwt.Server/Jwt.Server.http b/Jwt.Server/Jwt.Server.http new file mode 100644 index 0000000..329a99b --- /dev/null +++ b/Jwt.Server/Jwt.Server.http @@ -0,0 +1,6 @@ +@Jwt.Server_HostAddress = http://localhost:5079 + +GET {{Jwt.Server_HostAddress}}/weatherforecast/ +Accept: application/json + +### diff --git a/Jwt.Server/Program.cs b/Jwt.Server/Program.cs new file mode 100644 index 0000000..161f695 --- /dev/null +++ b/Jwt.Server/Program.cs @@ -0,0 +1,44 @@ +var builder = WebApplication.CreateBuilder(args); + +// Add services to the container. +// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle +builder.Services.AddEndpointsApiExplorer(); +builder.Services.AddSwaggerGen(); + +var app = builder.Build(); + +// Configure the HTTP request pipeline. +if (app.Environment.IsDevelopment()) +{ + app.UseSwagger(); + app.UseSwaggerUI(); +} + +app.UseHttpsRedirection(); + +var summaries = new[] +{ + "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching" +}; + +app.MapGet("/weatherforecast", () => + { + var forecast = Enumerable.Range(1, 5).Select(index => + new WeatherForecast + ( + DateOnly.FromDateTime(DateTime.Now.AddDays(index)), + Random.Shared.Next(-20, 55), + summaries[Random.Shared.Next(summaries.Length)] + )) + .ToArray(); + return forecast; + }) + .WithName("GetWeatherForecast") + .WithOpenApi(); + +app.Run(); + +record WeatherForecast(DateOnly Date, int TemperatureC, string? Summary) +{ + public int TemperatureF => 32 + (int)(TemperatureC / 0.5556); +} \ No newline at end of file diff --git a/Jwt.Server/Properties/launchSettings.json b/Jwt.Server/Properties/launchSettings.json new file mode 100644 index 0000000..c9f4735 --- /dev/null +++ b/Jwt.Server/Properties/launchSettings.json @@ -0,0 +1,41 @@ +{ + "$schema": "http://json.schemastore.org/launchsettings.json", + "iisSettings": { + "windowsAuthentication": false, + "anonymousAuthentication": true, + "iisExpress": { + "applicationUrl": "http://localhost:65145", + "sslPort": 44390 + } + }, + "profiles": { + "http": { + "commandName": "Project", + "dotnetRunMessages": true, + "launchBrowser": true, + "launchUrl": "swagger", + "applicationUrl": "http://localhost:5079", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + }, + "https": { + "commandName": "Project", + "dotnetRunMessages": true, + "launchBrowser": true, + "launchUrl": "swagger", + "applicationUrl": "https://localhost:7256;http://localhost:5079", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + }, + "IIS Express": { + "commandName": "IISExpress", + "launchBrowser": true, + "launchUrl": "swagger", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + } + } +} diff --git a/Jwt.Server/appsettings.Development.json b/Jwt.Server/appsettings.Development.json new file mode 100644 index 0000000..0c208ae --- /dev/null +++ b/Jwt.Server/appsettings.Development.json @@ -0,0 +1,8 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + } +} diff --git a/Jwt.Server/appsettings.json b/Jwt.Server/appsettings.json new file mode 100644 index 0000000..10f68b8 --- /dev/null +++ b/Jwt.Server/appsettings.json @@ -0,0 +1,9 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + }, + "AllowedHosts": "*" +} diff --git a/RsaKeyLoader/Class1.cs b/RsaKeyLoader/Class1.cs new file mode 100644 index 0000000..2b23f44 --- /dev/null +++ b/RsaKeyLoader/Class1.cs @@ -0,0 +1,5 @@ +namespace RsaKeyLoader; + +public class Class1 +{ +} \ No newline at end of file diff --git a/RsaKeyLoader/KeyLoader.cs b/RsaKeyLoader/KeyLoader.cs new file mode 100644 index 0000000..7d65518 --- /dev/null +++ b/RsaKeyLoader/KeyLoader.cs @@ -0,0 +1,13 @@ +using System.Security.Cryptography; + +namespace RsaKeyLoader; + +public class KeyLoader +{ + public void Generate(string path) + { + var rsaKey = RSA.Create(); + var privateKey = rsaKey.ExportRSAPrivateKey(); + File.WriteAllBytes(path, privateKey); + } +} \ No newline at end of file diff --git a/RsaKeyLoader/RsaKeyLoader.csproj b/RsaKeyLoader/RsaKeyLoader.csproj new file mode 100644 index 0000000..3a63532 --- /dev/null +++ b/RsaKeyLoader/RsaKeyLoader.csproj @@ -0,0 +1,9 @@ + + + + net8.0 + enable + enable + + +