Asp.net web api 如何在Swagger中显示WebApi OAuth令牌端点

Asp.net web api 如何在Swagger中显示WebApi OAuth令牌端点,asp.net-web-api,asp.net-identity,swagger-ui,swashbuckle,Asp.net Web Api,Asp.net Identity,Swagger Ui,Swashbuckle,我创建了一个新的Web Api项目,添加了Asp.Net标识,并按如下方式配置了OAuth: OAuthOptions = new OAuthAuthorizationServerOptions { TokenEndpointPath = new PathString("/Token"), Provider = new ApplicationOAuthProvider(PublicClientId), AuthorizeEndpointPath = new PathStr

我创建了一个新的Web Api项目,添加了Asp.Net标识,并按如下方式配置了OAuth:

OAuthOptions = new OAuthAuthorizationServerOptions
{
    TokenEndpointPath = new PathString("/Token"),
    Provider = new ApplicationOAuthProvider(PublicClientId),
    AuthorizeEndpointPath = new PathString("/api/Account/ExternalLogin"),
    AccessTokenExpireTimeSpan = TimeSpan.FromDays(14),
    AllowInsecureHttp = true
};
这一切都很好,我可以调用/Token端点并获取承载令牌

问题是,我认为这在Swagger中是无法发现的,因为它不在控制器上,因此没有为它生成xml文档

有人知道在我的招摇过市文档中显示此登录端点的方法吗

谢谢


另外,我应该说,Swagger文档适用于我所有的控制器,只是我缺少了一个明显的方法—如何登录。

ApiExplorer不会自动为您的端点生成任何信息,因此您需要添加自定义DocumentFilter以手动描述令牌端点

下面是一个例子:

类AuthTokenOperation:IDocumentFilter
{
public void Apply(swagger文档swagger文档、SchemaRegistry SchemaRegistry、IApiExplorer apiExplorer)
{
swaggerDoc.path.Add(“/auth/token”),新的路径项
{
post=新操作
{
tags=新列表{“Auth”},
消费=新列表
{
“应用程序/x-www-form-urlencoded”
},
参数=新列表{
新参数
{
type=“string”,
name=“授权类型”,
必需=真,
@in=“formData”
},
新参数
{
type=“string”,
name=“username”,
必需=错误,
@in=“formData”
},
新参数
{
type=“string”,
name=“password”,
必需=错误,
@in=“formData”
}
}
}
});
}
}
httpConfig.EnableSwagger(c=>
{
c、 DocumentFilter();
});

如果有人想知道如何在此操作中添加响应主体,以下是更新的Ruaidhri代码:

class AuthTokenOperation : IDocumentFilter
{
    public void Apply(SwaggerDocument swaggerDoc, SchemaRegistry schemaRegistry, IApiExplorer apiExplorer)
    {
        swaggerDoc.paths.Add("/token", new PathItem
        {
            post = new Operation
            {
                tags = new List<string> { "Auth" },
                consumes = new List<string>
                    {
                        "application/x-www-form-urlencoded"
                    },
                parameters = new List<Parameter> {
                    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<string, Response>()
                {
                    {
                        "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; }
}
类AuthTokenOperation:IDocumentFilter
{
public void Apply(swagger文档swagger文档、SchemaRegistry SchemaRegistry、IApiExplorer apiExplorer)
{
swaggerDoc.path.Add(“/token”),新的路径项
{
post=新操作
{
tags=新列表{“Auth”},
消费=新列表
{
“应用程序/x-www-form-urlencoded”
},
参数=新列表{
新参数
{
type=“string”,
name=“授权类型”,
必需=真,
@in=“formData”
},
新参数
{
type=“string”,
name=“username”,
必需=错误,
@in=“formData”
},
新参数
{
type=“string”,
name=“password”,
必需=错误,
@in=“formData”
}
},
响应=新字典()
{
{
"200",
新响应{schema=schemaRegistry.GetOrRegister(typeof(OAuthTokenResponse))}
}
}
}
});
}
}
类OAuthTokenResponse
{
[JsonProperty(“访问令牌”)]
公共字符串AccessToken{get;set;}
[JsonProperty(“令牌类型”)]
公共字符串标记类型{get;set;}
[JsonProperty(“到期日”)]
公共长过期在{get;set;}
[JsonProperty(“用户名”)]
公共字符串用户名{get;set;}
[JsonProperty(“.已发行”)]
发布的公共日期时间{get;set;}
[JsonProperty(“.expires”)]
公共日期时间过期{get;set;}
}

我不知道这是否有帮助,但它也将解释如何创建XML如果没有OneHanks,它实际上是我安装Swagger时所遵循的文章之一。你在标记中提到了Swashback,你看过“描述安全/授权方案”吗部分?是通过Swashback-我已经为oauth隐式工作流配置了swagger,这使swagger UI能够执行post/get请求等。不过,我不认为此配置会创建任何与令牌端点相关的api文档。我们需要将此类放在何处才能让帮助页面生成器创建正确的html?@EeKay:最有可能在swagger config.cs中-这是代码的最后一部分上面的示例(httpConfig.EnableSwagger(…)是否有一种方法可以标记密码字段,以便在“尝试”部分中输入时显示*******而不是密码?确定找到它-在参数对象上设置format=“password”。谢谢。@user230910:是的,它使用了虚张声势的皮带扣。虚张声势
class AuthTokenOperation : IDocumentFilter
{
    public void Apply(SwaggerDocument swaggerDoc, SchemaRegistry schemaRegistry, IApiExplorer apiExplorer)
    {
        swaggerDoc.paths.Add("/token", new PathItem
        {
            post = new Operation
            {
                tags = new List<string> { "Auth" },
                consumes = new List<string>
                    {
                        "application/x-www-form-urlencoded"
                    },
                parameters = new List<Parameter> {
                    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<string, Response>()
                {
                    {
                        "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; }
}