扬鞭 - 添加用户名/登录,并在自定义页眉使用 [英] Swagger - Add Username/Login and use in Custom Header
问题描述
我有一个蓝色的手机应用程序的API写在C#。
I have an azure mobile app api written in c#.
我一直在使用的NuGet添加扬鞭。
I have added Swagger using Nuget.
扬鞭似乎很好地工作。但是,开箱即用,它允许用户输入招摇主页右上方的API密钥...这个值则附加到所有的测试与调用?/ API_KEY = XXXX
Swagger seems to work perfectly. But "out of the box" it allows a user to enter an API key in the top right of the swagger homepage... this value is then appended to all test calls with "?/api_key=xxxx"
我需要找出如何让招摇派这把钥匙作为所谓的X-谟-AUTH在通话的身体自定义标题。
I need to work out how to make swagger send this "key" as a custom header called "X-ZUMO-AUTH" in the body of the call.
在理想情况下,我想在这个扩大为好。
Ideally, I would like to expand on this as well.
我有一个权威性API调用,这需要用户名/密码......这将是巨大的,如果我能与调用我的身份验证端点和用户名/密码组合消除对UI的钥匙场使用返回值我所有后续调用API ...再次作为自定义标题X-谟-AUTH..
I have an "auth" api call which takes a username/password... It would be great if I could remove the "key" field on the UI with a username/password combination that calls my "auth" endpoint and uses the returned value I all subsequent calls to the api... Again as a custom header "X-ZUMO-AUTH"..
如果我能做到第1部分,这将是伟大的...
If I can achieve part 1 that would be great...
任何人都可以点我朝着正确的方向吗?
Can anyone point me in the right direction please?
推荐答案
请参阅的自定义认证报头中使用SwaggerUI Swashbuckle为JavaScript的解决方案。如果您正在使用MVC或的WebAPI,在SwaggerConfig.cs为 GlobalConfiguration.Configuration.EnableSwagger的一部分(C => {})
添加 c.OperationFilter< AddAuthorizationHeaderParameterOperationFilter>();
See Customize Authentication Header in SwaggerUI using Swashbuckle for a javascript solution. If you are using MVC or a WebAPI, in the SwaggerConfig.cs as a part of GlobalConfiguration.Configuration.EnableSwagger(c => { })
add c.OperationFilter<AddAuthorizationHeaderParameterOperationFilter>();
.
然后,你可以通过添加类似于类配置
Then you can configure the by adding a class similar to:
public class AddAuthorizationHeaderParameterOperationFilter : IOperationFilter
{
public void Apply(Operation operation, SchemaRegistry schemaRegistry, ApiDescription apiDescription)
{
var filterPipeline = apiDescription.ActionDescriptor.GetFilterPipeline();
var isAuthorized = filterPipeline
.Select(filterInfo => filterInfo.Instance)
.Any(filter => filter is IAuthorizationFilter);
var allowAnonymous = apiDescription.ActionDescriptor.GetCustomAttributes<AllowAnonymousAttribute>().Any();
if (isAuthorized && !allowAnonymous)
{
if (operation.parameters == null)
{
operation.parameters = new List<Parameter>();
}
operation.parameters.Add(new Parameter
{
name = "Authorization",
@in = "header",
description = "access token",
required = true,
type = "string"
});
}
}
}
这篇关于扬鞭 - 添加用户名/登录,并在自定义页眉使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!