1
我从客户端发送令牌到服务器令牌认证在Asp.net核心使用策略库/ 5
“授权:承载eyJhbGciOiJodHR ......”
我想对用户进行授权谁有代币 这里是我的代码。
services.AddAuthorization(auth =>
{
auth.AddPolicy("Have", new AuthorizationPolicyBuilder()
.AddAuthenticationSchemes(JwtBearerDefaults.AuthenticationScheme)
.RequireAuthenticatedUser().Build());
});
services.AddMvc(config =>
{
var policy = new AuthorizationPolicyBuilder()
.RequireAuthenticatedUser()
.Build();
config.Filters.Add(new AuthorizeFilter(policy));
});
app.UseJwtBearerAuthentication(options =>
{
options.AutomaticAuthenticate = false;
});
即使我把AutomaticAuthenticate我得到500错误,如果为假,则401错误
[Authorize(Policy ="Have")]
[HttpGet]
[Route("list")]
public IEnumerable<Products> List()
{
.......
}
那么我不想让不拥有令牌的用户。 如果我删除了授权属性,我将允许用户访问。但我不希望没有提交令牌的用户 –
没有令牌(或过期的)的用户根据定义未经授权。这是'Authorize'的标准行为,为什么你需要一个自定义策略呢?也许澄清你的问题 – Tseng
我纠正了我的问题。 –