.Net6 实现 JWT

.Net6 实现 JWT

(需了解知识)依赖注入

首先安装 Nuget 包 Microsoft.AspNetCore.Authentication.JwtBearer

Program.cs 里添加Jwt

//添加jwt验证:
builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme).AddJwtBearer(options =>
{
    options.TokenValidationParameters = new TokenValidationParameters()
    {
        ValidateIssuer = true,
        ValidIssuer = builder.Configuration["JWT:Issuer"],
        ValidateAudience = true,
        ValidAudience = builder.Configuration["JWT:Audience"],
        ValidateLifetime = true,
        IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(builder.Configuration["JWT:SecretKey"]))
    };
});

//下面的app添加这个  和这个长得很像  app.UseAuthorization();
app.UseAuthentication();//在前
app.UseAuthorization();//在后

在IOC容器里注入 JWT header类和我自己写的 TokenHelper类

//用于Jwt的各种操作
     builder.RegisterType<JwtSecurityTokenHandler>().InstancePerLifetimeScope();
//自己写的支持泛型存入Jwt 便于扩展
     builder.RegisterType<TokenHelper>().InstancePerLifetimeScope();

我的TokenHelper类 模型也放在这里 比如我放了一个实体进去拿token的时候也会拿出一个实体

using Microsoft.IdentityModel.Tokens;
using System.IdentityModel.Tokens.Jwt;
using System.Reflection;
using System.Security.Claims;
using System.Text;

namespace Quartz_Crawler.Tools
{
    public class TokenHelper
    {
        private readonly IConfiguration _configuration;
        private readonly JwtSecurityTokenHandler _jwtSecurityTokenHandler;
        public TokenHelper(IConfiguration configuration, JwtSecurityTokenHandler jwtSecurityTokenHandler)
        {
            _configuration = configuration;
            _jwtSecurityTokenHandler = jwtSecurityTokenHandler;
        }
        /// <summary>
        /// 创建加密JwtToken
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        public string CreateJwtToken<T>(T user)
        {
            var claimList = this.CreateClaimList(user);
            //  从 appsettings.json 中读取SecretKey
            var secretKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(_configuration["JWT:SecretKey"]));
            // 从 appsettings.json 中读取Expires
            var expires = Convert.ToDouble(_configuration["JWT:Expires"]);
            //  选择加密算法
            var algorithm = SecurityAlgorithms.HmacSha256;
            // 生成Credentials
            var signingCredentials = new SigningCredentials(secretKey, algorithm);
            JwtSecurityToken jwtSecurityToken = new JwtSecurityToken(
              _configuration["JWT:Issuer"],     //Issuer
               _configuration["JWT:Audience"],   //Audience
               claims: claimList,
               DateTime.Now,                    //notBefore
               DateTime.Now.AddDays(expires),   //expires
               signingCredentials               //Credentials
               );
            string jwtToken = _jwtSecurityTokenHandler.WriteToken(jwtSecurityToken);
            return jwtToken;
        }

        public T GetToken<T>(string Token)
         {
            Type t = typeof(T);

            object objA = Activator.CreateInstance(t);
            var b = _jwtSecurityTokenHandler.ReadJwtToken(Token);
            foreach (var item in b.Claims)
            {
                PropertyInfo _Property = t.GetProperty(item.Type);
                if (_Property != null && _Property.CanRead)
                {
                    _Property.SetValue(objA, item.Value, null);
                }
                
            }
            return (T)objA; 
        }


        /// <summary>
        /// 创建包含用户信息的CalimList
        /// </summary>
        /// <param name="authUser"></param>
        /// <returns></returns>
        private List<Claim> CreateClaimList<T>(T authUser)
        {
            var Class = typeof(User);
            List<Claim> claimList = new List<Claim>();
            foreach (var item in Class.GetProperties())
            {
                claimList.Add(new Claim(item.Name, Convert.ToString(item.GetValue(authUser))));
            }
            return claimList;
        }

      


    }
    /// <summary>
    /// 登录用户信息
    /// </summary>
    public class User
    {
        public int UserID { get; set; }
        public string Email { get; set; }

        public string Name { get; set; }
        public string Role { get; set; }


        public User(int userID=default, string name=default, string email=default, string role=default)
        {
            this.UserID = userID;
            this.Name = name;
            this.Email = email;
            this.Role = role;
        }
    }
    public class UserModel
    {
        public string UserID { get; set; }
        public string Email { get; set; }

        public string Name { get; set; }
        public string Role { get; set; }


        public UserModel()
        {
            
        }
    }
}

我这里是重新自定义了 认证标签 继承这个接口IAuthorizationFilter来实现
从header中获取 Authorization 来验证是否具有权限 以及用户信息 把用户信息存到类里 注入到容器就可以随时随地的拿到用户信息,而无需再从数据库里拿到信息

using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc.Filters;
using Quartz_Crawler.Service;
using Quartz_Crawler.Tools;
using System.IdentityModel.Tokens.Jwt;

namespace Quartz_Crawler.Filter
{
    public class LoginAttrbuteFilter : IAuthorizationFilter
    {
        private readonly IServiceFactory _serviceFactory;
        public JwtSecurityTokenHandler _jwtSecurityTokenHandler;
        private readonly TokenHelper _tokenHelper;
        public LoginAttrbuteFilter(IServiceFactory serviceFactory, JwtSecurityTokenHandler jwtSecurityTokenHandler, TokenHelper tokenHelper)
        {
            _serviceFactory = serviceFactory;
            _jwtSecurityTokenHandler = jwtSecurityTokenHandler;
            _tokenHelper = tokenHelper;
        }

        public void OnAuthorization(AuthorizationFilterContext context)
        {
            var heads = context.HttpContext.Request.Headers["Authorization"];

            var a= _tokenHelper.GetToken<UserModel>(header);
                        
            _serviceFactory.Get();
            throw new NotImplementedException();
        }
    }
}

最后是我的 Controller 里面的代码

using Microsoft.AspNetCore.Mvc;
using Quartz_Crawler.Filter;
using Quartz_Crawler.Tools;
using System.IdentityModel.Tokens.Jwt;

namespace Quartz_Crawler.Controller
{
    [ApiController]
    public class HomeController : ControllerBase
    {
        private readonly ILogger<HomeController> _logger;
        private readonly IConfiguration _configuration;
        private readonly JwtSecurityTokenHandler _jwtSecurityTokenHandler;
        private readonly TokenHelper _tokenHelper;
        public HomeController(ILogger<HomeController> logger, IConfiguration configuration, JwtSecurityTokenHandler jwtSecurityTokenHandler, TokenHelper tokenHelper)
        {
            _logger = logger;
            _configuration = configuration;
            _jwtSecurityTokenHandler = jwtSecurityTokenHandler;
            _tokenHelper = tokenHelper;
        }
        [HttpGet]
        [Route("Login")]
        public object Login()
        {
            var aaa = _tokenHelper.CreateJwtToken(new Tools.User(1, "22", "33", "44"));
            return aaa;

        }
        [HttpGet]
        [Route("Get")]
        public object Get()
        {
            return null;
        }
        [HttpGet]
        [Route("Post")]
        [TypeFilter(typeof(LoginAttrbuteFilter))]
        public object post()
        {
            return null;
        }
    }
}

ASP.NET Core MVC实现JWT(JSON Web Tokens)是一种用于双方之间安全传输信息的简洁的、URL安全的方式。JWT通常用于Web应用的认证和授权过程。在ASP.NET Core MVC中,可以通过以下步骤实现JWT的生成和验证: 1. 安装必要的NuGet包,如`System.IdentityModel.Tokens.Jwt`,用于JWT的创建和解析。 2. 创建一个认证服务来生成JWT。通常会在登录接口中实现,当用户认证成功后,会生成一个包含用户信息的JWT。 3. 将生成的JWT作为响应的一部分返回给客户端。客户端会将这个JWT存储在本地,通常是在浏览器的localStorage或者sessionStorage中。 4. 在需要进行安全检查的API请求中,客户端需要将JWT附加到请求头中,例如在HTTP请求的`Authorization`头中附带一个`Bearer`令牌。 5. 服务器端需要中间件来拦截请求并验证JWT的有效性。这通常在ASP.NET Core的`Startup.cs`文件中的`ConfigureServices`方法中配置`JwtBearer`认证,并在`Configure`方法中调用`app.UseAuthentication()`和`app.UseAuthorization()`。 6. 如果JWT验证失败,则返回相应的错误信息;如果验证成功,则继续执行请求的处理流程。 示例代码(非完整实现): ```csharp // Startup.cs public void ConfigureServices(IServiceCollection services) { // ... // 配置JWT认证 var signingKey = new SymmetricSecurityKey(Encoding.ASCII.GetBytes("your_secret_key_here")); var options = new JwtBearerOptions { TokenValidationParameters = new TokenValidationParameters { ValidateIssuer = true, ValidIssuer = "your_issuer", ValidateAudience = true, ValidAudience = "your_audience", ValidateLifetime = true, IssuerSigningKey = signingKey } }; services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme) .AddJwtBearer(options); // ... } public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { // ... app.UseAuthentication(); app.UseAuthorization(); // ... } ```
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值