Esempio n. 1
0
        public static string CreateJwtToken(LoginForJwt 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);
        }
Esempio n. 2
0
 public static LoginForJwt AddToLoginForJwt(this DbContext context, LoginForJwt model)
 {
     context.Set <LoginForJwt>().Add(model);
     return(model);
 }
Esempio n. 3
0
 public static string CreateJwtToken(LoginForJwt model)
 {
     return(CreateJwtToken(model, null));
 }