public static TokenModelJWT GetToken(HttpContext httpContext) { if (!httpContext.Request.Headers.ContainsKey("Authorization")) { return(null); } var tokenHeader = httpContext.Request.Headers["Authorization"].ToString().Replace("Bearer ", ""); TokenModelJWT tm = JwtHelper.SerializeJWT(tokenHeader); return(tm); }
/// <summary> /// /// </summary> /// <param name="httpContext"></param> /// <returns></returns> public Task Invoke(HttpContext httpContext) { //检测是否包含'Authorization'请求头 if (!httpContext.Request.Headers.ContainsKey("Authorization")) { return(_next(httpContext)); } var tokenHeader = httpContext.Request.Headers["Authorization"].ToString().Replace("Bearer ", ""); TokenModelJWT tm = JwtHelper.SerializeJWT(tokenHeader); //授权 var claimList = new List <Claim>(); var claim = new Claim(ClaimTypes.Role, tm.Role); claimList.Add(claim); var identity = new ClaimsIdentity(claimList); var principal = new ClaimsPrincipal(identity); httpContext.User = principal; return(_next(httpContext)); }
/// <summary> /// 解析 /// </summary> /// <param name="jwtStr"></param> /// <returns></returns> public static TokenModelJWT SerializeJWT(string jwtStr) { var jwtHandler = new JwtSecurityTokenHandler(); JwtSecurityToken jwtToken = jwtHandler.ReadJwtToken(jwtStr); object role = new object(); try { jwtToken.Payload.TryGetValue(ClaimTypes.Role, out role); //jwtToken.Payload.TryGetValue("Role", out role); } catch (Exception e) { Console.WriteLine(e); throw; } var tm = new TokenModelJWT { id = Convert.ToInt32(jwtToken.Id), Role = role != null?role.ToString() : "", }; return(tm); }
/// <summary> /// 颁发JWT字符串 /// </summary> /// <param name="tokenModel"></param> /// <returns></returns> public static string IssueJWT(TokenModelJWT tokenModel) { var dateTime = DateTime.UtcNow; //new Claim(JwtRegisteredClaimNames.Exp, $"{new DateTimeOffset(DateTime.Now.AddSeconds(10)).ToUnixTimeSeconds()}"); //Claim[] claims = new Claim[] var claims = new List <Claim> { //下边为Claim的默认配置 new Claim(JwtRegisteredClaimNames.Jti, tokenModel.id.ToString()), new Claim(JwtRegisteredClaimNames.Iat, $"{new DateTimeOffset(DateTime.Now).ToUnixTimeSeconds()}"), new Claim(JwtRegisteredClaimNames.Nbf, $"{new DateTimeOffset(DateTime.Now).ToUnixTimeSeconds()}"), //这个就是过期时间,目前是过期100秒,可自定义,注意JWT有自己的缓冲过期时间 new Claim(JwtRegisteredClaimNames.Exp, $"{new DateTimeOffset(DateTime.Now.AddSeconds(100)).ToUnixTimeSeconds()}"), new Claim(JwtRegisteredClaimNames.Iss, "Blog.Core"), new Claim(JwtRegisteredClaimNames.Aud, "wr"), //这个Role是官方UseAuthentication要要验证的Role,我们就不用手动设置Role这个属性了 //new Claim(ClaimTypes.Role,tokenModel.Role), }; claims.AddRange(tokenModel.Role.Split(',').Select(s => new Claim(ClaimTypes.Role, s))); //秘钥 var key = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(JwtHelper.secretKey)); var creds = new SigningCredentials(key, SecurityAlgorithms.HmacSha256); var jwt = new JwtSecurityToken( issuer: "Blog.Core", claims: claims, signingCredentials: creds); var jwtHandler = new JwtSecurityTokenHandler(); var encodedJwt = jwtHandler.WriteToken(jwt); return(encodedJwt); }