public override string OnComponentVerifyTicketRequest(RequestMessageComponentVerifyTicket requestMessage) { //使用数据库去记录 var _contextOptions = new DbContextOptionsBuilder <WechatOpenCoreDbContext>() .UseMySql("Server=120.79.31.209;Port=3336;Uid=root;Pwd=628VqB2sgJwLgOvngXQ3;Database=hchh;") .Options; using (var db = new WechatOpenCoreDbContext(_contextOptions)) { var model = db.Query <WechatOpenTicket>() .Where(m => m.AppId == requestMessage.AppId) .FirstOrDefault(); if (model == null) { model = new WechatOpenTicket() { AppId = requestMessage.AppId, OpenTicket = requestMessage.ComponentVerifyTicket, CreateTime = requestMessage.CreateTime }; db.AddTo(model); } else { model.OpenTicket = requestMessage.ComponentVerifyTicket; model.CreateTime = requestMessage.CreateTime; } db.SaveChanges(); } //var openTicketPath = hostingEnvironment.MapPath("/App_Data/OpenTicket"); //if (!Directory.Exists(openTicketPath)) //{ // Directory.CreateDirectory(openTicketPath); //} ////RequestDocument.Save(Path.Combine(openTicketPath, string.Format("{0}_Doc.txt", DateTime.Now.Ticks))); ////记录ComponentVerifyTicket(也可以存入数据库或其他可以持久化的地方) //using (FileStream fs = new FileStream(Path.Combine(openTicketPath, string.Format("{0}.txt", RequestMessage.AppId)),FileMode.OpenOrCreate,FileAccess.ReadWrite)) //{ // using (TextWriter tw = new StreamWriter(fs)) // { // tw.Write(requestMessage.ComponentVerifyTicket); // tw.Flush(); // //tw.Close(); // } //} return(base.OnComponentVerifyTicketRequest(requestMessage)); }
/// <summary> /// 获取openTicket /// </summary> /// <param name="hostingEnvironment"></param> /// <param name="componentAppId"></param> /// <param name="_contextOptions"></param> /// <returns></returns> public static string GetOpenTicket(this IHostingEnvironment hostingEnvironment, string componentAppId, DbContextOptions <WechatOpenCoreDbContext> _contextOptions) { using (var db = new WechatOpenCoreDbContext(_contextOptions)) { return(db.Query <WechatOpenTicket>().First().OpenTicket); //var model = db.Query<WechatOpenTicket>() // .Where(m => m.AppId == componentAppId) // .FirstOrDefault(); //if (model == null) throw new Exception($"获取OpenTicket失败,appId为:{componentAppId}"); //return model.OpenTicket; } }