下面的注册 JWT-bearer authentication 的代码
services.AddAuthentication()
.AddJwtBearer(
op =>
{
//...
op.Events ??= new JwtBearerEvents();
//..
});
在日志中发现运行时有时竟然会出现 null 引用异常
System.NullReferenceException: Object reference not set to an instance of an object.
at Cnblogs.Web.Startup.<ConfigureServices>b__5_2(JwtBearerOptions op) in /src/Cnblogs.Web/Startup.cs:line
让人有点想不通,请问如何避免这个 NullReferenceException?
弄错代码了,是 op.Events ??= new JwtBearerEvents();
被删除,后面的 op.Events.OnTokenValidated
引起的 NullReferenceException
github 上的相关 issue:JwtBearerEvents might be null, despite being marked as not null
– dudu 2年前