Browse Source

Added oauth authencation to swagger page.

pull/20979/head
maliming 2 years ago
parent
commit
e1e6615888
No known key found for this signature in database GPG Key ID: A646B9CB645ECEA4
  1. 45
      modules/openiddict/app/OpenIddict.Demo.API/Program.cs
  2. 32
      modules/openiddict/app/OpenIddict.Demo.Server/EntityFrameworkCore/ServerDataSeedContributor.cs
  3. 2
      modules/openiddict/app/OpenIddict.Demo.Server/Program.cs

45
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<string, string>
{
{ "AbpAPI", "AbpAPI"}
}
}
}
});
options.AddSecurityRequirement(new OpenApiSecurityRequirement
{
{
new OpenApiSecurityScheme
{
Reference = new OpenApiReference
{
Type = ReferenceType.SecurityScheme,
Id = "oauth2"
}
},
Array.Empty<string>()
}
});
});
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();

32
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)
}
});
}
}
}

2
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()

Loading…
Cancel
Save