2

Using Swashbuckle.AspNetCore, I got a Swagger interface for my .NET core REST API, with an "Authorize" button that takes me to my Single Sign-On server:

.NET core screenshot

I need to do the same in an ASP.NET application based on .NET Framework 4.8, using the Swashbuckle package, but I don't get the Authorize button:

.NET framework screenshot

This is my initialization code:

using System.Web.Http;
using WebActivatorEx;
using Swashbuckle.Application;

[assembly: PreApplicationStartMethod(typeof(SwaggerConfig), "Register")]

namespace Test {

    public class SwaggerConfig {
        public static void Register() {
            var thisAssembly = typeof(SwaggerConfig).Assembly;

            GlobalConfiguration.Configuration
                .EnableSwagger(c => {
                    c.SingleApiVersion("v1", "Rest Test");
                    c.OAuth2("oauth2")
                        .Description("OAuth2 Implicit Grant")
                        .Flow("implicit")
                        .AuthorizationUrl("https://keycloak.some.test/auth/realms/test/protocol/openid-connect/auth")
                        .TokenUrl("https://keycloak.some.test/auth/realms/test/protocol/openid-connect");
                    c.DescribeAllEnumsAsStrings();
                })
                .EnableSwaggerUi(c => {
                    c.EnableOAuth2Support(
                        clientId: "test-client",
                        clientSecret: null,
                        realm: "test",
                        appName: "Swagger UI"
                    );
                });
        }
    }
}

Is there a way to get the authorize button with Swashbuckle on ASP.NET, using the .NET framework and not .NET core? Am I missing some initialization?

I tried searching for this issue, but all the answers and documentation I've found were related to .NET core, and for me it's essential to use .NET framework 4.8.

6
  • Does your test/test endpoint have a AuthorizeAttribute attached to it? Or the Net Framework equivalent of that that i don't know of? Maybe it help showing how the endpoint/controller signatures (especially its attributes) look. Commented Nov 22, 2023 at 15:14
  • Curios how does Swashbuckle.AspNetCore get together with Net. Framework 4.8? OK seen it. The very old versions aren't for NetStandard 2 and "might" work. Commented Nov 22, 2023 at 15:18
  • @Ralf in .NET framework version I'm using Swashbuckle, not Swashbuckle.AspNetCore. I've updated the question. Commented Nov 22, 2023 at 15:23
  • Also, I haven't defined any AuthorizeAttribute, I'll see if that changes anything. Commented Nov 22, 2023 at 15:23
  • Try getting it to work without [Authorize] which should allow any user. OAUTH2 should work with Net 4.8. Some people it works, and other have had issues. Get the status code returned in the response which will help find the answer. Commented Nov 22, 2023 at 15:27

1 Answer 1

1

This is how to enable adding the Authorization header in Swagger UI version 5.6.0 in .NET Framework 4.8.1 MVC 5 :

In SwaggerConfig.cs (or App_Start/SwaggerConfig.cs), configure security as follows:

public class SwaggerConfig
{
    public static void Register(HttpConfiguration config)
    {
        // Enable Swagger UI
        GlobalConfiguration.Configuration
            .EnableSwagger(c =>
            {
                c.SingleApiVersion("v1", "Your API Title");

                // This adds a global "Bearer" authentication type to Swagger
                c.OperationFilter<AddAuthorizationHeaderParameterOperationFilter>();
            })
            .EnableSwaggerUi(c =>
            {
                // Optionally enable the authorization button in Swagger UI
                c.EnableApiKeySupport("Authorization", "header");
            });
    }
}

And this is the AddAuthorizationHeaderParameterOperationFilter

public class AddAuthorizationHeaderParameterOperationFilter : IOperationFilter
{
    public void Apply(Operation operation, SchemaRegistry schemaRegistry, ApiDescription apiDescription)
    {
        if (operation.parameters == null)
            operation.parameters = new List<Parameter>();

        operation.parameters.Add(new Parameter
        {
            name = "Authorization",
            @in = "header",
            description = "Bearer token for authentication",
            required = false,
            type = "string"
        });
    }
}

Result:

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.