Пример #1
0
        private string GetAccessToken(SessionUser user)
        {
            var authClaims = new[]
            {
                new Claim(JwtRegisteredClaimNames.Sub, user.Id.ToString()),
                new Claim(JwtRegisteredClaimNames.Sub, user.Name),
                new Claim(JwtRegisteredClaimNames.Sub, "admin")
            };

            var authSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes("TESTTESTTESTTESTTESTTEST"));
            var token          = new JwtSecurityToken(
                issuer: "jjf",
                audience: "jiangjf",
                expires: DateTime.Now.AddHours(2),
                claims: authClaims,
                signingCredentials: new SigningCredentials(authSigningKey, SecurityAlgorithms.HmacSha256)
                );

            return(new JwtSecurityTokenHandler().WriteToken(token));
        }
Пример #2
0
        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="username"></param>
        /// <param name="password"></param>
        /// <param name="code"></param>
        /// <returns></returns>
        public AjaxResult CheckLogin(string username, string password, string code)
        {
            if (!username.Equals("kbdadmin"))
            {
                if (1 == 2)
                {
                    throw new Exception("验证码错误,请重新输入");
                }
            }
            SessionUser sessionUser = new SessionUser()
            {
                Id   = 1,
                Name = "jiangjf",
                Role = "admin"
            };
            string jwtToken = GetAccessToken(sessionUser);

            return(new AjaxResult {
                state = ResultType.success.ToString(), message = jwtToken
            });
        }