之前做过webform的网页微信授权,跟mvc的又不太一样,特此记录下二者实现方式的不同。
webform:
统一访问入口中做如下跳转:
string url = Uri.EscapeDataString("http://" + HttpContext.Current.Request.Url.Host + "/mobile/LuckDraw/drawS.aspx"); Response.Redirect("https://open.weixin.qq.com/connect/oauth2/authorize?appid=" + Maticsoft.WeChat.BLL.Core.Config.GetValueByCache("WeChat_AppId", -1, "AA") + "&redirect_uri=" + url + "&response_type=code&scope=snsapi_base#wechat_redirect");
mvc中:
由于项目场景的入口不统一,所以在OnActionExecuting方法中做微信授权处理
protected override void OnActionExecuting(ActionExecutingContext filterContext) { if (string.IsNullOrEmpty(Request.QueryString["code"])) { RedirectUrl(GetCodeUrl(Request.Url.AbsoluteUri)); } }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
public void RedirectUrl(string url) { this.Response.Clear();//这里是关键,清除在返回前已经设置好的标头信息,这样后面的跳转才不会报错 this.Response.BufferOutput = true;//设置输出缓冲 if (!this.Response.IsRequestBeingRedirected)//在跳转之前做判断,防止重复 { this.Response.Redirect(url, true); return; } }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#region 微信网页授权 /// <summary> /// 对页面是否要用授权 用snsapi_base方式 获取Code Appid是微信应用id /// </summary> /// <param name="Appid"></param> /// <param name="redirect_uri"></param> /// <returns></returns> public string GetCodeUrl(string redirect_uri) { string Appid = Maticsoft.WeChat.BLL.Core.Config.GetValueByCache("WeChat_AppId", -1, "AA"); return string.Format("https://open.weixin.qq.com/connect/oauth2/authorize?appid={0}&redirect_uri={1}&response_type=code&scope=snsapi_base&state=STATE#wechat_redirect", Appid, redirect_uri); } /// <summary> /// 用微信回传的Code换取用户的Openid /// </summary> /// <param name="Code"></param> /// <returns></returns> public string CodeGetOpenid(string Code) { string Appid = Maticsoft.WeChat.BLL.Core.Config.GetValueByCache("WeChat_AppId", -1, "AA"); string Appsecret = Maticsoft.WeChat.BLL.Core.Config.GetValueByCache("WeChat_AppSercet", -1, "AA"); string url = string.Format("https://api.weixin.qq.com/sns/oauth2/access_token?appid={0}&secret={1}&code={2}&grant_type=authorization_code", Appid, Appsecret, Code); string uri = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=" + Maticsoft.WeChat.BLL.Core.Config.GetValueByCache("WeChat_AppId", -1, "AA") + "&secret=" + Maticsoft.WeChat.BLL.Core.Config.GetValueByCache("WeChat_AppSercet", -1, "AA") + "&code=" + Request.QueryString["code"] + "&grant_type=authorization_code"; System.Net.WebClient wc = new System.Net.WebClient(); var serializer = new JavaScriptSerializer(); try { byte[] rs = wc.DownloadData(uri); //Response.Write(System.Text.Encoding.UTF8.GetString(rs)); OAuthToken model = serializer.Deserialize<OAuthToken>(System.Text.Encoding.UTF8.GetString(rs)); return model.openid; } catch (Exception ex) { LogHelp.AddErrorLog("抽奖ERROR: ", ex.StackTrace); return ""; } } #endregion
public class OAuthToken { public string access_token { get; set; } public int expires_in { get; set; } public string refresh_token { get; set; } public string openid { get; set; } public string scope { get; set; } }