當我們調用 AddAuthentication()
時,將返回 AuthenticationBuilder
,之後我們通過調用它的 AddScheme()
進行註冊,
在它的內部會調用一個方法 AddSchemeHelper
,而這個方法將 Scheme
配置到了 AuthenticationOptions
的私有字段 IList<AuthenticationSchemeBuilder> _schemes
當中;
之後在 AuthenticationHandler
的 InitializeAsync
方法中通過 OptionsMonitor.Get( scheme )
取得對應的選項實例,並賦值給屬性Options
由於每個認證處理類都是繼承自AuthenticationHandler
,因此將始終獲得與Scheme
相關聯的Options
實例。
// AuthenticatioinHandler.cs
public async Task InitializeAsync(AuthenticationScheme scheme, HttpContext context)
{
if (scheme == null)
{
throw new ArgumentNullException(nameof(scheme));
}
if (context == null)
{
throw new ArgumentNullException(nameof(context));
}
Scheme = scheme;
Context = context;
Options = OptionsMonitor.Get(Scheme.Name); // Here.
await InitializeEventsAsync();
await InitializeHandlerAsync();
}
認證核心類:
// class
AuthenticationOptions
AuthenticationBuilder
AuthenticationScheme
AuthenticationSchemeBuilder
AuthenticationSchemeProvider
AuthenticationHandler
AuthenticationHandlerProvider
AuthenticationMiddleware
AuthenticationService