Пример #1
0
        public string writeToken(string pvtkey, Claim[] claims, string username)
        {
            JwtTokenCreator     jwtTokenCreator     = new JwtTokenCreator();
            JsonWebTokenHandler jsonWebTokenHandler = new JsonWebTokenHandler();

            privateKey = pvtkey;
            var securityKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(privateKey));
            var credentials = new SigningCredentials(securityKey, SecurityAlgorithms.HmacSha256);

            var jwtToken = new JwtSecurityToken("Millo", username, claims, DateTime.Now, DateTime.Now.AddDays(2000), credentials);
            //Console.Writeline(new JwtSecurityTokenHandler().WriteToken(jwtToken));
            var jwt = new JwtSecurityTokenHandler().WriteToken(jwtToken);

            return(jwt);
        }
Пример #2
0
        public async Task <string> CreateToken(User user)
        {
            string          token           = string.Empty;
            JwtTokenCreator jwtTokenCreator = new JwtTokenCreator();
            string          PrivateKey      = user.PrivateToken;

            var claims = new Claim[]
            {
                new Claim("Id", user.UserId.ToString()),
                new Claim("UserName", user.UserName),
                new Claim("Role", user.Role),
                new Claim("FullName", user.FirstName + " " + user.LastName),
            };
            await Task.Run(() =>
                           jwtTokenCreator.writeToken(PrivateKey, claims, user.UserName)
                           );

            return(token);
        }
Пример #3
0
        public async Task <string> CreateToken(User user)
        {
            string          token           = string.Empty;
            JwtTokenCreator jwtTokenCreator = new JwtTokenCreator();
            string          PrivateKey      = user.PrivateToken;

            // var claims = new Claim[]
            //{
            //    new Claim(type:"id",user.UserId.ToString()),
            //     new Claim(type:"UserName",user.UserName),
            //     new Claim(type:ClaimTypes.Role,user.Role),
            //     new Claim(type:"FullName",user.FirstName + " "+user.LastName),
            //     new Claim(type:ClaimTypes.Email,user.Email)

            //};
            ClaimsUserManager claimsUserManager = new ClaimsUserManager();
            var claims = claimsUserManager.ConvertUserToClaimArray(user);
            await Task.Run(() =>
                           token = jwtTokenCreator.writeToken(PrivateKey, claims, user.UserName)
                           );

            return(token);
        }