2

I have setup Swagger within my ASP.NET project using NSwag which works fine but I am attempting to add support for authentication.

My authentication model is a simple username/password that uses OAuth using ApplicationOAuthProvider

The URL I use to login is as below

/token

With the POST parameters:

grant_type=password&username=${username}&password=${password}

Now my swagger setup [in Global.asax] is

app.UseSwaggerUi(typeof(Global).Assembly, new SwaggerUiSettings
{
    MiddlewareBasePath = "/swagger",
    OAuth2Client = new OAuth2ClientSettings
    {
        ClientId = "my_auth_id",
        //ClientSecret = "bar",
        AppName = "my",
        //Realm = "my_realm",
        //AdditionalQueryStringParameters = { { "foo", "bar" } }
    },
    DocumentProcessors =    {
        new SecurityDefinitionAppender("oauth2", new SwaggerSecurityScheme
        {
            Type = SwaggerSecuritySchemeType.Basic,
            Description = "Description is set htere",
            Flow = SwaggerOAuth2Flow.Password,
            AuthorizationUrl = "https://localhost:28866/token?",
            TokenUrl = "https://localhost:28866/token",
            In = SwaggerSecurityApiKeyLocation.Query
            //Scopes = new Dictionary<string,string>
            //{
            //    //{ "read", "Read access to protected resources" },
            //    { "write", "Write access to protected resources" }
            //}
        })
    },
    OperationProcessors =
    {
        new OperationSecurityScopeProcessor("oauth2")
    }
});

I know its a bit messy but I was literally trying every option I could to make it work.

So this actually gives me the Authorize button and a Username and Password field. But when I click login it refreshes the swagger.json but doesnt actually attempt to log in anywhere?

Chris
  • 26,744
  • 48
  • 193
  • 345
  • I don't understand, what are you trying to set up Basic Authentication or OAuth? – zakaria amine Jul 28 '17 at 08:33
  • We use the OAuthProvider but you login with a username and password. So I need a username and password box to appear, after which the user will login and be given an authorization token for future requests – Chris Jul 28 '17 at 09:21
  • Maybe you need to add them both. Basic Auth and OAuth. This is an example: http://petstore.swagger.io/, it is does not have Basic Auth, but it has two types of Authentication – zakaria amine Jul 28 '17 at 13:31

0 Answers0