示例#1
0
        public static Token_Extracted_Data ExtractTokenData(string strToken)
        {
            var tk      = new Token_Extracted_Data();
            var epoch   = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
            var handler = new JwtSecurityTokenHandler();

            try
            {
                JwtSecurityToken jwtToken = handler.ReadToken(strToken) as JwtSecurityToken;
                tk.jti      = jwtToken.Claims.First(claim => claim.Type == "jti").Value;
                tk.userid   = Conversions.ToInteger(jwtToken.Claims.First(claim => claim.Type == "userid").Value);
                tk.username = jwtToken.Claims.First(claim => claim.Type == "user").Value;
                tk.role     = jwtToken.Claims.First(claim => claim.Type == "role").Value;
                tk.email    = jwtToken.Claims.First(claim => claim.Type == "email").Value;
                tk.nbf      = Conversions.ToDouble(jwtToken.Claims.First(claim => claim.Type == "nbf").Value);
                tk.iat      = Conversions.ToDouble(jwtToken.Claims.First(claim => claim.Type == "iat").Value);
                tk.exp      = Conversions.ToDouble(jwtToken.Claims.First(claim => claim.Type == "exp").Value);
                tk.iss      = jwtToken.Claims.First(claim => claim.Type == "iss").Value;
                tk.aud      = jwtToken.Claims.First(claim => claim.Type == "aud").Value;
                var timeSpan = TimeSpan.FromSeconds(tk.nbf);
                tk.nbf_date = epoch.Add(timeSpan).ToString("yyyy-MM-dd HH:mm:ss");
                timeSpan    = TimeSpan.FromSeconds(tk.iat);
                tk.iat_date = epoch.Add(timeSpan).ToString("yyyy-MM-dd HH:mm:ss");
                timeSpan    = TimeSpan.FromSeconds(tk.exp);
                tk.exp_date = epoch.Add(timeSpan).ToString("yyyy-MM-dd HH:mm:ss");
            }
            catch (Exception ex)
            {
                WriteActivityLog(ex.Message, 2);
            }

            return(tk);
        }
示例#2
0
        public static bool AddCardexTokens(string uri, string method, int status, string statusMsg, string ip, string token)
        {
            bool bolR;

            try
            {
                Create_db();
                bolR = false;
                var lastaccess = DateTime.UtcNow;
                var tk         = new Token_Extracted_Data();
                tk = ExtractTokenData(token);
                string jti         = tk.jti;
                string userid      = tk.userid.ToString();
                string username    = tk.username;
                string role        = tk.role;
                string email       = tk.email;
                ulong  nbf         = Conversions.ToULong(tk.nbf);
                ulong  iat         = Conversions.ToULong(tk.iat);
                ulong  exp         = Conversions.ToULong(tk.exp);
                string iss         = tk.iss;
                string aud         = tk.aud;
                string nbf_date    = tk.nbf_date;
                string iat_date    = tk.iat_date;
                string exp_date    = tk.exp_date;
                string strSQLQuery = @"INSERT INTO validations
(requestUri, method, status, statusMsg, ipaddr, userid, username, role, email, nbf_date, iat_date, exp_date, iss, aud, nbf, iat, exp, jti, token)
VALUES
(@requestUri, @method, @status, @statusMsg, @ipaddr, @userid, @username, @role, @email, @nbf_date, @iat_date, @exp_date, @iss, @aud, @nbf, @iat, @exp, @jti, @token);
UPDATE users SET lastaccess = @lastaccess, laststatus = @status, lastipaddr = @ipaddr WHERE id = @userid;";
                using (var connection = new System.Data.SQLite.SQLiteConnection(mStrSQLiteConnString))
                {
                    using (var command = new System.Data.SQLite.SQLiteCommand(strSQLQuery, connection))
                    {
                        command.CommandType = CommandType.Text;
                        command.Parameters.Add("@requestUri", DbType.String);
                        command.Parameters["@requestUri"].Value = uri;
                        command.Parameters.Add("@method", DbType.String);
                        command.Parameters["@method"].Value = method;
                        command.Parameters.Add("@status", DbType.Int32);
                        command.Parameters["@status"].Value = status;
                        command.Parameters.Add("@statusMsg", DbType.String);
                        command.Parameters["@statusMsg"].Value = statusMsg;
                        command.Parameters.Add("@ipaddr", DbType.String);
                        command.Parameters["@ipaddr"].Value = ip;
                        command.Parameters.Add("@userid", DbType.String);
                        command.Parameters["@userid"].Value = userid;
                        command.Parameters.Add("@username", DbType.String);
                        command.Parameters["@username"].Value = username;
                        command.Parameters.Add("@role", DbType.String);
                        command.Parameters["@role"].Value = role;
                        command.Parameters.Add("@email", DbType.String);
                        command.Parameters["@email"].Value = email;
                        command.Parameters.Add("@nbf_date", DbType.String);
                        command.Parameters["@nbf_date"].Value = nbf_date;
                        command.Parameters.Add("@iat_date", DbType.String);
                        command.Parameters["@iat_date"].Value = iat_date;
                        command.Parameters.Add("@exp_date", DbType.String);
                        command.Parameters["@exp_date"].Value = exp_date;
                        command.Parameters.Add("@iss", DbType.String);
                        command.Parameters["@iss"].Value = iss;
                        command.Parameters.Add("@aud", DbType.String);
                        command.Parameters["@aud"].Value = aud;
                        command.Parameters.Add("@nbf", DbType.UInt64);
                        command.Parameters["@nbf"].Value = nbf;
                        command.Parameters.Add("@iat", DbType.UInt64);
                        command.Parameters["@iat"].Value = iat;
                        command.Parameters.Add("@exp", DbType.UInt64);
                        command.Parameters["@exp"].Value = exp;
                        command.Parameters.Add("@jti", DbType.String);
                        command.Parameters["@jti"].Value = jti;
                        command.Parameters.Add("@token", DbType.String);
                        command.Parameters["@token"].Value = token;
                        command.Parameters.Add("@lastaccess", DbType.DateTime);
                        command.Parameters["@lastaccess"].Value = lastaccess;
                        connection.Open();
                        command.ExecuteNonQuery();
                        connection.Close();
                    }
                }

                bolR = true;
            }
            catch (Exception ex)
            {
                WriteActivityLog(ex.Message, 2);
                bolR = false;
            }

            return(bolR);
        }