asp.net core 认证疑问

睡神在睡觉 2019-09-11 09:15:36
ConfigureServices中:

#region 注册认证服务
services.AddAuthentication(options =>
{
options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
})
.AddJwtBearer(o =>
{
o.TokenValidationParameters = new TokenValidationParameters
{
ValidIssuer = "Issuer",
ValidAudience = "Audience",
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes("JwtAuth:SecurityKey")),
RequireSignedTokens = true,
ValidateAudience = false,
ValidateIssuer = true,
ValidateIssuerSigningKey = true,
RequireExpirationTime = true,
ClockSkew = TimeSpan.FromSeconds(300),
ValidateLifetime = true
};
});
#endregion


Configure中:

app.UseAuthentication().UseMvc();


controller中:

[Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme,Policy = "xxxxx")]
public class XXXController : Controller
{
//do....
}


这样功能都正常,但是controller中把
AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme
去掉就会认证失败,之前的项目中一样的写法没加过这个,为什么现在就需要加这个?仔细看了下控制台输出info,不加一段会显示使用默认认证方式去认证,结果失败,加上之后显示使用JwtBearer认证成功(仅限第一次,之后再有调用就显示默认认证成功了),不知道是哪里的问题,感觉是哪里没设置对?参考了一下这个https://stackoverflow.com/questions/51263883/webapi-authorize-attribute-with-services-addidentity-returns-404-not-found里面说的,但是也不太明白,望大神给指点下。
最后希望大家能给推荐点认证过程介绍的文章;)
...全文
162 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
exception92 2019-09-12
  • 打赏
  • 举报
回复
https://mp.weixin.qq.com/s/9F90xa61n3RS_qfbPxomdw
睡神在睡觉 2019-09-12
  • 打赏
  • 举报
回复
额。。。不是吧。。。。

110,536

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧