Last active
March 28, 2019 13:21
-
-
Save wallymathieu/e149735645232dfc0dd92f8e6fc71f9b to your computer and use it in GitHub Desktop.
HowTo register auth for swashbuckle with identity server on asp.net core
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Namespace ProjectWithSwagger | |
{ | |
public class Startup | |
{ | |
// This method gets called by the runtime. Use this method to add services to the container | |
public void ConfigureServices(IServiceCollection services) | |
{ | |
//.... | |
services.ConfigureSwaggerGen(swaggerGen => | |
{ | |
swaggerGen.AddSecurityDefinition("Swagger", new OAuth2Scheme | |
{ | |
AuthorizationUrl = UriCreate(Configuration.IdentityServerSettings.Authority, "/connect/authorize").ToString(), | |
Flow = "implicit", | |
TokenUrl = UriCreate(Configuration.IdentityServerSettings.Authority, "/connect/token").ToString(), | |
Scopes = { { "api.name", "The Scope needed to access API" } } | |
}); | |
} | |
} | |
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline | |
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory, | |
IAppointmentPlusService aps, IMigrationsHandler mh) | |
{ | |
app.UseSwagger(c => | |
{ | |
c.RouteTemplate = "swagger/{documentName}/swagger.json"; | |
}); | |
// Enable middleware to serve swagger-ui assets (HTML, JS, CSS etc.) | |
app.UseSwaggerUi(c => | |
{ | |
c.ConfigureOAuth2("api.name.swagger", SwaggerSecret, "swagger-ui-realm", "API Swagger UI"); | |
}); | |
app.UseIdentityServerAuthentication(new IdentityServerAuthenticationOptions | |
{ | |
Authority = Configuration.IdentityServerSettings.Authority, | |
RequireHttpsMetadata = !_env.IsDevelopment(), | |
ApiName = "api.name", | |
SupportedTokens = SupportedTokens.Both | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I think this example might be a bit dated. It's probably Swashbuckle.AspNetCore and
IdentityServer4.AccessTokenValidation. I think that it might have become a bit easier to configure with later versions of Swashbuckle.