在NSwag SwaggerUi中实现JwtBearer身份验证 [英] Implement JwtBearer Authentication in NSwag SwaggerUi

查看:542
本文介绍了在NSwag SwaggerUi中实现JwtBearer身份验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的asp.net core 2.0解决方案中,我想添加Azure AD身份验证.借助VS 2017中的Azure AD模板,您可以获取JWTBearer身份验证实现或OpenIdConnect实现.开放ID还享有比OAuth更安全的声誉.

In my asp.net core 2.0 solution I want to add Azure AD authentication. With the Azure AD templates inside of VS 2017 you either get JWTBearer authentication-implementation or OpenIdConnect implementation. Open Id also has the reputation of being more secure than OAuth.

如何将Open ID/JWT与NSwag提供的Swagger Ui一起使用?

How can I use Open ID / JWT with the Swagger Ui, provided by NSwag?

我当前的解决方法是同时允许OAuth和Open Id,但是我需要自己实现,而且几乎没有关于新2.0 API的文档.具有两个身份验证工作流的安全性也较差.尤其是当一个安全性不如另一个安全性时.

My current workaround would be to allow both OAuth and Open Id, but I need to implement that myself and there is almost no documentation on the new 2.0 APIs. Its also less secure having two authentication workflows. Especially when one is less secure than the other.

推荐答案

示例通过 reepape :

app.UseSwaggerUi(typeof(Startup).GetTypeInfo().Assembly, settings =>
{
    settings.GeneratorSettings.OperationProcessors.Add(new OperationSecurityScopeProcessor("JWT Token"));

    settings.GeneratorSettings.DocumentProcessors.Add(new SecurityDefinitionAppender("JWT Token",
        new SwaggerSecurityScheme
        {
            Type = SwaggerSecuritySchemeType.ApiKey,
            Name = "Authorization",
            Description = "Copy 'Bearer ' + valid JWT token into field",
            In = SwaggerSecurityApiKeyLocation.Header
        }));
});

它也可以与UseSwaggerUi3一起使用.

这篇关于在NSwag SwaggerUi中实现JwtBearer身份验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆