diff --git a/modules/openiddict/app/OpenIddict.Demo.API/Program.cs b/modules/openiddict/app/OpenIddict.Demo.API/Program.cs index 4d56464b50..0938f87e6c 100644 --- a/modules/openiddict/app/OpenIddict.Demo.API/Program.cs +++ b/modules/openiddict/app/OpenIddict.Demo.API/Program.cs @@ -1,5 +1,8 @@ using Microsoft.AspNetCore.Authentication.JwtBearer; using OpenIddict.Demo.API; +using Microsoft.OpenApi.Models; +using Swashbuckle.AspNetCore.SwaggerGen; +using Swashbuckle.AspNetCore.SwaggerUI; var builder = WebApplication.CreateBuilder(args); builder.Logging.ClearProviders(); @@ -20,7 +23,40 @@ builder.Services.AddCors(options => builder.Services.AddControllers(); // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle builder.Services.AddEndpointsApiExplorer(); -builder.Services.AddSwaggerGen(); +builder.Services.AddSwaggerGen(options => +{ + options.AddSecurityDefinition("oauth2", new OpenApiSecurityScheme + { + Type = SecuritySchemeType.OAuth2, + Flows = new OpenApiOAuthFlows + { + AuthorizationCode = new OpenApiOAuthFlow + { + AuthorizationUrl = new Uri("https://localhost:44301/connect/authorize"), + TokenUrl = new Uri("https://localhost:44301/connect/token"), + Scopes = new Dictionary + { + { "AbpAPI", "AbpAPI"} + } + } + } + }); + + options.AddSecurityRequirement(new OpenApiSecurityRequirement + { + { + new OpenApiSecurityScheme + { + Reference = new OpenApiReference + { + Type = ReferenceType.SecurityScheme, + Id = "oauth2" + } + }, + Array.Empty() + } + }); +}); builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme) .AddAbpJwtBearer(options => @@ -37,7 +73,12 @@ await app.InitializeApplicationAsync(); if (app.Environment.IsDevelopment()) { app.UseSwagger(); - app.UseSwaggerUI(); + app.UseSwaggerUI(options => + { + options.SwaggerEndpoint("/swagger/v1/swagger.json", "OpenIddict.Demo.API"); + options.OAuthClientId("Swagger"); + options.OAuthScopes("AbpAPI"); + }); } app.UseHttpsRedirection(); diff --git a/modules/openiddict/app/OpenIddict.Demo.Server/EntityFrameworkCore/ServerDataSeedContributor.cs b/modules/openiddict/app/OpenIddict.Demo.Server/EntityFrameworkCore/ServerDataSeedContributor.cs index 9d4aee74e7..148685c1bd 100644 --- a/modules/openiddict/app/OpenIddict.Demo.Server/EntityFrameworkCore/ServerDataSeedContributor.cs +++ b/modules/openiddict/app/OpenIddict.Demo.Server/EntityFrameworkCore/ServerDataSeedContributor.cs @@ -156,5 +156,37 @@ public class ServerDataSeedContributor : IDataSeedContributor, ITransientDepende } }); } + + if (await _applicationManager.FindByClientIdAsync("Swagger") == null) + { + await _applicationManager.CreateAsync(new OpenIddictApplicationDescriptor + { + ApplicationType = OpenIddictConstants.ApplicationTypes.Web, + ClientId = "Swagger", + ClientType = OpenIddictConstants.ClientTypes.Public, + ConsentType = OpenIddictConstants.ConsentTypes.Explicit, + DisplayName = "Abp Swagger Application", + RedirectUris = + { + new Uri("https://localhost:44303/swagger/oauth2-redirect.html") + }, + Permissions = + { + OpenIddictConstants.Permissions.Endpoints.Authorization, + OpenIddictConstants.Permissions.Endpoints.Token, + + OpenIddictConstants.Permissions.GrantTypes.AuthorizationCode, + + OpenIddictConstants.Permissions.ResponseTypes.Code, + + OpenIddictConstants.Permissions.Prefixes.Scope + "AbpAPI" + }, + Settings = + { + // Use a shorter access token lifetime for tokens issued to the Postman application. + [OpenIddictConstants.Settings.TokenLifetimes.AccessToken] = TimeSpan.FromMinutes(5).ToString("c", CultureInfo.InvariantCulture) + } + }); + } } } diff --git a/modules/openiddict/app/OpenIddict.Demo.Server/Program.cs b/modules/openiddict/app/OpenIddict.Demo.Server/Program.cs index 9b3126f594..83c641e569 100644 --- a/modules/openiddict/app/OpenIddict.Demo.Server/Program.cs +++ b/modules/openiddict/app/OpenIddict.Demo.Server/Program.cs @@ -8,7 +8,7 @@ builder.Services.AddCors(options => options.AddDefaultPolicy( builder => { - builder.WithOrigins("http://localhost:4200", "https://localhost:44304") + builder.WithOrigins("http://localhost:4200", "https://localhost:44303", "https://localhost:44304") .SetIsOriginAllowedToAllowWildcardSubdomains() .AllowAnyHeader() .AllowAnyMethod()