Skip to content

Instantly share code, notes, and snippets.

@uzbekdev1
Forked from wallymathieu/Startup.cs
Created October 11, 2018 08:15
Show Gist options
  • Save uzbekdev1/1586d64e706e067bc03d954d0a368689 to your computer and use it in GitHub Desktop.
Save uzbekdev1/1586d64e706e067bc03d954d0a368689 to your computer and use it in GitHub Desktop.
HowTo register auth for swashbuckle with identity server on asp.net core
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