微信授权


using
KDMall.Common; using System; using System.Collections.Generic; using System.Configuration; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using Senparc.Weixin; using Senparc.Weixin.MP.AdvancedAPIs; using KDMall.BLL; using KDMall.Mall.Admin.RCCQPark.bll; using KDMall.ORM; using System.IO; namespace KDMall.Mall.Admin.RCCQPark_APP.mobile.view { public partial class reserve : System.Web.UI.Page { public static string COOKIE_HP_OPENID = "COOKIE_HP_OPENID"; protected void Page_Load(object sender, EventArgs e) { if (string.IsNullOrWhiteSpace(Utils.GetCookie(COOKIE_HP_OPENID))) { BaseCallbackOauth(Request.Url.AbsoluteUri, "snsapi_base"); } else { //t_rccq_users users = HttpContext.Current.Session[key.SESSION_ADMIN_INFO] as t_rccq_users; //if (users != null) //{ // DapperHelper.UpdateSql(DapperConnection.kd_user, "update t_rccq_users set open_id=@openid where user_name=@name", new { openid = Utils.GetCookie(COOKIE_HP_OPENID), name = users.user_name }); //} ////将用户的授权回来的openid保存在cook中 //Utils.WriteCookie(COOKIE_HP_OPENID, Utils.GetCookie(COOKIE_HP_OPENID),30); } } /// <summary> /// 用户授权获取union_openId /// </summary> /// <param name="to_wx_url">页面url地址绝对路劲,跳转到微信需要的url地址。</param> /// <param name="page_name">页面地址:微信跳转回来后,拿到openid后,内部跳转的地址</param> /// <param name="oauth_type">授权方式,snsapi_base:静默授权,不需要用户同意,直接获取openID;snsapi_userinfo:需要用户手动同意。</param> public void BaseCallbackOauth(string to_wx_url, string oauth_type = "snsapi_base") { var appid = ConfigurationManager.AppSettings["KDMallAppId"]; var appsecret = ConfigurationManager.AppSettings["KDMallsecret"]; string code = Request.QueryString["code"]; if (string.IsNullOrEmpty(code)) { Response.Redirect(string.Format("https://open.weixin.qq.com/connect/oauth2/authorize?appid={0}&redirect_uri={1}&response_type=code&scope={2}&state=ceiling#wechat_redirect", appid, HttpUtility.UrlEncode(to_wx_url), oauth_type)); return; } //通过,用code换取access_token try { var result = OAuth.GetAccessToken(appid, appsecret, code); if (result.errcode != ReturnCode.请求成功) return; t_rccq_users users = HttpContext.Current.Session[key.SESSION_ADMIN_INFO] as t_rccq_users; if (users != null) { string sql = string.Format("update t_rccq_users set open_id='{0}' where user_name='{1}'", result.openid, users.user_name); DapperHelper.UpdateSql(DapperConnection.kd_user, sql,null); } //将用户的授权回来的openid保存在cook中 Utils.WriteCookie(COOKIE_HP_OPENID, result.openid, 30); } catch (Senparc.Weixin.Exceptions.ErrorJsonResultException ex) { WriteLog("1", "授权失败--" + DateTime.Now, ex.Message ); } } public static void WriteLog(string type, string className, string content) { //在网站根目录下创建日志目录 string path = HttpContext.Current.Server.MapPath("~/app_Log"); if (!Directory.Exists(path))//如果日志目录不存在就创建 { Directory.CreateDirectory(path); } string time = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff");//获取当前系统时间 string filename = path + "/" + DateTime.Now.ToString("yyyy-MM-dd") + ".log";//用日期对日志文件命名 //创建或打开日志文件,向日志文件末尾追加记录 StreamWriter mySw = File.AppendText(filename); //向日志文件写入内容 string write_content = time + " " + type + " " + className + ": " + content; mySw.WriteLine(write_content); //关闭日志文件 mySw.Close(); } } }

添加两个引用

Senparc.Weixin
Senparc.Weixin.MP