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)

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM