AspNetCore Authentication 是如何將 Scheme 和 Options 關聯的?

當我們調用 AddAuthentication() 時,將返回 AuthenticationBuilder,之後我們通過調用它的 AddScheme() 進行註冊,

在它的內部會調用一個方法 AddSchemeHelper,而這個方法將 Scheme 配置到了 AuthenticationOptions 的私有字段 IList<AuthenticationSchemeBuilder> _schemes 當中;

之後在 AuthenticationHandlerInitializeAsync 方法中通過 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
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章