Esempio n. 1
0
        public CodeResult <string> Login(string userName, string password)
        {
            if (userName == "admin" && password == "123456")
            {
                var jti     = Guid.NewGuid().ToString(); // 唯一标识
                var expires = TimeSpan.FromHours(2);     // 有效期,这里时2小时
                var claims  = new[]
                {
                    new Claim(JwtRegisteredClaimNames.Jti, jti),
                    new Claim(ClaimTypes.Name, userName)
                };
                SigningCredentials credentials = BuildRsaCredentials();
                JwtSecurityToken   token       = new JwtSecurityToken(Configuration["Jwt:Issuer"], Configuration["Jwt:Audience"], claims, DateTime.Now, DateTime.Now.Add(expires), credentials);

                string tokenString = new JwtSecurityTokenHandler().WriteToken(token);

                return(tokenString.Success());
            }

            return(null);
        }