首页 > 代码库 > 微信第三方平台处理授权公众号的网页授权接口
微信第三方平台处理授权公众号的网页授权接口
写一OAuthController的MVC控制器,用来接收公众号的网页授权请求
1 public async Task<ActionResult> RedirectCallback(string state, string code = "", string appid = "", string clienturl = "") 2 { 3 if (code.Length == 0) 4 { 5 if (clienturl.IndexOf("?") != -1) 6 return Redirect(OAuthCallback + clienturl + "&errcode=-1&errmsg=" + HttpUtility.UrlEncode("用户禁止授权")); 7 else 8 return Redirect(OAuthCallback + clienturl + "?errcode=-1&errmsg=" + HttpUtility.UrlEncode("用户禁止授权")); 9 }10 11 var ticketValue = http://www.mamicode.com/"";12 string path = AppDomain.CurrentDomain.SetupInformation.ApplicationBase + @"configs\WeChat.ThirdpartyPlatform.ComponentVerifyTicket.xml";13 using (StreamReader sr = new StreamReader(path, Encoding.UTF8))14 {15 string txt = sr.ReadToEnd();16 string[] lines = txt.Split("\r\n".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);17 if (lines.Length == 4)18 ticketValue = http://www.mamicode.com/lines[3];19 }20 var accessTokenValue = http://www.mamicode.com/await Authentication.GetComponentAccessToken(AppId, AppSecret, ticketValue);21 var jsonStr = await Util.HttpGetAsync("https://api.weixin.qq.com/sns/oauth2/component/access_token?appid=" + appid + "&code=" + code + "&grant_type=authorization_code&component_appid=" + AppId + "&component_access_token=" + accessTokenValue);22 //if (jsonStr.Contains("errcode"))23 //{24 // response.Content = new StringContent(jsonStr, Encoding.UTF8, "text/html");25 // return response;26 //}27 JObject result = Newtonsoft.Json.JsonConvert.DeserializeObject<JObject>(jsonStr);28 string openid = result["openid"].Value<string>();29 if (clienturl.IndexOf("?") != -1)30 return Redirect(OAuthCallback + clienturl + "&errcode=0&openid=" + openid);31 else32 return Redirect(OAuthCallback + clienturl + "?errcode=0&openid=" + openid);33 }
注意:由第三方平台授权的网页授权,不需要将“公众号设置->功能设置->网页授权域名”特地设置为返回url对应的域名。
微信第三方平台处理授权公众号的网页授权接口
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。