Authentication 源代码 (addAuthentication ,useAuthentication )

ASP.NET Core[源码分析篇] - Authentication认证》:  https://cloud.tencent.com/developer/article/1498055

 《理解ASP.NET Core验证模型(Claim, ClaimsIdentity, ClaimsPrincipal)不得不读的英文博文

 

--------------------------------------------------------------------------------------------------------------------------

services.AddAuthentication()    参考 :https://www.cnblogs.com/liyouming/p/9916777.html

  AddAuthentication 源代码:https://github.com/dotnet/aspnetcore/blob/3e1e69eccef4ea54c46c7e706413a0448abbbec9/src/Security/Authentication/Core/src/AuthenticationServiceCollectionExtensions.cs

     实际是: services.AddAuthentication(); 

                 services.Configure(configureOptions);     

              【configureOptions  是一个  Action<AuthenticationOptions> 委托方法。定义如下:https://docs.microsoft.com/zh-cn/dotnet/api/microsoft.aspnetcore.authentication.authenticationoptions?view=aspnetcore-3.1    ]


 

 

----------------------------------------------------------------------------------------------------------------------------------------------------

 配置中间件(身份验证)的代码:app.UseAuthentication(); 实际是: app.UseMiddleware<AuthenticationMiddleware>();

public static IApplicationBuilder UseAuthentication(this IApplicationBuilder app)
{
      if (app == null)
     {
          throw new ArgumentNullException(nameof(app));
     }
    return app.UseMiddleware<AuthenticationMiddleware>();
}

AuthenticationMiddleware 源代码 :https://github.com/dotnet/aspnetcore/blob/v3.1.2/src/Security/Authentication/Core/src/AuthenticationMiddleware.cs

    public class AuthenticationMiddleware

    {

        private readonly RequestDelegate _next;

        public IAuthenticationSchemeProvider Schemes { get; set; }

        public AuthenticationMiddleware(RequestDelegate next, IAuthenticationSchemeProvider schemes)    //构造函数

        {

            if (next == null)

            {

                throw new ArgumentNullException(nameof(next));

            }

            if (schemes == null)

            {

                throw new ArgumentNullException(nameof(schemes));

            }

            _next = next;

            Schemes = schemes;

        }

        public async Task Invoke(HttpContext context)

        {

            context.Features.Set<IAuthenticationFeature>(new AuthenticationFeature

            {

                OriginalPath = context.Request.Path,

                OriginalPathBase = context.Request.PathBase

            });

            // Give any IAuthenticationRequestHandler schemes a chance to handle the request

            var handlers = context.RequestServices.GetRequiredService<IAuthenticationHandlerProvider>();

            foreach (var scheme in await Schemes.GetRequestHandlerSchemesAsync())

            {

                var handler = await handlers.GetHandlerAsync(context, scheme.Name) as IAuthenticationRequestHandler;

                if (handler != null && await handler.HandleRequestAsync())

                {

                    return;

                }

            }

            var defaultAuthenticate = await Schemes.GetDefaultAuthenticateSchemeAsync();

            if (defaultAuthenticate != null)

            {

                var result = await context.AuthenticateAsync(defaultAuthenticate.Name);

                if (result?.Principal != null)

                {

                    context.User = result.Principal;

                }

            }

            await _next(context);

        }

    }

 

 

ASP.NET Core Identity 实战(1)

 

posted @   Hopesun  阅读(1160)  评论(0编辑  收藏  举报
编辑推荐:
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· C#/.NET/.NET Core技术前沿周刊 | 第 29 期(2025年3.1-3.9)
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异
点击右上角即可分享
微信分享提示