首页 > 其他分享 >.Net Core中自定义认证实现

.Net Core中自定义认证实现

时间:2022-11-29 17:22:06浏览次数:81  
标签:Core 自定义 CustomerAuthenticationHandler 认证 CustomerSchemeName new Net public

一、起因

 最近项目中需要对项目同时支持JWT认证,以及自定义的认证校验方式认证。通过对官方文档了解,得到认证实现主要通过继承 IAuthenticationHandler 或 AuthenticationHandler<TOptions>来实现自定义认证的处理。 

 那么接下来实现一个自定义的认证访问。

二、自定义认证实现 

 1、根据前面内容得知,处理认证通过IAuthenticationHandler 实例处理;那么首先添加一个自定义IAuthenticationHandler 类型:

复制代码
/// <summary>
/// 方式一:自定义认证处理器
/// </summary>
public class CustomerAuthenticationHandler : IAuthenticationHandler
{
    private IUserService _userService;
    public CustomerAuthenticationHandler(IUserService userService)
    {
        _userService = userService;
    }

    /// <summary>
    /// 自定义认证Scheme名称
    /// </summary>
    public const string CustomerSchemeName = "cusAuth";

    private AuthenticationScheme _scheme;
    private HttpContext _context;

    /// <summary>
    /// 认证逻辑:认证校验主要逻辑
    /// </summary>
    /// <returns></returns>
    public Task<AuthenticateResult> AuthenticateAsync()
    {
        AuthenticateResult result;
        _context.Request.Headers.TryGetValue("Authorization", out StringValues values);
        string valStr = values.ToString();
        if (!string.IsNullOrWhiteSpace(valStr))
        {
            //认证模拟basic认证:cusAuth YWRtaW46YWRtaW4=
            string[] authVal = System.Text.Encoding.UTF8.GetString(Convert.FromBase64String(valStr.Substring(CustomerSchemeName.Length + 1))).Split(':');
            var loginInfo = new Dto.LoginDto() { Username = authVal[0], Password = authVal[1] };
            var validVale = _userService.IsValid(loginInfo);
            if (!validVale)
                result = AuthenticateResult.Fail("未登陆");
            else
            {
                var ticket = GetAuthTicket(loginInfo.Username, "admin");
                result = AuthenticateResult.Success(ticket);
            }
        }
        else
        {
            result = AuthenticateResult.Fail("未登陆");
        }
        return Task.FromResult(result);
    }

    /// <summary>
    /// 未登录时的处理
    /// </summary>
    /// <param name="properties"></param>
    /// <returns></returns>
    public Task ChallengeAsync(AuthenticationProperties properties)
    {
        _context.Response.StatusCode = (int)HttpStatusCode.Unauthorized;
        return Task.CompletedTask;
    }

    /// <summary>
    /// 权限不足时处理
    /// </summary>
    /// <param name="properties"></param>
    /// <returns></returns>
    public Task ForbidAsync(AuthenticationProperties properties)
    {
        _context.Response.StatusCode = (int)HttpStatusCode.Forbidden;
        return Task.CompletedTask;
    }

    /// <summary>
    /// 初始化认证
    /// </summary>
    /// <param name="scheme"></param>
    /// <param name="context"></param>
    /// <returns></returns>
    public Task InitializeAsync(AuthenticationScheme scheme, HttpContext context)
    {
        _scheme = scheme;
        _context = context;
        return Task.CompletedTask;
    }

    #region 认证校验逻辑

    /// <summary>
    /// 生成认证票据
    /// </summary>
    /// <param name="name"></param>
    /// <param name="role"></param>
    /// <returns></returns>
    private AuthenticationTicket GetAuthTicket(string name, string role)
    {
        var claimsIdentity = new ClaimsIdentity(new Claim[]
        {
    new Claim(ClaimTypes.Name, name),
    new Claim(ClaimTypes.Role, role),
        }, CustomerSchemeName);
        var principal = new ClaimsPrincipal(claimsIdentity);
        return new AuthenticationTicket(principal, _scheme.Name);
    }

    #endregion
}

/// <summary>
/// 方式二:继承已实现的基类
/// </summary>
public class SubAuthenticationHandler : AuthenticationHandler<AuthenticationSchemeOptions>
{
    public SubAuthenticationHandler(IOptionsMonitor<AuthenticationSchemeOptions> options, ILoggerFactory logger, UrlEncoder encoder, ISystemClock clock)
        : base(options, logger, encoder, clock)
    {
    }

    protected override Task<AuthenticateResult> HandleAuthenticateAsync()
    {
        throw new NotImplementedException();
    }
}
复制代码

 2、在Startup.cs中启用自定义认证:

复制代码
public void ConfigureServices(IServiceCollection services)
{
    //other code
    services.AddAuthentication(o =>
    {
        x.DefaultAuthenticateScheme = CustomerAuthenticationHandler.CustomerSchemeName;
        x.DefaultChallengeScheme = CustomerAuthenticationHandler.CustomerSchemeName;
        o.AddScheme<CustomerAuthenticationHandler>(CustomerAuthenticationHandler.CustomerSchemeName, CustomerAuthenticationHandler.CustomerSchemeName);
    });
    //other code
}

public void Configure(IApplicationBuilder app)
{
    //other code
    app.UseRouting();

    //在UseRouting后;UseEndpoints前添加以下代码
    app.UseAuthentication();
    app.UseAuthorization();

    //other code
    app.UseEndpoints()
}
复制代码

 3、在控制器上添加认证标记,测试验证

复制代码
//指定认证时,采用CustomerAuthenticationHandler.CustomerSchemeName
[Authorize(AuthenticationSchemes = CustomerAuthenticationHandler.CustomerSchemeName)]
[Route("api/[controller]")]
[ApiController]
public class AuditLogController : ControllerBase
{
 //code
}
复制代码

  调用

  

三、多认证支持

 在实际项目中可能存在,对一个控制器支持多种认证方式如:常用的Jwt认证、自定义认证等,那么如何实现呢?

 1、在Startup的ConfigureServices 方法中添加以下逻辑:

复制代码
public void ConfigureServices(IServiceCollection services)
{
    //other code
    services.Configure<JwtSetting>(Configuration.GetSection("JWTSetting"));
    var token = Configuration.GetSection("JWTSetting").Get<JwtSetting>();
    //JWT认证
    services.AddAuthentication(x =>
    {
        x.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
        x.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
     //添加自定义认证处理器 x.AddScheme<CustomerAuthenticationHandler>(CustomerAuthenticationHandler.CustomerSchemeName, CustomerAuthenticationHandler.CustomerSchemeName); }).AddJwtBearer(x => { x.RequireHttpsMetadata = false; x.SaveToken = true; x.TokenValidationParameters = new TokenValidationParameters { ValidateIssuerSigningKey = true, IssuerSigningKey = new SymmetricSecurityKey(Encoding.ASCII.GetBytes(token.SecretKey)), ValidIssuer = token.Issuer, ValidAudience = token.Audience, ValidateIssuer = false, ValidateAudience = false }; }); //other code }
复制代码

 2、在需要支持多种认证方式的控制器上添加标记:

复制代码
//指定认证时,采用CustomerAuthenticationHandler.CustomerSchemeName
[Authorize(AuthenticationSchemes = CustomerAuthenticationHandler.CustomerSchemeName)]
[Route("api/[controller]")]
[ApiController]
public class AuditLogController : ControllerBase
{
 //code
}



//指定认证采用JWT 
[Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)] 
public class WeatherForecastController : ControllerBase 
{
  //code 
}
复制代码

   这样就支持了两种认证方式

 3、一个控制器支持多种认证类型:继承Jwt认证处理,并根据Scheme那么调用自定义的认证处理器:

复制代码
/// <summary>
/// 方式二:同时支持多种认证方式
/// </summary>
public class MultAuthenticationHandler : JwtBearerHandler
{
    public const string MultAuthName = "MultAuth";
    IUserService _userService;
    public MultAuthenticationHandler(IOptionsMonitor<JwtBearerOptions> options, ILoggerFactory logger, UrlEncoder encoder, ISystemClock clock, IUserService userService)
        : base(options, logger, encoder, clock)
    {
        _userService = userService;
    }

    protected override Task<AuthenticateResult> HandleAuthenticateAsync()
    {
        Context.Request.Headers.TryGetValue("Authorization", out StringValues values);
        string valStr = values.ToString();
        if (valStr.StartsWith(CustomerAuthenticationHandler.CustomerSchemeName))
        {
            var result = Valid();
            if (result != null)
                return Task.FromResult(AuthenticateResult.Success(result));
            else
                return Task.FromResult(AuthenticateResult.Fail("未认证"));
        }
        else
            return base.AuthenticateAsync();
    }

    private AuthenticationTicket Valid()
    {
        Context.Request.Headers.TryGetValue("Authorization", out StringValues values);
        string valStr = values.ToString();
        if (!string.IsNullOrWhiteSpace(valStr))
        {
            //认证模拟basic认证:cusAuth YWRtaW46YWRtaW4=
            string[] authVal = System.Text.Encoding.UTF8.GetString(Convert.FromBase64String(valStr.Substring(CustomerAuthenticationHandler.CustomerSchemeName.Length + 1))).Split(':');
            var loginInfo = new Dto.LoginDto() { Username = authVal[0], Password = authVal[1] };
            if (_userService.IsValid(loginInfo))
                return GetAuthTicket(loginInfo.Username, "admin");
        }
        return null;
    }

    /// <summary>
    /// 生成认证票据
    /// </summary>
    /// <param name="name"></param>
    /// <param name="role"></param>
    /// <returns></returns>
    private AuthenticationTicket GetAuthTicket(string name, string role)
    {
        var claimsIdentity = new ClaimsIdentity(new Claim[]
        {
            new Claim(ClaimTypes.Name, name),
            new Claim(ClaimTypes.Role, role),
        }, CustomerAuthenticationHandler.CustomerSchemeName);
        var principal = new ClaimsPrincipal(claimsIdentity);
        return new AuthenticationTicket(principal, CustomerAuthenticationHandler.CustomerSchemeName);
    }
}
复制代码

四、总结

  .Net Core中的自定义认证主要通过实现IAuthenticationHandler 接口实现,如果要实现多认证方式通过AddScheme 应用自定义实现的认证处理器。

 源码:github

 参考:认证

 

转 https://www.cnblogs.com/cwsheng/p/15835731.html

标签:Core,自定义,CustomerAuthenticationHandler,认证,CustomerSchemeName,new,Net,public
From: https://www.cnblogs.com/wl-blog/p/16935979.html

相关文章

  • ArcObjects SDK开发 007 自定义App-Command-Tool框架
    1、为什么再设计一套App-Command-Tool框架为什么我们要自己再设计一套App-Command框架,而不直接使用AOAPI中的AxControl-ICommand这套已经非常好的框架呢?1、宿主不同。我......
  • NetwoekManger之nmcli
    nmcliconnectionshow #查看网卡启用状态 nmcliconnectionmodifyenp216s0f0ipv4.methodmanualipv4.addresses10.10.1.12/24 ipv4.gateway10.10.1.10ipv4.d......
  • .net如何优雅的使用EFCore
    .net如何优雅的使用EFCore EFCore是微软官方的一款ORM框架,主要是用于实体和数据库对象之间的操作。功能非常强大,在老版本的时候叫做EF,后来.netcore问世,EFCore也随之......
  • 我开发的开源项目,让.NET7中的EFCore更轻松地使用强类型Id
    在领域驱动设计(DDD)中,有一个非常重要的概念:“强类型Id”。使用强类型Id来做标识属性的类型会比用int、Guid等通用类型能带来更多的好处。比如有一个根据根据Id删除用户的方......
  • 汇编实验:自定义键盘中断的处理函数
    汇编实验报告-键盘中断1.实验任务:采用键盘中断方式,当输入是字符或数字的时候,回显输入并回车换行;否则退出。2.运行环境:Windows11+MASM3.题目分析:在课上我们刚刚学习......
  • Kubernetes(K8S) Service 介绍
    定义一组Pod的访问规则存在的意义防止Pod失联(服务发现),Pod重启后,IP会变定义一组Pod访问策略,负载均衡Pod和Service关系根据label和selector标签建立关联......
  • jQuery Core
    jQueryCDN-providedby(mt)MediaTemplejQueryCoreCurrentversion(canbehotlinked)​​jquery-1.10.0.min.js​​-latestversion,minifiedforproduction......
  • android自定义view实现progressbar的效果
    一键清理是很多Launcher都会带有的功能,其效果也比较美观。实现方式也许有很多中,其中常见的是使用图片drawable来完成的,具体可以参考这篇文章:​​模仿......
  • netty实现聊天和rpc
    title:netty实现聊天和rpcdate:2022-11-2914:40:13tags:项目地址https://gitee.com/hslxy/learn-netty用netty实现rpc的思路首先自定义协议,不用http的原因就是比......
  • 记一次 .NET 某打印服务 非托管内存泄漏分析
    一:背景1.讲故事前段时间有位朋友在微信上找到我,说他的程序出现了内存泄漏,能不能帮他看一下,这个问题还是比较经典的,加上好久没上非托管方面的东西了,这篇就和大家分享一下,话......