分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 教程案例

aspnetcore 认证相关类简要说明二

发布时间:2023-09-06 02:20责任编辑:傅花花关键词:暂无标签

能过《aspnetcore 认证相关类简要说明一》我们已经了解如何将AuthenticationOptions注入到我们依赖注入系统。接下来,我们将了解一下IAuthenticationSchemeProvider通过AuthenticationOptions如何提供AuthenticationScheme的。aspnetcore 中IAuthenticationSchemeProvider默认实现是AuthenticationSchemeProvider,我们简单的分析下它的源码(以下是我简化的源码):

 ????public class AuthenticationSchemeProvider : IAuthenticationSchemeProvider ????{
        private readonly IDictionary<string, AuthenticationScheme> _schemes;
       ... ????????public AuthenticationSchemeProvider(IOptions<AuthenticationOptions> options){
        foreach(var scheme in options.Schemes){
          var authenticationScheme=scheme.Build();
          _schemes.Add(authenticationScheme.Name,authenticationScheme);
        }
      }
     ... ????????public virtual Task<AuthenticationScheme> GetSchemeAsync(string name){
        return Task.FromResult(_schemes[name]);
      } ????????

      public virtual void AddScheme(AuthenticationScheme scheme); ????}

还记得我们上篇文章中通过services.AddAuthentication注入AuthenticationOptions吗?在AuthenticationSchemeProvider构造方法中,将通过参数传入给该类的对象。在构造方法中,通过foreach options对象的Schemes属性Buid方法获得AuthenticationScheme,以它的Name属性作为_schemes字典的key,保存AuthenticationScheme到字典中。我们还可以通过AuthenticationSchemeProvider类的AddScheme添加认证方案。

接下来,我们继续看看IAuthenticationHandlerProvider类实现类AuthenticationHandlerProvider:

public class AuthenticationHandlerProvider : IAuthenticationHandlerProvider ???{ ???????public AuthenticationHandlerProvider(IAuthenticationSchemeProvider schemes) ???????{ ???????????Schemes = schemes; ???????} ???????public IAuthenticationSchemeProvider Schemes { get; } ???????private Dictionary<string, IAuthenticationHandler> _handlerMap = new Dictionary<string, IAuthenticationHandler>(StringComparer.Ordinal); ???????public async Task<IAuthenticationHandler> GetHandlerAsync(HttpContext context, string authenticationScheme) ???????{ ???????????if (_handlerMap.ContainsKey(authenticationScheme)) ???????????{ ???????????????return _handlerMap[authenticationScheme]; ???????????} ???????????var scheme = await Schemes.GetSchemeAsync(authenticationScheme); ??????????...var handler = (context.RequestServices.GetService(scheme.HandlerType) ?? ???????????????ActivatorUtilities.CreateInstance(context.RequestServices, scheme.HandlerType)) ???????????????as IAuthenticationHandler; ???????????if (handler != null) ???????????{ ???????????????await handler.InitializeAsync(scheme, context); ???????????????_handlerMap[authenticationScheme] = handler; ???????????} ???????????return handler; ???????} ???}

该类实现还是比较简单的,通过构造方法,将IAuthenticationSchemeProvider注入进来,该类最重要的方法也是该类除了构造方法之外的唯一方法GetHandlerAsync。还记得我在上篇说明AuthenticationOptions类AddScheme<THandler>方法特别提醒注意THandler必须是IAuthenticationHandler接口类型吗?我们就是通过该类的GetHandlerAsync获取到。

既然讲到IAuthenticationHandler接口,我就简单复制一下该接口定义的方法代码放下面:

 ???public interface IAuthenticationHandler ???{ ???????Task InitializeAsync(AuthenticationScheme scheme, HttpContext context); ???????Task<AuthenticateResult> AuthenticateAsync(); ???????Task ChallengeAsync(AuthenticationProperties properties); ???????Task ForbidAsync(AuthenticationProperties properties); ???}

关于该接口的第一个方法,我们在上一段代码有调用,就不解释了。既然是认证,整个认证过程中最重要的逻辑当然实现在IAuthenticationHandler的AuthenticateAsync方法中。

本篇主要讲AuthenticationSchemeProvider和IAuthenticationHandlerProvider,以及简单提了一下IAuthenticationHandler,明天我们继续讲IAuthenticationHandler的默认实现。

由于时间已经很晚了,本篇就到此结束。

aspnetcore 认证相关类简要说明二

原文地址:https://www.cnblogs.com/koeltp/p/9880441.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved