How to show WebApi OAuth token endpoint in Swagger

前端 未结 2 1528
日久生厌
日久生厌 2020-12-14 18:30

I\'ve created a new Web Api project, added Asp.Net Identity and configured OAuth like so:

OAuthOptions = new OAuthAuthorizationServerOptions
{
    TokenEndpo         


        
2条回答
  •  囚心锁ツ
    2020-12-14 18:56

    If anyone wondered how to add a response body to this operation, here's updated Ruaidhri's code:

    class AuthTokenOperation : IDocumentFilter
    {
        public void Apply(SwaggerDocument swaggerDoc, SchemaRegistry schemaRegistry, IApiExplorer apiExplorer)
        {
            swaggerDoc.paths.Add("/token", new PathItem
            {
                post = new Operation
                {
                    tags = new List { "Auth" },
                    consumes = new List
                        {
                            "application/x-www-form-urlencoded"
                        },
                    parameters = new List {
                        new Parameter
                        {
                            type = "string",
                            name = "grant_type",
                            required = true,
                            @in = "formData"
                        },
                        new Parameter
                        {
                            type = "string",
                            name = "username",
                            required = false,
                            @in = "formData"
                        },
                        new Parameter
                        {
                            type = "string",
                            name = "password",
                            required = false,
                            @in = "formData"
                        }
                    },
                    responses = new Dictionary()
                    {
                        {
                            "200",
                            new Response {schema = schemaRegistry.GetOrRegister(typeof(OAuthTokenResponse))}
                        }
                    }
                }
            });
        }
    }
    
    class OAuthTokenResponse
    {
        [JsonProperty("access_token")]
        public string AccessToken { get; set; }
    
        [JsonProperty("token_type")]
        public string TokenType { get; set; }
    
        [JsonProperty("expires_in")]
        public long ExpiresIn { get; set; }
    
        [JsonProperty("userName")]
        public string Username { get; set; }
    
        [JsonProperty(".issued")]
        public DateTime Issued { get; set; }
    
        [JsonProperty(".expires")]
        public DateTime Expires { get; set; }
    }
    

提交回复
热议问题