C# Owin JWT 401未经授权

C# Owin JWT 401未经授权,c#,authentication,jwt,owin,identityserver4,C#,Authentication,Jwt,Owin,Identityserver4,我正在尝试使用IdentityServer4和JWT进行身份验证。我从我的客户那里得到一个令牌,并试图向我的一个控制器发送一个简单的请求 我有这样的要求 获取api/用户 授权:持票人{{我的代币} 在我的创业班,我已经注册了 var authorizationPolicy = new AuthorizationPolicyBuilder() .RequireAuthenticatedUser() .Build(); services.AddMvc(conf

我正在尝试使用IdentityServer4和JWT进行身份验证。我从我的客户那里得到一个令牌,并试图向我的一个控制器发送一个简单的请求

我有这样的要求

获取api/用户

授权:持票人{{我的代币}

在我的创业班,我已经注册了

var authorizationPolicy = new AuthorizationPolicyBuilder()
         .RequireAuthenticatedUser()
         .Build();

services.AddMvc(config => {
    config.Filters.Add(new AuthorizeFilter(authorizationPolicy)});

services.AddAuthentication(options =>
{
    options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
    options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
}).AddIdentityServerAuthentication(o =>
{
    o.Authority = "https://localhost:44333";
    o.RequireHttpsMetadata = false;
    o.ApiName = "MyApi";
    o.JwtBearerEvents = new JwtBearerEvents
    {
        OnAuthenticationFailed = async context => {
            Console.WriteLine("Debugger");
        },
        OnMessageReceived = async context => {
            Console.WriteLine("Debugger");
        },

        OnTokenValidated = async tokenValidationContext =>
        {
            Console.WriteLine("Debugger");
        }
});
我已经在Console.WriteLine(“Debugger”)语句中的每一条上都设置了断点,但没有一条断点被命中。但我还是被一个未经授权的人送回来了

标题是否适合我的授权?我想在请求失败时检查它,但即使打开了所有异常,我也无法达到断点,有人有什么建议吗

编辑 我的客户确认:

    public static IEnumerable<ApiResource> GetApiResources()
    {
        return new List<ApiResource>
        {
            new ApiResource("MyApi", "My Api"),
            new ApiResource
            {
                Name = "customAPI",
                DisplayName = "Custom API",
                Description = "Custom API Access",
                UserClaims = new List<string> {"role"},
                ApiSecrets = new List<Secret> {new Secret("secretPassword".Sha256())},
                Scopes = new List<Scope>
                {
                    new Scope("customAPI.read"),
                    new Scope("customAPI.write")
                }
            }
        };
    }
public静态IEnumerable GetApiResources()
{
返回新列表
{
新的Api资源(“我的Api”、“我的Api”),
新资源
{
Name=“customAPI”,
DisplayName=“自定义API”,
Description=“自定义API访问”,
UserClaims=新列表{“角色”},
ApiSecrets=new List{new Secret(“secretPassword”.Sha256())},
范围=新列表
{
新范围(“customAPI.read”),
新范围(“customAPI.write”)
}
}
};
}
控制器基座:

[Route("api/[controller]")]
public class AsyncCRUDSingleKeyServiceController<TDTO, TKey> : Controller
    where TKey : struct
{
    protected IAsyncCRUDService<TDTO> _service;

    public AsyncCRUDSingleKeyServiceController(IAsyncCRUDService<TDTO> service)
    {
        this._service = service;
    }

    [HttpGet("{id}")]
    public virtual async Task<TDTO> Get(TKey id)
    {
        return await this._service.Get(new object[] { id });
    }

    //...
}
[路由(“api/[控制器]”)]
公共类AsyncCRUDSingleKeyServiceController:控制器
其中TKey:struct
{
受保护的IAsyncCRUDService\u服务;
公共异步CrudSingleKeyServiceController(IAsyncCRUDService服务)
{
这个。_服务=服务;
}
[HttpGet(“{id}”)]
公共虚拟异步任务Get(TKey id)
{
return等待此操作。_service.Get(新对象[]{id});
}
//...
}

在Startup.Configure中,是否包含以下行(在app.UseMvc之前)


将此行中的https更改为http:o.Authority=“”;有几件事。显示客户端配置和控制器代码。听起来您正在点击API,而不是为已登录的用户传递令牌。您是否查看过您的代币,以查看代币中包含哪些声明和详细信息。您可以使用查看详细信息。Https也没有帮助。在返回401ID之前,我甚至无法找到要命中的断点。如果在Configure to use authentication:app.UseAuthentication()中包含该行(在app.UseMvc之前)@感谢我错过了比赛,现在我可以突破了。如果你加一个答案,我会把它标对的
app.UseAuthentication();