private void RegisterConfig() { if (string.IsNullOrEmpty(WeixinConfig.AppID)) { WeixinConfig.Register(); } }
internal Authorization(WeixinConfig set) { Setting = set; authUrl = "https://open.weixin.qq.com/connect/oauth2/authorize?appid={0}&redirect_uri={1}&response_type=code&scope={2}&state={3}#wechat_redirect"; authTokenUrl = Setting.ApiUrl + "/sns/oauth2/access_token?appid={0}&secret={1}&code={2}&grant_type=authorization_code"; authUserInfoUrl = Setting.ApiUrl + "/sns/userinfo?access_token={0}&openid={1}"; }
public IServiceProvider ConfigureServices(IServiceCollection services) { //初始化数据库 services.AddDbContext <DataContext>(options => options.UseMySql(Configuration.GetConnectionString("MySql"), b => { b.MigrationsAssembly("ProgrammersLiveShow"); })); //注册路由 services.AddMvc(); //注册定时任务 services.AddTimedJob(); //注册Session services.AddSession(c => { c.IdleTimeout = TimeSpan.FromMinutes(30); }); //注入读取配置文件 services.AddOptions(); services.Configure <ApplicationConfig>(Configuration.GetSection("ApplicationConfig")); services.Configure <SendMailConfig>(Configuration.GetSection("SendMailConfig")); //注册微信app信息 WeixinConfig.RegistWeixinAppMsg(); //解决界面显示HTML编码问题 services.AddSingleton(HtmlEncoder.Create(UnicodeRanges.All)); //注册AutoFac services.AddSingleton <IHttpContextAccessor, HttpContextAccessor>(); var containerBuilder = new ContainerBuilder(); containerBuilder.RegisterModule <RegestAutoFac>(); containerBuilder.Populate(services); return(containerBuilder.Build().Resolve <IServiceProvider>()); }
private bool RefreshConfig() { try { WeixinConfig config = new WeixinConfig(); //获取access_token (有效期7200秒,开发者必须在自己的服务全局缓存access_token) string url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=SECRET"; url = url.Replace("APPID", APPID); url = url.Replace("SECRET", SECRET); CommonUtils.HttpUtil util = new CommonUtils.HttpUtil(); Dictionary <string, object> result = util.GetHttpData(url); if (!result.ContainsKey("access_token")) { return(false); } config.AccessToken = result["access_token"].ToString(); //获取jsapi_ticket(有效期7200秒,开发者必须在自己的服务全局缓存jsapi_ticket) url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=ACCESS_TOKEN&type=jsapi"; url = url.Replace("ACCESS_TOKEN", config.AccessToken); result = util.GetHttpData(url); if (!result.ContainsKey("ticket")) { return(false); } config.Ticket = result["ticket"].ToString(); // 设置缓存失效时间7175秒 HttpContext.Current.Cache.Insert("WEIXIN_CONFIG", config, null, DateTime.Now.AddSeconds(7175), System.Web.Caching.Cache.NoSlidingExpiration); return(true); } catch { return(false); } }
public WeiXinController(IOptions <WeixinConfig> options, IMessageHandler messageHandler) { _weixinConfig = options.Value; this._log = LogManager.GetLogger(Startup.repository.Name, this.GetType()); _messageHandler = messageHandler; }