From e33f7840b02b4396f18cc7b9623c9a957482983a Mon Sep 17 00:00:00 2001 From: tonyfages Date: Sat, 16 Mar 2024 14:04:09 +0100 Subject: [PATCH] =?UTF-8?q?Add=20Auth=20=E2=9C=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Verax_API_EF/Verax_API_EF/API/API.csproj | 2 +- .../API/Entity_FrameWork.Article.db | Bin 49152 -> 49152 bytes .../API/Entity_FrameWork.Article.db-shm | Bin 32768 -> 32768 bytes .../API/Entity_FrameWork.Article.db-wal | Bin 57712 -> 0 bytes Verax_API_EF/Verax_API_EF/API/Program.cs | 69 +++++++++++- Verax_API_EF/Verax_API_EF/API/log.txt | 106 ++++++++++++++++++ .../API_DbDataManager.csproj | 2 +- .../DbContextLib/ApplicationDbContext.cs | 12 ++ .../DbContextLib/DbContextLib.csproj | 2 +- .../Verax_API_EF/Entities/Entities.csproj | 2 +- .../StubbedContextLib.csproj | 2 +- .../Test_Console_EF/Test_Console_EF.csproj | 2 +- .../Unit_Test_EF/Unit_Test_EF.csproj | 2 +- 13 files changed, 191 insertions(+), 10 deletions(-) create mode 100644 Verax_API_EF/Verax_API_EF/DbContextLib/ApplicationDbContext.cs diff --git a/Verax_API_EF/Verax_API_EF/API/API.csproj b/Verax_API_EF/Verax_API_EF/API/API.csproj index 1a9894b..263970e 100644 --- a/Verax_API_EF/Verax_API_EF/API/API.csproj +++ b/Verax_API_EF/Verax_API_EF/API/API.csproj @@ -11,11 +11,11 @@ - all runtime; build; native; contentfiles; analyzers; buildtransitive + all diff --git a/Verax_API_EF/Verax_API_EF/API/Entity_FrameWork.Article.db b/Verax_API_EF/Verax_API_EF/API/Entity_FrameWork.Article.db index 175a9e3bfb33888228bcc9beffeb72eedaac90e5..c5f5399327cccc0df8a71c39b5c8df3bfcf05006 100644 GIT binary patch delta 236 zcmZo@U~Xt&-oRKO%>RvnmH!X}|116n{MY!;@*mo z^_nsa3=CYHU_KZBKA@gW4E!(o@9}Tqzs!Gfvp~ZFesOLVMsY?auo8BlvD^^dY?Dv+ z>q;PW^YUM2VB)W2;9tgH$zKVyE1Q3E<@_iXpbFi|*XL^q^Irs-ew=~-EB_n*C;Yeh zkMkb~s@lvi!U=N_JIFM!J#3S2uUF&c{|PklB?JFQpo?B^7F4*wKl#~yVUQ~J;EY57 D8)!x} delta 183 zcmZo@U~Xt&-oRKO$iTqB%72!D{~P}+{s;Wm_|I+@6xhu_`E0$W3h!HOt_CIA2c diff --git a/Verax_API_EF/Verax_API_EF/API/Entity_FrameWork.Article.db-shm b/Verax_API_EF/Verax_API_EF/API/Entity_FrameWork.Article.db-shm index d5fc9fbb704b9577a86e5b9773468d5275689d97..fe9ac2845eca6fe6da8a63cd096d9cf9e24ece10 100644 GIT binary patch delta 80 zcmZo@U}|V!;+1%$%K!t66Awy@TChv7nNGgVi7p`mlYpuI4+Il)WHw$<2;X=iLIVJ* Cu@r>> delta 243 zcmZo@U}|V!s+V}A%K!qbK+MR%AixKtd4Tx)Z#j$SMxX3vZ&TqmHQuEh{4&Sk?DgMz zq^buR1qL8<|04mY@WgsfQw|`T6NtHim>4z}P#w?4M&odHMg|rJb_TAEjo+ErnSfs4 eVBiL^8F@E0{$*lkW?%!F3sead;oI0KE&~AC+Bb0k diff --git a/Verax_API_EF/Verax_API_EF/API/Entity_FrameWork.Article.db-wal b/Verax_API_EF/Verax_API_EF/API/Entity_FrameWork.Article.db-wal index 7605da657bc9a544f302ea364306a208df39841d..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 100644 GIT binary patch literal 0 HcmV?d00001 literal 57712 zcmeI*O-K}B9LMo_+qYSlCs;%!5KC{Owk}dcdJ+jDjWjPE!qiwXT{FqN6x5~Yq{oa1 zdbxxiBn&r_l0rQgsSrYFQfX`$QK1oCdS-QX6$Ozc2K+vkeP;G?_IWE8dAQ z*`*ZCG>yGPgUG}QNO_iFd=^1Ky zxTdnAF&DRBQcHXga1P(E%h{1T0z*Z||G6*1m00IagfB*srAbt4)5I_I{1Q0*~0R#|0009IL zu)Tmsk5>wIlp{EMW#WlH@NKV^BXEkLwfzW|(;3@79W?p9B z9&oMpoIA=9v{hxEc(Z@1-4}cAU_y>yh#Y|(zj)?^00IagfB*srAbj$pU{(pWzN2q1s}0tg_000IagfPftY z5_1IdpZeb9?nm&TH1$H~5}Gm?Ib_M_`96<;9FUlwj{$_i$c)?ch`ZD?t02sej9Z7qij3eL$lg88-K zrpi!Fu9h&kaErl(D}&`M^H#4M*kW*CZE%YGgOqq(;85n@-z7f_znJ2WtP4aH@k1sF sqRCu1*FpdR1Q0*~0R#|0009ILKmdXNF5uGrs;n^RE3cZIfJ+|#1EYCvcK`qY diff --git a/Verax_API_EF/Verax_API_EF/API/Program.cs b/Verax_API_EF/Verax_API_EF/API/Program.cs index 06fffd8..63c6025 100644 --- a/Verax_API_EF/Verax_API_EF/API/Program.cs +++ b/Verax_API_EF/Verax_API_EF/API/Program.cs @@ -1,20 +1,23 @@ using API_Services; using DbContextLib; using DbDataManager; +using Microsoft.AspNetCore.Identity; +using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; +using Microsoft.OpenApi.Models; using Model; using StubbedContextLib; var builder = WebApplication.CreateBuilder(args); +// Add services to the container. +// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle -// Add services to the container. -// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle builder.Services.AddControllers(); +builder.Services.AddSwaggerGen(); builder.Services.AddEndpointsApiExplorer(); -builder.Services.AddSwaggerGen(); builder.Services.AddDbContext(options => { @@ -23,10 +26,54 @@ builder.Services.AddDbContext(options => }); builder.Services.AddScoped(); +// Add Authorization +builder.Services.AddDbContext( + options => options.UseInMemoryDatabase("AppDb")); +builder.Services.AddAuthorization(); +builder.Services.AddIdentityApiEndpoints() + .AddEntityFrameworkStores(); + + + + + + + +builder.Services.AddSwaggerGen(option => +{ + option.SwaggerDoc("v1", new OpenApiInfo { Title = "Demo API", Version = "v1" }); + option.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme + { + In = ParameterLocation.Header, + Description = "Please enter a valid token", + Name = "Authorization", + Type = SecuritySchemeType.Http, + BearerFormat = "JWT", + Scheme = "Bearer" + }); + option.AddSecurityRequirement(new OpenApiSecurityRequirement + { + { + new OpenApiSecurityScheme + { + Reference = new OpenApiReference + { + Type=ReferenceType.SecurityScheme, + Id="Bearer" + } + }, + new string[]{} + } + }); +}); var app = builder.Build(); +app.MapIdentityApi(); + +app.MapSwagger().RequireAuthorization(); + // Configure the HTTP request pipeline. if (app.Environment.IsDevelopment()) { @@ -35,12 +82,28 @@ if (app.Environment.IsDevelopment()) } app.UseHttpsRedirection(); +app.UseAuthorization(); app.MapControllers(); using var scoped = app.Services.CreateScope(); var libraryContext = scoped.ServiceProvider.GetService(); //libraryContext.Database.EnsureCreated(); libraryContext.Database.Migrate(); + +app.MapPost("/logout", async (SignInManager signInManager, + [FromBody]object empty) => + { + if (empty != null) + { + await signInManager.SignOutAsync(); + return Results.Ok(); + } + return Results.Unauthorized(); + }) + .WithOpenApi() + .RequireAuthorization(); + + app.Run(); diff --git a/Verax_API_EF/Verax_API_EF/API/log.txt b/Verax_API_EF/Verax_API_EF/API/log.txt index 7d66f59..50bfd6e 100644 --- a/Verax_API_EF/Verax_API_EF/API/log.txt +++ b/Verax_API_EF/Verax_API_EF/API/log.txt @@ -32,3 +32,109 @@ info: 03/15/2024 17:08:33.305 RelationalEventId.CommandExecuted[20101] (Microsof Executed DbCommand (0ms) [Parameters=[], CommandType='Text', CommandTimeout='30'] SELECT "a"."Id", "a"."Author", "a"."DatePublished", "a"."Description", "a"."LectureTime", "a"."Title" FROM "ArticleSet" AS "a" +info: 03/16/2024 13:47:23.492 RelationalEventId.CommandExecuted[20101] (Microsoft.EntityFrameworkCore.Database.Command) + Executed DbCommand (6ms) [Parameters=[], CommandType='Text', CommandTimeout='30'] + SELECT COUNT(*) FROM "sqlite_master" WHERE "name" = '__EFMigrationsHistory' AND "type" = 'table'; +info: 03/16/2024 13:47:23.496 RelationalEventId.CommandExecuted[20101] (Microsoft.EntityFrameworkCore.Database.Command) + Executed DbCommand (0ms) [Parameters=[], CommandType='Text', CommandTimeout='30'] + SELECT COUNT(*) FROM "sqlite_master" WHERE "name" = '__EFMigrationsHistory' AND "type" = 'table'; +info: 03/16/2024 13:47:23.503 RelationalEventId.CommandExecuted[20101] (Microsoft.EntityFrameworkCore.Database.Command) + Executed DbCommand (0ms) [Parameters=[], CommandType='Text', CommandTimeout='30'] + SELECT "MigrationId", "ProductVersion" + FROM "__EFMigrationsHistory" + ORDER BY "MigrationId"; +info: 03/16/2024 13:47:23.511 RelationalEventId.MigrationsNotApplied[20405] (Microsoft.EntityFrameworkCore.Migrations) + No migrations were applied. The database is already up to date. +info: 03/16/2024 13:49:15.334 RelationalEventId.CommandExecuted[20101] (Microsoft.EntityFrameworkCore.Database.Command) + Executed DbCommand (0ms) [Parameters=[], CommandType='Text', CommandTimeout='30'] + SELECT "a"."Id", "a"."Author", "a"."DatePublished", "a"."Description", "a"."LectureTime", "a"."Title" + FROM "ArticleSet" AS "a" +info: 03/16/2024 13:50:41.465 RelationalEventId.CommandExecuted[20101] (Microsoft.EntityFrameworkCore.Database.Command) + Executed DbCommand (5ms) [Parameters=[], CommandType='Text', CommandTimeout='30'] + SELECT COUNT(*) FROM "sqlite_master" WHERE "name" = '__EFMigrationsHistory' AND "type" = 'table'; +info: 03/16/2024 13:50:41.469 RelationalEventId.CommandExecuted[20101] (Microsoft.EntityFrameworkCore.Database.Command) + Executed DbCommand (0ms) [Parameters=[], CommandType='Text', CommandTimeout='30'] + SELECT COUNT(*) FROM "sqlite_master" WHERE "name" = '__EFMigrationsHistory' AND "type" = 'table'; +info: 03/16/2024 13:50:41.475 RelationalEventId.CommandExecuted[20101] (Microsoft.EntityFrameworkCore.Database.Command) + Executed DbCommand (0ms) [Parameters=[], CommandType='Text', CommandTimeout='30'] + SELECT "MigrationId", "ProductVersion" + FROM "__EFMigrationsHistory" + ORDER BY "MigrationId"; +info: 03/16/2024 13:50:41.483 RelationalEventId.MigrationsNotApplied[20405] (Microsoft.EntityFrameworkCore.Migrations) + No migrations were applied. The database is already up to date. +info: 03/16/2024 13:53:25.287 RelationalEventId.CommandExecuted[20101] (Microsoft.EntityFrameworkCore.Database.Command) + Executed DbCommand (6ms) [Parameters=[], CommandType='Text', CommandTimeout='30'] + SELECT COUNT(*) FROM "sqlite_master" WHERE "name" = '__EFMigrationsHistory' AND "type" = 'table'; +info: 03/16/2024 13:53:25.291 RelationalEventId.CommandExecuted[20101] (Microsoft.EntityFrameworkCore.Database.Command) + Executed DbCommand (0ms) [Parameters=[], CommandType='Text', CommandTimeout='30'] + SELECT COUNT(*) FROM "sqlite_master" WHERE "name" = '__EFMigrationsHistory' AND "type" = 'table'; +info: 03/16/2024 13:53:25.298 RelationalEventId.CommandExecuted[20101] (Microsoft.EntityFrameworkCore.Database.Command) + Executed DbCommand (0ms) [Parameters=[], CommandType='Text', CommandTimeout='30'] + SELECT "MigrationId", "ProductVersion" + FROM "__EFMigrationsHistory" + ORDER BY "MigrationId"; +info: 03/16/2024 13:53:25.307 RelationalEventId.MigrationsNotApplied[20405] (Microsoft.EntityFrameworkCore.Migrations) + No migrations were applied. The database is already up to date. +warn: 03/16/2024 13:55:55.456 CoreEventId.RowLimitingOperationWithoutOrderByWarning[10102] (Microsoft.EntityFrameworkCore.Query) + The query uses a row limiting operator ('Skip'/'Take') without an 'OrderBy' operator. This may lead to unpredictable results. If the 'Distinct' operator is used after 'OrderBy', then make sure to use the 'OrderBy' operator after 'Distinct' as the ordering would otherwise get erased. +info: 03/16/2024 13:55:55.509 RelationalEventId.CommandExecuted[20101] (Microsoft.EntityFrameworkCore.Database.Command) + Executed DbCommand (2ms) [Parameters=[@__p_0='?' (DbType = Int32)], CommandType='Text', CommandTimeout='30'] + SELECT "f"."Id", "f"."DatePublication", "f"."Link", "f"."Theme", "f"."UserEntityPseudo" + FROM "FormSet" AS "f" + LIMIT -1 OFFSET @__p_0 +info: 03/16/2024 13:58:19.293 RelationalEventId.CommandExecuted[20101] (Microsoft.EntityFrameworkCore.Database.Command) + Executed DbCommand (6ms) [Parameters=[], CommandType='Text', CommandTimeout='30'] + SELECT COUNT(*) FROM "sqlite_master" WHERE "name" = '__EFMigrationsHistory' AND "type" = 'table'; +info: 03/16/2024 13:58:19.297 RelationalEventId.CommandExecuted[20101] (Microsoft.EntityFrameworkCore.Database.Command) + Executed DbCommand (0ms) [Parameters=[], CommandType='Text', CommandTimeout='30'] + SELECT COUNT(*) FROM "sqlite_master" WHERE "name" = '__EFMigrationsHistory' AND "type" = 'table'; +info: 03/16/2024 13:58:19.304 RelationalEventId.CommandExecuted[20101] (Microsoft.EntityFrameworkCore.Database.Command) + Executed DbCommand (0ms) [Parameters=[], CommandType='Text', CommandTimeout='30'] + SELECT "MigrationId", "ProductVersion" + FROM "__EFMigrationsHistory" + ORDER BY "MigrationId"; +info: 03/16/2024 13:58:19.312 RelationalEventId.MigrationsNotApplied[20405] (Microsoft.EntityFrameworkCore.Migrations) + No migrations were applied. The database is already up to date. +info: 03/16/2024 13:59:32.285 RelationalEventId.CommandExecuted[20101] (Microsoft.EntityFrameworkCore.Database.Command) + Executed DbCommand (6ms) [Parameters=[], CommandType='Text', CommandTimeout='30'] + SELECT COUNT(*) FROM "sqlite_master" WHERE "name" = '__EFMigrationsHistory' AND "type" = 'table'; +info: 03/16/2024 13:59:32.289 RelationalEventId.CommandExecuted[20101] (Microsoft.EntityFrameworkCore.Database.Command) + Executed DbCommand (0ms) [Parameters=[], CommandType='Text', CommandTimeout='30'] + SELECT COUNT(*) FROM "sqlite_master" WHERE "name" = '__EFMigrationsHistory' AND "type" = 'table'; +info: 03/16/2024 13:59:32.296 RelationalEventId.CommandExecuted[20101] (Microsoft.EntityFrameworkCore.Database.Command) + Executed DbCommand (0ms) [Parameters=[], CommandType='Text', CommandTimeout='30'] + SELECT "MigrationId", "ProductVersion" + FROM "__EFMigrationsHistory" + ORDER BY "MigrationId"; +info: 03/16/2024 13:59:32.303 RelationalEventId.MigrationsNotApplied[20405] (Microsoft.EntityFrameworkCore.Migrations) + No migrations were applied. The database is already up to date. +info: 03/16/2024 14:00:53.457 RelationalEventId.CommandExecuted[20101] (Microsoft.EntityFrameworkCore.Database.Command) + Executed DbCommand (5ms) [Parameters=[], CommandType='Text', CommandTimeout='30'] + SELECT COUNT(*) FROM "sqlite_master" WHERE "name" = '__EFMigrationsHistory' AND "type" = 'table'; +info: 03/16/2024 14:00:53.462 RelationalEventId.CommandExecuted[20101] (Microsoft.EntityFrameworkCore.Database.Command) + Executed DbCommand (0ms) [Parameters=[], CommandType='Text', CommandTimeout='30'] + SELECT COUNT(*) FROM "sqlite_master" WHERE "name" = '__EFMigrationsHistory' AND "type" = 'table'; +info: 03/16/2024 14:00:53.468 RelationalEventId.CommandExecuted[20101] (Microsoft.EntityFrameworkCore.Database.Command) + Executed DbCommand (0ms) [Parameters=[], CommandType='Text', CommandTimeout='30'] + SELECT "MigrationId", "ProductVersion" + FROM "__EFMigrationsHistory" + ORDER BY "MigrationId"; +info: 03/16/2024 14:00:53.476 RelationalEventId.MigrationsNotApplied[20405] (Microsoft.EntityFrameworkCore.Migrations) + No migrations were applied. The database is already up to date. +info: 03/16/2024 14:01:22.357 RelationalEventId.CommandExecuted[20101] (Microsoft.EntityFrameworkCore.Database.Command) + Executed DbCommand (0ms) [Parameters=[], CommandType='Text', CommandTimeout='30'] + SELECT "a"."Id", "a"."Author", "a"."DatePublished", "a"."Description", "a"."LectureTime", "a"."Title" + FROM "ArticleSet" AS "a" +info: 03/16/2024 14:02:47.267 RelationalEventId.CommandExecuted[20101] (Microsoft.EntityFrameworkCore.Database.Command) + Executed DbCommand (0ms) [Parameters=[], CommandType='Text', CommandTimeout='30'] + SELECT "a"."Id", "a"."Author", "a"."DatePublished", "a"."Description", "a"."LectureTime", "a"."Title" + FROM "ArticleSet" AS "a" +info: 03/16/2024 14:03:10.579 RelationalEventId.CommandExecuted[20101] (Microsoft.EntityFrameworkCore.Database.Command) + Executed DbCommand (0ms) [Parameters=[], CommandType='Text', CommandTimeout='30'] + SELECT "a"."Id", "a"."Author", "a"."DatePublished", "a"."Description", "a"."LectureTime", "a"."Title" + FROM "ArticleSet" AS "a" +info: 03/16/2024 14:03:15.675 RelationalEventId.CommandExecuted[20101] (Microsoft.EntityFrameworkCore.Database.Command) + Executed DbCommand (0ms) [Parameters=[], CommandType='Text', CommandTimeout='30'] + SELECT "a"."Id", "a"."Author", "a"."DatePublished", "a"."Description", "a"."LectureTime", "a"."Title" + FROM "ArticleSet" AS "a" + ORDER BY "a"."Title" diff --git a/Verax_API_EF/Verax_API_EF/API_DbDataManager/API_DbDataManager.csproj b/Verax_API_EF/Verax_API_EF/API_DbDataManager/API_DbDataManager.csproj index a020201..7933319 100644 --- a/Verax_API_EF/Verax_API_EF/API_DbDataManager/API_DbDataManager.csproj +++ b/Verax_API_EF/Verax_API_EF/API_DbDataManager/API_DbDataManager.csproj @@ -15,7 +15,7 @@ - + all diff --git a/Verax_API_EF/Verax_API_EF/DbContextLib/ApplicationDbContext.cs b/Verax_API_EF/Verax_API_EF/DbContextLib/ApplicationDbContext.cs new file mode 100644 index 0000000..66e1b58 --- /dev/null +++ b/Verax_API_EF/Verax_API_EF/DbContextLib/ApplicationDbContext.cs @@ -0,0 +1,12 @@ +using Microsoft.AspNetCore.Identity; +using Microsoft.AspNetCore.Identity.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore; + +namespace DbContextLib; + +public class ApplicationDbContext : IdentityDbContext +{ + public ApplicationDbContext(DbContextOptions options) : + base(options) + { } +} \ No newline at end of file diff --git a/Verax_API_EF/Verax_API_EF/DbContextLib/DbContextLib.csproj b/Verax_API_EF/Verax_API_EF/DbContextLib/DbContextLib.csproj index ad20d53..540a3c5 100644 --- a/Verax_API_EF/Verax_API_EF/DbContextLib/DbContextLib.csproj +++ b/Verax_API_EF/Verax_API_EF/DbContextLib/DbContextLib.csproj @@ -7,7 +7,7 @@ - + diff --git a/Verax_API_EF/Verax_API_EF/Entities/Entities.csproj b/Verax_API_EF/Verax_API_EF/Entities/Entities.csproj index e547c5d..dd967bc 100644 --- a/Verax_API_EF/Verax_API_EF/Entities/Entities.csproj +++ b/Verax_API_EF/Verax_API_EF/Entities/Entities.csproj @@ -7,7 +7,7 @@ - + diff --git a/Verax_API_EF/Verax_API_EF/StubbedContextLib/StubbedContextLib.csproj b/Verax_API_EF/Verax_API_EF/StubbedContextLib/StubbedContextLib.csproj index 6c03da9..35518c0 100644 --- a/Verax_API_EF/Verax_API_EF/StubbedContextLib/StubbedContextLib.csproj +++ b/Verax_API_EF/Verax_API_EF/StubbedContextLib/StubbedContextLib.csproj @@ -12,7 +12,7 @@ - + diff --git a/Verax_API_EF/Verax_API_EF/Test_Console_EF/Test_Console_EF.csproj b/Verax_API_EF/Verax_API_EF/Test_Console_EF/Test_Console_EF.csproj index bd068dc..3a5c52b 100644 --- a/Verax_API_EF/Verax_API_EF/Test_Console_EF/Test_Console_EF.csproj +++ b/Verax_API_EF/Verax_API_EF/Test_Console_EF/Test_Console_EF.csproj @@ -9,7 +9,7 @@ - + diff --git a/Verax_API_EF/Verax_API_EF/Unit_Test_EF/Unit_Test_EF.csproj b/Verax_API_EF/Verax_API_EF/Unit_Test_EF/Unit_Test_EF.csproj index fc4de9b..8cbb5d2 100644 --- a/Verax_API_EF/Verax_API_EF/Unit_Test_EF/Unit_Test_EF.csproj +++ b/Verax_API_EF/Verax_API_EF/Unit_Test_EF/Unit_Test_EF.csproj @@ -12,7 +12,7 @@ - +