如何使用.NetCore自带类库实现JSON Web Token 认证【二】如何获取JSON Web Token

登录控制器登录控制器的代码public class LoginController : BaseController{#region 服务依赖private readonly ITokenService tokenHelper;public LoginController(ITokenService _tokenHelper){tokenHelper = _to...

登录控制器

登录控制器的代码

    public class LoginController : BaseController
    {

        #region 服务依赖

        private readonly ITokenService tokenHelper;
        public LoginController(ITokenService _tokenHelper)
        {
            tokenHelper = _tokenHelper;
        }

        #endregion

        public ActionResult Login(LoginParaModel paraModel)
        {

            //根据用户名和密码去数据库查询,判断用户是否存在,判断密码是否正确
            if (paraModel.UserLoginName == "NoUser")
            {
                throw new Exception("当前用户不存在");
            }

            //带额外数据的JWT 
            //TnToken tnToken = tokenHelper.CreateToken(keyValuePairs);

            //不带额外数据的JWT 
            LoginViewModel viewModel = tokenHelper.CreateToken();

            //返回前端
            return JsonResult(viewModel);

        }
    }

登录控制器的参数模型

    public class LoginParaModel
    {
        /// <summary>
        /// 用户登录名称
        /// </summary>
        [Required]
        public string UserLoginName { get; set; }

        /// <summary>
        /// 登录密码
        /// </summary>
        [Required]
        public string PassWord { get; set; }
    }

登录控制器的返回值模型

    public class LoginViewModel
    {
        /// <summary>
        /// JsonWebToken字符串
        /// </summary>
        public string TokenStr { get; set; }
        /// <summary>
        /// JWT有效期
        /// </summary>
        public DateTime Expires { get; set; }
    }

Token服务

Token服务接口

    public interface ITokenService
    {
        /// <summary>
        /// 将一个Model作为额外数据,生成token
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="user"></param>
        /// <returns></returns>
        LoginViewModel CreateToken<T>(T user) where T : class;

        /// <summary>
        /// 创建 TOKEN 无负载数据
        /// </summary>
        /// <returns></returns>
        LoginViewModel CreateToken();
    }

Token服务实例

    public class TokenService : ITokenService
    {

        #region 依赖注入

        private readonly IOptions<JwtConfig> _options;
        public TokenService(IOptions<JwtConfig> options)
        {
            _options = options;
        }

        #endregion



        //创建 TOKEN 无负载数据
        public LoginViewModel CreateToken()
        {

            List<Claim> claims = new List<Claim>();

            return CreateToken(claims);

        }


        /// <summary>
        /// 将一个Model作为额外数据,生成token
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="user"></param>
        /// <returns></returns>
        public LoginViewModel CreateToken<T>(T user) where T : class
        {


            //携带的额外数据,类似一个键值对
            List<Claim> claims = new List<Claim>();

            //填充负载数据的键值对
            foreach (var item in user.GetType().GetProperties())
            {
                object obj = item.GetValue(user);
                string value = "";
                if (obj != null)
                    value = obj.ToString();

                claims.Add(new Claim(item.Name, value));
            }

            //创建token
            return CreateToken(claims);

        }


        private LoginViewModel CreateToken(List<Claim> claims)
        {
            var now = DateTime.Now; var expires = now.Add(TimeSpan.FromMinutes(_options.Value.AccessTokenExpiresMinutes));
            var token = new JwtSecurityToken(
                issuer: _options.Value.Issuer,//Token发布者
                audience: _options.Value.Audience,//Token接受者
                claims: claims,//携带的负载数据
                notBefore: now,//当前时间token生成时间
                expires: expires,//过期时间
                signingCredentials: new SigningCredentials(
                    new SymmetricSecurityKey(Encoding.UTF8.GetBytes(_options.Value.IssuerSigningKey)), SecurityAlgorithms.HmacSha256));
            return new LoginViewModel { TokenStr = new JwtSecurityTokenHandler().WriteToken(token), Expires = expires };
        }
    }

appsettings.json中添加相关配置

  //JsonWebToken配置
  "JsonWebToken": {
    "Issuer": "Wu-Tian", //Token发布者
    "Audience": "UX", //Token接受者
    "IssuerSigningKey": "ThisIsIssuerSigningKey", //签名秘钥(拿到秘钥就可以构建服务器认可的token)
    "AccessTokenExpiresMinutes": "30" //过期时间(分钟)
  }

appsettings.json中相关配置对应的模型

    public class JwtConfig
    {
        /// <summary>
        /// 发布者
        /// </summary>
        public string Issuer { get; set; }
        /// <summary>
        /// 接收者
        /// </summary>
        public string Audience { get; set; }
        /// <summary>
        /// 发布者秘钥
        /// </summary>
        public string IssuerSigningKey { get; set; }
        /// <summary>
        /// Token有效的分钟数
        /// </summary>
        public int AccessTokenExpiresMinutes { get; set; }
    }

Startup.cs中添加相关服务

ConfigureServices添加

            #region JsonWebToken服务相关

            //JsonWebToken服务依赖注入
            services.AddScoped(typeof(ITokenService), typeof(TokenService));

            //读取配置文件配置的JsonWebToken相关配置
            services.Configure<JwtConfig>(Configuration.GetSection("JsonWebToken"));

            //启用JsonWebToken
            services.AddAuthentication(Options =>
            {
                Options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
                Options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
            }).

            AddJwtBearer();

            #endregion

Configure中添加

//使用认证中间件
app.UseAuthentication();

本文标题为:如何使用.NetCore自带类库实现JSON Web Token 认证【二】如何获取JSON Web Token