认证Authentication

认证authentication,基于声明式认证

基于HttpContext的认证的扩展,SignIn、SignOut、Authenticate 、Challenge、  Forbid 、 GetTocken,调用AuthenticationService同名方法执行

在aspnetcore的http下authentication.abstrations与authentication.core对关键抽象进行描述,Security下则是Authentication则是具体的实现

服务注入 AddAuthentication,最重要三个对象AuthenticationService、 AuthenticationHandlerProvider、AuthenticationSchemeProvider三个重要对象

services.AddAuthenticationCore();
services.AddDataProtection();
services.AddWebEncoders();
services.TryAddSingleton();
return new AuthenticationBuilder(services);

public static AuthenticationBuilder AddAuthentication(this IServiceCollection services, string defaultScheme)
            => services.AddAuthentication(o => o.DefaultScheme = defaultScheme);

public static AuthenticationBuilder AddAuthentication(this IServiceCollection services, Action configureOptions) 

 services.Configure(configureOptions);

通过AddAuthentication返回的AuthenticationBuilder 通过AddJwtBearer(或者AddCookie)来指定Scheme类型和需要验证的参数

在Startup类中的Configure方法通过添加UseAuthentication注册认证中间件(AuthenticationMiddleware),在认证过程中,通过AuthenticationSchemeProvider获取正确的Scheme,在AuthenticationService中通过Scheme和AuthenticationHandlerProvider获取正确的AuthenticationHandler,最后通过对应的AuthenticationHandler的AuthenticateAsync方法进行认证流程

 

 

1、AuthenticationOption

scheme:有cookie, bearer, oauth, openid等等,保存着IList schemes

 DefaultScheme、DefaultAuthenticateScheme、DefaultSignInScheme、DefaultSignOutScheme、DefaultChallengeScheme、DefaultForbidScheme??

什么时候赋值??schememap对应是哪个AuthenticationSchemeBuilder,即使用哪个IAuthenticationHandle(方法有InitializeAsync、AuthenticateAsync、ChallengeAsync、ForbidAsync,Signin SignOut方法单独出来)处理

 public interface IAuthenticationRequestHandler : IAuthenticationHandler
    {
        /// 
        /// Returns true if request processing should stop.
        /// 
        ///  if request processing should stop.
        Task<bool> HandleRequestAsync();
    }
    /// 
    /// Used to determine if a handler supports SignIn.
    /// 
    public interface IAuthenticationSignInHandler : IAuthenticationSignOutHandler
    {
        /// 
        /// Handle sign in.
        /// 
        /// The  user.
        /// The  that contains the extra meta-data arriving with the authentication.
        /// A task.
        Task SignInAsync(ClaimsPrincipal user, AuthenticationProperties properties);
    }

    public interface IAuthenticationSignOutHandler : IAuthenticationHandler
    {
        /// 
        /// Signout behavior.
        /// 
        /// The  that contains the extra meta-data arriving with the authentication.
        /// A task.
        Task SignOutAsync(AuthenticationProperties properties);
    }

它的通用方法是AddScheme(),即增加到IList,每一个schemeName映射 Dictionary< schemeName  , AuthenticationSchemeBuilder> schememap

AuthenticationSchemeBuilder:名字、展示名字、Type(IAuthenticationHandler)、用来build方法创建AuthenticationScheme
 
2、AuthenticationSchemeProvider,创建的时候,就从上面Option里面取出 schemes,创建保存是IDictionary _schemes;
_requestHandlers:typeof(IAuthenticationRequestHandler).IsAssignableFrom(scheme.HandlerType) 的集合。
 
3、authenticationHandlerProvider:是得到IAuthenticationHandler,它要使用IAuthenticationSchemeProvider,取出对应的 AuthenticationScheme,再根据创建对应
IAuthenticationHandle的实例。再进行初始化。
 
4、AuthenticationService:使用哪个scheme,没有指定则使用default,使用这个scheme下定义的 IAuthenticationHandler,创建IAuthenticationHandler
 
 
AuthenticateResult:它是AuthenticationHandler对象执行Authenticate方法的认证结果
它有AuthenticationTicket(表示证书颁发的相关信息,如颁发时间,过期时间,重定向地址等)、 ClaimsPrincipal(用来给HttpContext.User)、AuthenticationProperties,有success 、fail静态方法
 
 

你可能感兴趣的:(认证Authentication)