예제 #1
0
        /// <summary>
        /// 获取微信授权信息
        /// </summary>
        /// <param name="code">微信授权码</param>
        /// <returns></returns>
        internal static async Task <WxToken> GetTokenAsync(string code = null)
        {
            string url = $"{WxConstants.CGI_BIN_TOKEN_URL}?grant_type=client_credential&appid={WxClient.WxOption.AppId}&secret={WxClient.WxOption.AppSecret}";

            if (!string.IsNullOrEmpty(code))
            {
                url = $"{WxConstants.SNSAPI_OAUTH2_URL}?appid={WxClient.WxOption.AppId}&secret={WxClient.WxOption.AppSecret}&code={code}&grant_type=authorization_code";

                return(await WxClient.GetAsync <WxToken>(url));
            }

            if (!WxClient.MemoryCache.TryGetValue(WxConstants.ACCESS_TOKEN_KEY, out WxToken token))
            {
                token = await WxClient.GetAsync <WxToken>(url);

                WxClient.MemoryCache.Set(WxConstants.ACCESS_TOKEN_KEY, token, TimeSpan.FromSeconds(token.ExpiresIn));
            }

            return(token);
        }
예제 #2
0
파일: TicketFactory.cs 프로젝트: sud423/Csp
        /// <summary>
        /// 获取微信授权信息
        /// </summary>
        /// <param name="client">httpclient</param>
        /// <param name="cache">memorycache</param>
        /// <param name="logger">日志记录器,记录返回信息</param>
        /// <param name="option">微信配置信息</param>
        /// <param name="code">微信授权码</param>
        /// <returns></returns>
        internal static async Task <string> GetTicketAsync()
        {
            var token = await AccessTokenFactory.GetTokenAsync();

            if (token == null)
            {
                return(string.Empty);
            }

            string url = $"{WxConstants.JSAPI_TICKET_URL}?access_token={token.AccessToken}&type=jsapi";

            if (!WxClient.MemoryCache.TryGetValue(WxConstants.JS_API_TICKET_KEY, out WxTicket ticket))
            {
                ticket = await WxClient.GetAsync <WxTicket>(url);

                WxClient.MemoryCache.Set("wechat_jsapi_ticket", ticket.Ticket, TimeSpan.FromSeconds(ticket.ExpiresIn));
            }

            return(ticket.Ticket);
        }