• ASP.NET Core框架探索之Authentication


    今天我们来探索一下ASP.NET Core中关于权限认证,所谓权限认证,就是通过某些方式获取到用户的信息。

    需要开启权限认证,我们首先需要在容器中注入认证服务,使用services.AddAuthentication。进入该方法的源码,最重要的其实就是AddAuthenticationCore方法,他向容器中注入了认证体系中很重要的对象:IAuthenticationServiceIAuthenticationHandlerProviderIAuthenticationSchemeProvider

     1         public static IServiceCollection AddAuthenticationCore(this IServiceCollection services)
     2         {
     3             if (services == null)
     4             {
     5                 throw new ArgumentNullException(nameof(services));
     6             }
     7 
     8             services.TryAddScoped<IAuthenticationService, AuthenticationService>();
     9             services.TryAddSingleton<IClaimsTransformation, NoopClaimsTransformation>(); // Can be replaced with scoped ones that use DbContext
    10             services.TryAddScoped<IAuthenticationHandlerProvider, AuthenticationHandlerProvider>();
    11             services.TryAddSingleton<IAuthenticationSchemeProvider, AuthenticationSchemeProvider>();
    12             return services;
    13         }
    View Code

    然后还需要在Configure管道处理中加上需要权限认证app.UseAuthentication(),该方法会向处理管道中添加名为AuthenticationMiddleware的中间件,具体一个请求到来时,框架是如何进行权限认证的处理逻辑都在这里。

     

    下面我们就大致讨论一下AuthenticationMiddleware中间件中对于权限认证过程的细节:

    1、请求进入认证环节,首先会进入IAuthenticationSchemeProvider对象拿到AuthenticationOptions对象中的IList<AuthenticationSchemeBuilder>集合,所有注册到认证体系中的认证方案都会在该集合中拿到。

    2、通过循环集合,调用AuthenticationSchemeBuilder对象的Builder方法去获得所有的认证方案AuthenticationScheme,将得到的AuthenticationScheme以AuthenticationScheme.Name作为key保存到AuthenticationSchemeProvider对象的字典集合IDictionary<string, AuthenticationScheme>中,这样AuthenticationSchemeProvider就拥有了返回AuthenticationScheme的能力,通过传入认证方案名称,即可得到方案对象。

    3、拿到了AuthenticationScheme对象,其认证处理类型即可在HandlerType属性中得到,然后在IAuthenticationHandlerProvider的实例对象中根据HandlerType创建一个认证处理对象IAuthenticationHandler,最后调用该对象的AuthenticateAsync方法就是完成具体的认证处理逻辑。需要注意的是这里的IAuthenticationHandler对象会根据每个AuthenticationScheme具备的认证处理逻辑而来,所以得到的AuthenticationScheme不同,认证处理的逻辑就不同。

    下图是我针对认证流程画的一个的大致过程:

     

     以上就是用户认证体系中大致认证逻辑,当然通过以上的描述还会存在以下疑点:

    1、进入认证环节后,AuthenticationSchemeProvider可能会拥有很多个AuthenticationScheme,需要通过传入某个认证方案名称来拿到具体的AuthenticationScheme,那么这个传入的动作是在哪里呢?

    2、AuthenticationSchemeProvider对象在实例化的时候从AuthenticationOptions对象中获取IList<AuthenticationSchemeBuilder>集合进行循环Builder得到AuthenticationScheme,那AuthenticationOptions中该集合的数据是在什么时候添加进去的呢?

    3、如何在认证体系中添加需要的AuthenticationScheme呢?

    以Cookie为例,我们会在向容器添加认证服务的时候就会指定默认的认证方案名称:service.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme),方法返回AuthenticationBuilder,但此刻还只是组建了认证服务的框架,还需要向这个框架中添加处理认证的方案,所以会通过AuthenticationBuilder.AddCookie将Cooike的认证方案添加进来,当然我们可以添加很多个方案,比如使用JWT进行认证,但实际认证过程还是根据传递的默认方案的名称进行的。

     

    今天的分享就到这里,后续会继续带来更多关于ASP.NET Core框架的解读,希望有收获的小伙伴推荐支持一下,有疑问可评论区留言讨论!

  • 相关阅读:
    软件工程开发模式:从传统到现代的演进
    阿里云和AWS的对比研究一:AWS的起源
    抖音短视频提取器|视频内容批量提取软件
    第十二章 使用中的 OpenAPI 属性
    netty报错:too many open files,调整文件句柄
    [算法日志]图论: 广度优先搜索(BFS)
    【数据结构】list.h 详细使用教程 -- 附带例子代码
    小程序配置服务器域名
    中介者模式(Mediator Pattern)
    uni-app 阉割版的vue uni-app开发中的小坑。
  • 原文地址:https://www.cnblogs.com/XFlyMan/p/16008208.html