Example #1
0
        private static bool InsertAccessToken(string appid, AccessTokenResult accessToken)
        {
            string sqlStr = "insert into [wx_AccessToken]([appid] ,[accessToken] ,[createtime] ) VALUES(@appid,@accessToken,@createtime) ";

            SqlParameter[] parameters = { new SqlParameter("@appid", appid), new SqlParameter("@accessToken", accessToken.access_token), new SqlParameter("@createtime", DateTime.Now.AddSeconds(accessToken.expires_in - 60)) };

            if (SQLServerHepler.ExecuteNonQuery(SQLServerHepler.GetConnectionStringByConfig(), System.Data.CommandType.Text, sqlStr, parameters) > 0)
            {
                return(true);
            }
            else
            {
                return(false);
            }
        }
Example #2
0
        private static bool UpdateAccessToken(string appid, AccessTokenResult accessToken)
        {
            string sqlStr = "update wx_AccessToken set accessToken=@accessToken,createtime=@createtime where appid=@appid";

            SqlParameter[] parameters = { new SqlParameter("@accessToken", accessToken.access_token), new SqlParameter("@createtime", DateTime.Now.AddSeconds(accessToken.expires_in - 60000)), new SqlParameter("@appid", appid) };

            if (SQLServerHepler.ExecuteNonQuery(SQLServerHepler.GetConnectionStringByConfig(), System.Data.CommandType.Text, sqlStr, parameters) > 0)
            {
                return(true);
            }
            else
            {
                return(false);
            }
        }
Example #3
0
        public static string GetTokenForSqlServer(string appid, string secret)
        {
            string sqlStr = "select top 1 accessToken,createtime from wx_AccessToken where appid=appid";

            SqlParameter[] parameters = { new SqlParameter("appid", appid) };


            string   access_token   = string.Empty;
            DateTime?expireDateTime = null;

            using (SqlDataReader reader = SQLServerHepler.ExecuteReader(SQLServerHepler.GetConnectionStringByConfig(), System.Data.CommandType.Text, sqlStr, parameters)){
                while (reader.Read())
                {
                    access_token   = reader.GetString(0);
                    expireDateTime = reader.GetDateTime(1);
                }
            }
            //数据库中不存在accessToken
            if (string.IsNullOrEmpty(access_token))
            {
                AccessTokenResult result = GetToken(appid, secret);

                InsertAccessToken(appid, result);

                return(result.access_token);
            }
            else if (expireDateTime < DateTime.Now) //accessToken已过期

            {
                AccessTokenResult result = GetToken(appid, secret);

                UpdateAccessToken(appid, result);

                return(result.access_token);
            }
            else
            {
                return(access_token);
            }
        }