Identityserver4 如何访问Swashback.AspNetCore受保护的api以测试自动文档?

Identityserver4 如何访问Swashback.AspNetCore受保护的api以测试自动文档?,identityserver4,asp.net-core-webapi,swashbuckle,Identityserver4,Asp.net Core Webapi,Swashbuckle,我正在开发一个带有IdentityServer4、一个受保护的web api和一个消费者(控制台应用程序到测试)的解决方案 我使用swashback.AspNetCore包自动记录我的web api 在web api中的我的ConfigureServices方法中,我编写了以下代码: services.AddSwaggerGen(c => { c.SwaggerDoc("v1", new Info { Title = "API v1", Versio

我正在开发一个带有IdentityServer4、一个受保护的web api和一个消费者(控制台应用程序到测试)的解决方案

我使用swashback.AspNetCore包自动记录我的web api

在web api中的我的ConfigureServices方法中,我编写了以下代码:

services.AddSwaggerGen(c =>
        {
            c.SwaggerDoc("v1", new Info { Title = "API v1", Version = "v1" });
            c.SwaggerDoc("v2", new Info { Title = "API v2", Version = "v2" });
            c.AddSecurityDefinition("oauth2", new OAuth2Scheme()
            {
                AuthorizationUrl = "http://localhost:5000/connect/authorize",
                Flow = "accessCode",
                TokenUrl = "http://localhost:5000/connect/token",
                Scopes = new Dictionary<string, string> { { "api1", "My API" } }
            });
            c.OperationFilter<AuthorizationHeaderParameterOperationFilter>();            
        });
services.AddSwaggerGen(c=>
{
c、 大摇大摆的文档(“v1”,新信息{Title=“API v1”,Version=“v1”});
c、 虚张声势的文件(“v2”,新信息{Title=“API v2”,Version=“v2”});
c、 AddSecurityDefinition(“oauth2”,新的OAuth2Scheme()
{
授权URL=”http://localhost:5000/connect/authorize",
Flow=“accessCode”,
令牌URL=”http://localhost:5000/connect/token",
Scopes=新字典{{“api1”,“我的API”}
});
c、 操作过滤器();
});
当我在Swagger.UI页面中单击“授权”按钮时,我重定向到IdentityServer,在这里输入用户凭据,它就工作了。 因此,现在我必须输入访问令牌(在Swagger.UI页面中)才能访问我的受保护端点以查看文档:

[ApiExplorerSettings(GroupName = "v2")]
public class ValuesController : Controller
{
    [HttpGet("[controller]")]
    [Authorize]
    public async Task<IEnumerable<string>> GetAsync()
    {
        var accessToken = await HttpContext.Authentication.GetTokenAsync("access_token");
        return new string[] { "value1", "value2" };
    }
}
[apiplorersettings(GroupName=“v2”)]
公共类值控制器:控制器
{
[HttpGet(“[controller]”)
[授权]
公共异步任务GetAsync()
{
var accessToken=await-HttpContext.Authentication.GetTokenAsync(“访问令牌”);
返回新字符串[]{“value1”,“value2”};
}
}
但是访问令牌保存在哪里?如果调用api并放置断点,则访问令牌将保存在accessToken变量中。 我认为应该在授权头中自动对访问令牌进行估值?步骤是什么

提前感谢

试试这个

var accesstoken=await HttpContext.GetTokenAsync(“访问令牌”)


我使用客户端凭据流解决了这个问题。

您使用的是IdentityServer4 1.x还是2.x?@aaronR IdentityServer4在.net core 2.0中,我的api在.net core 1.1中