From 3aadfa5a877300deceebe8841d11da07a3e7103f Mon Sep 17 00:00:00 2001 From: maliming Date: Sat, 27 Apr 2024 10:05:04 +0800 Subject: [PATCH] Use the latest `Microsoft.IdentityModel.*` packages. Update `OpenIddict` apps. --- Directory.Packages.props | 1 + ...AspNetCore.Authentication.JwtBearer.csproj | 3 + ...etCore.Authentication.OpenIdConnect.csproj | 1 + .../OpenIddict.Demo.API.csproj | 3 +- .../OpenIddictApiModule.cs | 14 +++++ .../app/OpenIddict.Demo.API/Program.cs | 18 ++++-- .../OpenIddict.Demo.Client.Mvc.csproj | 3 +- .../OpenIddictMvcModule.cs | 14 +++++ .../Pages/Index.cshtml | 3 +- .../Pages/Shared/_Layout.cshtml | 3 +- .../app/OpenIddict.Demo.Client.Mvc/Program.cs | 11 ++-- ....cs => 20240427010513_Initial.Designer.cs} | 61 ++++++++++++++++++- ...6_Initial.cs => 20240427010513_Initial.cs} | 44 +++++++++++++ .../ServerDbContextModelSnapshot.cs | 59 ++++++++++++++++++ .../OpenIddict.Demo.Server.csproj | 8 --- .../app/OpenIddict.Demo.Server/Program.cs | 7 --- 16 files changed, 221 insertions(+), 32 deletions(-) create mode 100644 modules/openiddict/app/OpenIddict.Demo.API/OpenIddictApiModule.cs create mode 100644 modules/openiddict/app/OpenIddict.Demo.Client.Mvc/OpenIddictMvcModule.cs rename modules/openiddict/app/OpenIddict.Demo.Server/Migrations/{20240108043816_Initial.Designer.cs => 20240427010513_Initial.Designer.cs} (96%) rename modules/openiddict/app/OpenIddict.Demo.Server/Migrations/{20240108043816_Initial.cs => 20240427010513_Initial.cs} (95%) diff --git a/Directory.Packages.props b/Directory.Packages.props index 1cb2ae0b88..920232ee86 100644 --- a/Directory.Packages.props +++ b/Directory.Packages.props @@ -106,6 +106,7 @@ + diff --git a/framework/src/Volo.Abp.AspNetCore.Authentication.JwtBearer/Volo.Abp.AspNetCore.Authentication.JwtBearer.csproj b/framework/src/Volo.Abp.AspNetCore.Authentication.JwtBearer/Volo.Abp.AspNetCore.Authentication.JwtBearer.csproj index d43da06dde..4d7b852b9c 100644 --- a/framework/src/Volo.Abp.AspNetCore.Authentication.JwtBearer/Volo.Abp.AspNetCore.Authentication.JwtBearer.csproj +++ b/framework/src/Volo.Abp.AspNetCore.Authentication.JwtBearer/Volo.Abp.AspNetCore.Authentication.JwtBearer.csproj @@ -23,6 +23,9 @@ + + + diff --git a/framework/src/Volo.Abp.AspNetCore.Authentication.OpenIdConnect/Volo.Abp.AspNetCore.Authentication.OpenIdConnect.csproj b/framework/src/Volo.Abp.AspNetCore.Authentication.OpenIdConnect/Volo.Abp.AspNetCore.Authentication.OpenIdConnect.csproj index 702b1e2ed5..4542fc4cb5 100644 --- a/framework/src/Volo.Abp.AspNetCore.Authentication.OpenIdConnect/Volo.Abp.AspNetCore.Authentication.OpenIdConnect.csproj +++ b/framework/src/Volo.Abp.AspNetCore.Authentication.OpenIdConnect/Volo.Abp.AspNetCore.Authentication.OpenIdConnect.csproj @@ -16,6 +16,7 @@ + diff --git a/modules/openiddict/app/OpenIddict.Demo.API/OpenIddict.Demo.API.csproj b/modules/openiddict/app/OpenIddict.Demo.API/OpenIddict.Demo.API.csproj index a04d196bf0..cf0cd880b2 100644 --- a/modules/openiddict/app/OpenIddict.Demo.API/OpenIddict.Demo.API.csproj +++ b/modules/openiddict/app/OpenIddict.Demo.API/OpenIddict.Demo.API.csproj @@ -8,7 +8,8 @@ - + + diff --git a/modules/openiddict/app/OpenIddict.Demo.API/OpenIddictApiModule.cs b/modules/openiddict/app/OpenIddict.Demo.API/OpenIddictApiModule.cs new file mode 100644 index 0000000000..cdf2399808 --- /dev/null +++ b/modules/openiddict/app/OpenIddict.Demo.API/OpenIddictApiModule.cs @@ -0,0 +1,14 @@ +using Volo.Abp.AspNetCore.Authentication.JwtBearer; +using Volo.Abp.AspNetCore.Mvc; +using Volo.Abp.Modularity; + +namespace OpenIddict.Demo.API; + +[DependsOn( + typeof(AbpAspNetCoreMvcModule), + typeof(AbpAspNetCoreAuthenticationJwtBearerModule) +)] +public class OpenIddictApiModule : AbpModule +{ + +} diff --git a/modules/openiddict/app/OpenIddict.Demo.API/Program.cs b/modules/openiddict/app/OpenIddict.Demo.API/Program.cs index c40a46ad60..4d56464b50 100644 --- a/modules/openiddict/app/OpenIddict.Demo.API/Program.cs +++ b/modules/openiddict/app/OpenIddict.Demo.API/Program.cs @@ -1,4 +1,5 @@ using Microsoft.AspNetCore.Authentication.JwtBearer; +using OpenIddict.Demo.API; var builder = WebApplication.CreateBuilder(args); builder.Logging.ClearProviders(); @@ -22,13 +23,15 @@ builder.Services.AddEndpointsApiExplorer(); builder.Services.AddSwaggerGen(); builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme) - .AddJwtBearer(options => + .AddAbpJwtBearer(options => { options.Authority = "https://localhost:44301"; options.Audience = "AbpAPIResource"; }); +await builder.AddApplicationAsync(); var app = builder.Build(); +await app.InitializeApplicationAsync(); // Configure the HTTP request pipeline. if (app.Environment.IsDevelopment()) @@ -38,11 +41,16 @@ if (app.Environment.IsDevelopment()) } app.UseHttpsRedirection(); - +app.UseRouting(); app.UseCors(); app.UseAuthentication(); app.UseAuthorization(); - -app.MapControllers(); - +app.UseConfiguredEndpoints(options => +{ + options.MapFallback("{**slug}", context => + { + context.Response.Redirect("/swagger"); + return Task.CompletedTask; + }); +}); app.Run(); diff --git a/modules/openiddict/app/OpenIddict.Demo.Client.Mvc/OpenIddict.Demo.Client.Mvc.csproj b/modules/openiddict/app/OpenIddict.Demo.Client.Mvc/OpenIddict.Demo.Client.Mvc.csproj index de9010eadf..f4a7e24d6e 100644 --- a/modules/openiddict/app/OpenIddict.Demo.Client.Mvc/OpenIddict.Demo.Client.Mvc.csproj +++ b/modules/openiddict/app/OpenIddict.Demo.Client.Mvc/OpenIddict.Demo.Client.Mvc.csproj @@ -8,7 +8,8 @@ - + + diff --git a/modules/openiddict/app/OpenIddict.Demo.Client.Mvc/OpenIddictMvcModule.cs b/modules/openiddict/app/OpenIddict.Demo.Client.Mvc/OpenIddictMvcModule.cs new file mode 100644 index 0000000000..53693de823 --- /dev/null +++ b/modules/openiddict/app/OpenIddict.Demo.Client.Mvc/OpenIddictMvcModule.cs @@ -0,0 +1,14 @@ +using Volo.Abp.AspNetCore.Authentication.OpenIdConnect; +using Volo.Abp.AspNetCore.Mvc; +using Volo.Abp.Modularity; + +namespace OpenIddict.Demo.Client.Mvc; + +[DependsOn( + typeof(AbpAspNetCoreMvcModule), + typeof(AbpAspNetCoreAuthenticationOpenIdConnectModule) +)] +public class OpenIddictMvcModule : AbpModule +{ + +} diff --git a/modules/openiddict/app/OpenIddict.Demo.Client.Mvc/Pages/Index.cshtml b/modules/openiddict/app/OpenIddict.Demo.Client.Mvc/Pages/Index.cshtml index 51646646d1..dc32cca59c 100644 --- a/modules/openiddict/app/OpenIddict.Demo.Client.Mvc/Pages/Index.cshtml +++ b/modules/openiddict/app/OpenIddict.Demo.Client.Mvc/Pages/Index.cshtml @@ -1,6 +1,7 @@ @page -@using Microsoft.AspNetCore.Authentication +@using System.Net.Http @using System.Net.Http.Headers +@using Microsoft.AspNetCore.Authentication @using System.Text.Json @model IndexModel @{ diff --git a/modules/openiddict/app/OpenIddict.Demo.Client.Mvc/Pages/Shared/_Layout.cshtml b/modules/openiddict/app/OpenIddict.Demo.Client.Mvc/Pages/Shared/_Layout.cshtml index d7f0e61a5b..f3e8499ba7 100644 --- a/modules/openiddict/app/OpenIddict.Demo.Client.Mvc/Pages/Shared/_Layout.cshtml +++ b/modules/openiddict/app/OpenIddict.Demo.Client.Mvc/Pages/Shared/_Layout.cshtml @@ -6,7 +6,6 @@ @ViewData["Title"] - OpenIddict.Demo.Client -
@@ -48,4 +47,4 @@ @await RenderSectionAsync("Scripts", required: false) - \ No newline at end of file + diff --git a/modules/openiddict/app/OpenIddict.Demo.Client.Mvc/Program.cs b/modules/openiddict/app/OpenIddict.Demo.Client.Mvc/Program.cs index d08b46893c..108919797a 100644 --- a/modules/openiddict/app/OpenIddict.Demo.Client.Mvc/Program.cs +++ b/modules/openiddict/app/OpenIddict.Demo.Client.Mvc/Program.cs @@ -1,4 +1,5 @@ using IdentityModel; +using OpenIddict.Demo.Client.Mvc; var builder = WebApplication.CreateBuilder(args); builder.Logging.ClearProviders(); @@ -16,7 +17,7 @@ builder.Services.AddAuthentication(options => { options.ExpireTimeSpan = TimeSpan.FromDays(365); }) - .AddOpenIdConnect("oidc", options => + .AddAbpOpenIdConnect("oidc", options => { options.Authority = "https://localhost:44301/"; options.RequireHttpsMetadata = true; @@ -35,7 +36,9 @@ builder.Services.AddAuthentication(options => options.Scope.Add("AbpAPI"); }); +await builder.AddApplicationAsync(); var app = builder.Build(); +await app.InitializeApplicationAsync(); // Configure the HTTP request pipeline. if (!app.Environment.IsDevelopment()) @@ -47,12 +50,8 @@ if (!app.Environment.IsDevelopment()) app.UseHttpsRedirection(); app.UseStaticFiles(); - app.UseRouting(); - app.UseAuthentication(); app.UseAuthorization(); - -app.MapRazorPages(); - +app.UseConfiguredEndpoints(); app.Run(); diff --git a/modules/openiddict/app/OpenIddict.Demo.Server/Migrations/20240108043816_Initial.Designer.cs b/modules/openiddict/app/OpenIddict.Demo.Server/Migrations/20240427010513_Initial.Designer.cs similarity index 96% rename from modules/openiddict/app/OpenIddict.Demo.Server/Migrations/20240108043816_Initial.Designer.cs rename to modules/openiddict/app/OpenIddict.Demo.Server/Migrations/20240427010513_Initial.Designer.cs index bb9793af70..dfea4e41ba 100644 --- a/modules/openiddict/app/OpenIddict.Demo.Server/Migrations/20240108043816_Initial.Designer.cs +++ b/modules/openiddict/app/OpenIddict.Demo.Server/Migrations/20240427010513_Initial.Designer.cs @@ -13,7 +13,7 @@ using Volo.Abp.EntityFrameworkCore; namespace OpenIddict.Demo.Server.Migrations { [DbContext(typeof(ServerDbContext))] - [Migration("20240108043816_Initial")] + [Migration("20240427010513_Initial")] partial class Initial { /// @@ -384,6 +384,58 @@ namespace OpenIddict.Demo.Server.Migrations b.ToTable("AbpSecurityLogs", (string)null); }); + modelBuilder.Entity("Volo.Abp.Identity.IdentitySession", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uniqueidentifier"); + + b.Property("ClientId") + .HasMaxLength(64) + .HasColumnType("nvarchar(64)"); + + b.Property("Device") + .IsRequired() + .HasMaxLength(64) + .HasColumnType("nvarchar(64)"); + + b.Property("DeviceInfo") + .HasMaxLength(64) + .HasColumnType("nvarchar(64)"); + + b.Property("IpAddresses") + .HasMaxLength(256) + .HasColumnType("nvarchar(256)"); + + b.Property("LastAccessed") + .HasColumnType("datetime2"); + + b.Property("SessionId") + .IsRequired() + .HasMaxLength(128) + .HasColumnType("nvarchar(128)"); + + b.Property("SignedIn") + .HasColumnType("datetime2"); + + b.Property("TenantId") + .HasColumnType("uniqueidentifier") + .HasColumnName("TenantId"); + + b.Property("UserId") + .HasColumnType("uniqueidentifier"); + + b.HasKey("Id"); + + b.HasIndex("Device"); + + b.HasIndex("SessionId"); + + b.HasIndex("TenantId", "UserId"); + + b.ToTable("AbpSessions", (string)null); + }); + modelBuilder.Entity("Volo.Abp.Identity.IdentityUser", b => { b.Property("Id") @@ -1424,10 +1476,17 @@ namespace OpenIddict.Demo.Server.Migrations .HasMaxLength(64) .HasColumnType("nvarchar(64)"); + b.Property("NormalizedName") + .IsRequired() + .HasMaxLength(64) + .HasColumnType("nvarchar(64)"); + b.HasKey("Id"); b.HasIndex("Name"); + b.HasIndex("NormalizedName"); + b.ToTable("AbpTenants", (string)null); }); diff --git a/modules/openiddict/app/OpenIddict.Demo.Server/Migrations/20240108043816_Initial.cs b/modules/openiddict/app/OpenIddict.Demo.Server/Migrations/20240427010513_Initial.cs similarity index 95% rename from modules/openiddict/app/OpenIddict.Demo.Server/Migrations/20240108043816_Initial.cs rename to modules/openiddict/app/OpenIddict.Demo.Server/Migrations/20240427010513_Initial.cs index 9e98844b1c..dabfc5ffa3 100644 --- a/modules/openiddict/app/OpenIddict.Demo.Server/Migrations/20240108043816_Initial.cs +++ b/modules/openiddict/app/OpenIddict.Demo.Server/Migrations/20240427010513_Initial.cs @@ -221,6 +221,26 @@ namespace OpenIddict.Demo.Server.Migrations table.PrimaryKey("PK_AbpSecurityLogs", x => x.Id); }); + migrationBuilder.CreateTable( + name: "AbpSessions", + columns: table => new + { + Id = table.Column(type: "uniqueidentifier", nullable: false), + SessionId = table.Column(type: "nvarchar(128)", maxLength: 128, nullable: false), + Device = table.Column(type: "nvarchar(64)", maxLength: 64, nullable: false), + DeviceInfo = table.Column(type: "nvarchar(64)", maxLength: 64, nullable: true), + TenantId = table.Column(type: "uniqueidentifier", nullable: true), + UserId = table.Column(type: "uniqueidentifier", nullable: false), + ClientId = table.Column(type: "nvarchar(64)", maxLength: 64, nullable: true), + IpAddresses = table.Column(type: "nvarchar(256)", maxLength: 256, nullable: true), + SignedIn = table.Column(type: "datetime2", nullable: false), + LastAccessed = table.Column(type: "datetime2", nullable: true) + }, + constraints: table => + { + table.PrimaryKey("PK_AbpSessions", x => x.Id); + }); + migrationBuilder.CreateTable( name: "AbpSettingDefinitions", columns: table => new @@ -262,6 +282,7 @@ namespace OpenIddict.Demo.Server.Migrations { Id = table.Column(type: "uniqueidentifier", nullable: false), Name = table.Column(type: "nvarchar(64)", maxLength: 64, nullable: false), + NormalizedName = table.Column(type: "nvarchar(64)", maxLength: 64, nullable: false), EntityVersion = table.Column(type: "int", nullable: false), ExtraProperties = table.Column(type: "nvarchar(max)", nullable: false), ConcurrencyStamp = table.Column(type: "nvarchar(40)", maxLength: 40, nullable: false), @@ -753,6 +774,21 @@ namespace OpenIddict.Demo.Server.Migrations table: "AbpSecurityLogs", columns: new[] { "TenantId", "UserId" }); + migrationBuilder.CreateIndex( + name: "IX_AbpSessions_Device", + table: "AbpSessions", + column: "Device"); + + migrationBuilder.CreateIndex( + name: "IX_AbpSessions_SessionId", + table: "AbpSessions", + column: "SessionId"); + + migrationBuilder.CreateIndex( + name: "IX_AbpSessions_TenantId_UserId", + table: "AbpSessions", + columns: new[] { "TenantId", "UserId" }); + migrationBuilder.CreateIndex( name: "IX_AbpSettingDefinitions_Name", table: "AbpSettingDefinitions", @@ -771,6 +807,11 @@ namespace OpenIddict.Demo.Server.Migrations table: "AbpTenants", column: "Name"); + migrationBuilder.CreateIndex( + name: "IX_AbpTenants_NormalizedName", + table: "AbpTenants", + column: "NormalizedName"); + migrationBuilder.CreateIndex( name: "IX_AbpUserClaims_UserId", table: "AbpUserClaims", @@ -878,6 +919,9 @@ namespace OpenIddict.Demo.Server.Migrations migrationBuilder.DropTable( name: "AbpSecurityLogs"); + migrationBuilder.DropTable( + name: "AbpSessions"); + migrationBuilder.DropTable( name: "AbpSettingDefinitions"); diff --git a/modules/openiddict/app/OpenIddict.Demo.Server/Migrations/ServerDbContextModelSnapshot.cs b/modules/openiddict/app/OpenIddict.Demo.Server/Migrations/ServerDbContextModelSnapshot.cs index 0345f4da8d..a2be3405db 100644 --- a/modules/openiddict/app/OpenIddict.Demo.Server/Migrations/ServerDbContextModelSnapshot.cs +++ b/modules/openiddict/app/OpenIddict.Demo.Server/Migrations/ServerDbContextModelSnapshot.cs @@ -381,6 +381,58 @@ namespace OpenIddict.Demo.Server.Migrations b.ToTable("AbpSecurityLogs", (string)null); }); + modelBuilder.Entity("Volo.Abp.Identity.IdentitySession", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uniqueidentifier"); + + b.Property("ClientId") + .HasMaxLength(64) + .HasColumnType("nvarchar(64)"); + + b.Property("Device") + .IsRequired() + .HasMaxLength(64) + .HasColumnType("nvarchar(64)"); + + b.Property("DeviceInfo") + .HasMaxLength(64) + .HasColumnType("nvarchar(64)"); + + b.Property("IpAddresses") + .HasMaxLength(256) + .HasColumnType("nvarchar(256)"); + + b.Property("LastAccessed") + .HasColumnType("datetime2"); + + b.Property("SessionId") + .IsRequired() + .HasMaxLength(128) + .HasColumnType("nvarchar(128)"); + + b.Property("SignedIn") + .HasColumnType("datetime2"); + + b.Property("TenantId") + .HasColumnType("uniqueidentifier") + .HasColumnName("TenantId"); + + b.Property("UserId") + .HasColumnType("uniqueidentifier"); + + b.HasKey("Id"); + + b.HasIndex("Device"); + + b.HasIndex("SessionId"); + + b.HasIndex("TenantId", "UserId"); + + b.ToTable("AbpSessions", (string)null); + }); + modelBuilder.Entity("Volo.Abp.Identity.IdentityUser", b => { b.Property("Id") @@ -1421,10 +1473,17 @@ namespace OpenIddict.Demo.Server.Migrations .HasMaxLength(64) .HasColumnType("nvarchar(64)"); + b.Property("NormalizedName") + .IsRequired() + .HasMaxLength(64) + .HasColumnType("nvarchar(64)"); + b.HasKey("Id"); b.HasIndex("Name"); + b.HasIndex("NormalizedName"); + b.ToTable("AbpTenants", (string)null); }); diff --git a/modules/openiddict/app/OpenIddict.Demo.Server/OpenIddict.Demo.Server.csproj b/modules/openiddict/app/OpenIddict.Demo.Server/OpenIddict.Demo.Server.csproj index eef34f037a..60bbb53ee8 100644 --- a/modules/openiddict/app/OpenIddict.Demo.Server/OpenIddict.Demo.Server.csproj +++ b/modules/openiddict/app/OpenIddict.Demo.Server/OpenIddict.Demo.Server.csproj @@ -4,7 +4,6 @@ net8.0 enable enable - OpenIddict.Demo.Server1 @@ -65,7 +64,6 @@ - runtime; build; native; contentfiles; analyzers compile; contentFiles; build; buildMultitargeting; buildTransitive; analyzers; native @@ -81,10 +79,4 @@ - - <_ContentIncludedByDefault Remove="Pages\Shared\_Layout.cshtml" /> - <_ContentIncludedByDefault Remove="Pages\Shared\_ValidationScriptsPartial.cshtml" /> - <_ContentIncludedByDefault Remove="wwwroot\js\site.js" /> - - diff --git a/modules/openiddict/app/OpenIddict.Demo.Server/Program.cs b/modules/openiddict/app/OpenIddict.Demo.Server/Program.cs index bee5baa950..dd16dfb75b 100644 --- a/modules/openiddict/app/OpenIddict.Demo.Server/Program.cs +++ b/modules/openiddict/app/OpenIddict.Demo.Server/Program.cs @@ -28,7 +28,6 @@ builder.Services.Configure(options => }); await builder.AddApplicationAsync(); - var app = builder.Build(); await app.InitializeApplicationAsync(); @@ -51,15 +50,9 @@ app.UseHttpsRedirection(); app.UseStaticFiles(); app.UseRouting(); app.UseCors(); - -// Use Microsoft.AspNetCore.Authentication.JwtBearer instead of OpenIddict.Validation.AspNetCore -//app.UseJwtTokenMiddleware(); - app.UseAuthentication(); app.UseAbpOpenIddictValidation(); app.UseMultiTenancy(); app.UseAuthorization(); - app.UseConfiguredEndpoints(); - app.Run();