首先我想要簡要說明是AuthenticationScheme類,每次看到Scheme這個單詞我就感受它是一個很高大上的單詞,其實簡單翻譯過來就是認證方案的意思。既然一種方案,那咱們就要知道這個方案的名字(Name)和它對外宣傳的名字(DisplayName)以及這方案的認證處理類型(Type handlerType)。ui
namespace Microsoft.AspNetCore.Authentication { public class AuthenticationScheme { public AuthenticationScheme(string name, string displayName, Type handlerType); //The name of the authentication scheme. public string Name { get; }
//The display name for the scheme. Null is valid and used for non user facing schemes. public string DisplayName { get; } //The Microsoft.AspNetCore.Authentication.IAuthenticationHandler type that handles this scheme. public Type HandlerType { get; } } }
aspnetcore同時還提供了一個構建AuthenticationScheme類的AuthenticationSchemeBuilder類。AuthenticationSchemeBuilder的屬性和AuthenticationScheme是同樣的,這些屬性將做爲實例化AuthenticationScheme類的參數。AuthenticationSchemeBuilder僅比AuthenticationScheme多了一個Build方法。this
namespace Microsoft.AspNetCore.Authentication { /// <summary> /// Used to build <see cref="AuthenticationScheme"/>s. /// </summary> public class AuthenticationSchemeBuilder { /// <summary> /// Constructor. /// </summary> /// <param name="name">The name of the scheme being built.</param> public AuthenticationSchemeBuilder(string name) { Name = name; } ...
//一些和AuthenticationScheme類相同的屬性,將做爲構建AuthenticationScheme的參數
...
// Builds the <see cref="AuthenticationScheme"/> instance. public AuthenticationScheme Build() => new AuthenticationScheme(Name, DisplayName, HandlerType); } }
而AuthenticationSchemeBuilder又在那裏註冊進來的呢?接下來咱們看另一個類:AuthenticationOptionsspa
public class AuthenticationOptions { public AuthenticationOptions(); public IEnumerable<AuthenticationSchemeBuilder> Schemes { get; } public IDictionary<string, AuthenticationSchemeBuilder> SchemeMap { get; } public string DefaultScheme { get; set; } public string DefaultAuthenticateScheme { get; set; } public string DefaultSignInScheme { get; set; } public string DefaultSignOutScheme { get; set; } public string DefaultChallengeScheme { get; set; } public string DefaultForbidScheme { get; set; } public void AddScheme(string name, Action<AuthenticationSchemeBuilder> configureBuilder); public void AddScheme<THandler>(string name, string displayName) where THandler : IAuthenticationHandler; } }
該類的主要做用就是讓咱們配置AuthenticationScheme的一些選項。翻譯
咱們能夠經過該類的AddScheme和AddScheme<THandler>方法添加方案到AuthenticationOptions類Schemes和SchemeMap屬性中。屬性SchemeMap是一個字典類型,它是以咱們AuthenticationScheme的Name屬性做爲Key。aspnetcore默認提供了幾個方案就不一 一列具了,還有一點咱們須要注意的是AddScheme<THandler>中THandler必須是IAuthenticationHandler接口類型。咱們能夠經過在Startup類的ConfigureServices方法中注入咱們的方案:code
public void ConfigureServices(IServiceCollection services) { services.AddAuthentication(x => { x.AddScheme<MyAuth>("abc", "個人中國方案"); }); }
MyAuth是我本身實現IAuthenticationHandler接口添加的類,認證相關的邏輯就實如今他們的方法中,代碼以下:
public class MyAuth : IAuthenticationHandler { public Task<AuthenticateResult> AuthenticateAsync() { throw new NotImplementedException(); } public Task ChallengeAsync(AuthenticationProperties properties) { throw new NotImplementedException(); } public Task ForbidAsync(AuthenticationProperties properties) { throw new NotImplementedException(); } public Task InitializeAsync(AuthenticationScheme scheme, HttpContext context) { throw new NotImplementedException(); } }
經過以上的短篇小論,咱們主要梳理了AuthenticationScheme、AuthenticationSchemeBuilder和AuthenticationOptions三個類。blog