Example #1
0
        public static string CreateJwtToken(MemberLogin model, TimeSpan?expiration)
        {
            var secretKey  = "STkzn6iROYF8YqE840An";
            var signingKey = new SymmetricSecurityKey(System.Text.Encoding.ASCII.GetBytes(secretKey));

            var now = DateTime.UtcNow;

            if (!expiration.HasValue)
            {
                expiration = TimeSpan.FromMinutes(30 * 2 * 2);
            }

            var claims = new Claim[]
            {
                new Claim(ClaimTypes.Name, model.Flag),
                new Claim(JwtRegisteredClaimNames.Jti, Guid.NewGuid().ToString()),
                new Claim(JwtRegisteredClaimNames.Iat, CommonUtil.ToTimestamp(now).ToString(), ClaimValueTypes.Integer64)
            };

            // Create the JWT and write it to a string
            var jwt = new JwtSecurityToken(
                issuer: "wechat",    //token 是给谁的
                audience: "user",    //读者
                claims: claims,
                notBefore: now,
                expires: now.Add(expiration.Value),
                signingCredentials: new SigningCredentials(signingKey, SecurityAlgorithms.HmacSha256));

            var encodedJwt = new JwtSecurityTokenHandler().WriteToken(jwt);

            return(encodedJwt);
        }
Example #2
0
 public static MemberLogin AddToMemberLogin(this DbContext context, MemberLogin model)
 {
     context.Set <MemberLogin>().Add(model);
     return(model);
 }
Example #3
0
 public static string CreateJwtToken(MemberLogin model)
 {
     return(CreateJwtToken(model, null));
 }