Exemplo n.º 1
0
        public JWTDTO GetJWT(LoginDTO credentials)
        {
            string token = null;
            USER   user;
            JWTDTO retVal;

            user = userRepository.CheckCredentials(credentials.Username, credentials.Password);
            var payload = new Dictionary <string, object>();

            retVal = new JWTDTO()
            {
                Success = false
            };


            if (CheckHelper.IsFilled(user))
            {
                payload.Add("Id", user.USR_ID);
                token = JWT.JsonWebToken.Encode(payload,
                                                AppConstants.JWT_SECRET_KEY,
                                                JWT.JwtHashAlgorithm.HS256);
                retVal = new JWTDTO()
                {
                    Role    = user.USR_ROLE,
                    Success = true,
                    Token   = token
                };
            }

            return(retVal);
        }
Exemplo n.º 2
0
        public string GenerateToken(JWTDTO data)
        {
            // Define const Key this should be private secret key  stored in some safe place
            string key = "401b09eab3c013d4ca54922bb802bec8fd5318192b0a75f201d8b3727429090fb337591abd3e44453b954555b7a0812e1081c39b740293f765eae731f5a65ed1";

            // Create Security key  using private key above:
            // not that latest version of JWT using Microsoft namespace instead of System
            var securityKey = new Microsoft
                              .IdentityModel.Tokens.SymmetricSecurityKey(Encoding.UTF8.GetBytes(key));

            // Also note that securityKey length should be >256b
            // so you have to make sure that your private key has a proper length
            //
            var credentials = new Microsoft.IdentityModel.Tokens.SigningCredentials
                                  (securityKey, SecurityAlgorithms.HmacSha256Signature);

            //  Finally create a Token
            var header = new JwtHeader(credentials);

            //Some PayLoad that contain information about the  customer
            var payload = new JwtPayload
            {
                { "userId ", data.user_id },
            };

            //
            var secToken = new JwtSecurityToken(header, payload);
            var handler  = new JwtSecurityTokenHandler();

            // Token to String so you can use it in your client
            var tokenString = handler.WriteToken(secToken);

            Console.WriteLine(tokenString);
            Console.WriteLine("Consume Token");


            // And finally when  you received token from client
            // you can  either validate it or try to  read
            var token = handler.ReadJwtToken(tokenString);

            return(tokenString);
        }
Exemplo n.º 3
0
        public IActionResult Index()
        {
            string token = null;
            var    data  = new JWTDTO()
            {
                admin              = false,
                user_id            = 12312,
                company_id         = 123123,
                my_company_id      = 0,
                company_min_credit = 0,
                roleId             = 2,
                licenseNo          = "-",
                mobile             = "-",
                loginName          = "094498456464",
                email              = "-",
                logged_in          = true,
                services           = new List <int>(),
                type = "AGENT"
            };

            token = _jWTHandler.GenerateToken(data);
            return(Ok(token));
        }