AspNetCore Authentication 是如何将 Scheme 和 Options 关联的?
AspNetCore Authentication 是如何将 Scheme 和 Options 关联的?
当我们调用 AddAuthentication() 时,将返回 AuthenticationBuilder,之后我们通过调用它的 AddScheme() 进行注册,
在它的内部会调用一个方法 AddSchemeHelper,而这个方法将 Scheme 配置到了 AuthenticationOptions 的私有字段 IList<AuthenticationSchemeBuilder> _schemes 当中;
之后在 AuthenticationHandler 的 InitializeAsync 方法中通过 OptionsMonitor.Get( scheme ) 取得对应的选项实例,并赋值给属性Options
由于每个认证处理类都是继承自AuthenticationHandler,因此将始终获得与Scheme相关联的Options实例。
1 | // AuthenticatioinHandler.cs |
认证核心类:
1 | // class |