zoukankan      html  css  js  c++  java
  • 微信第三方平台处理授权公众号的网页授权接口

    写一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 = "";
    12             string path = AppDomain.CurrentDomain.SetupInformation.ApplicationBase + @"configsWeChat.ThirdpartyPlatform.ComponentVerifyTicket.xml";
    13             using (StreamReader sr = new StreamReader(path, Encoding.UTF8))
    14             {
    15                 string txt = sr.ReadToEnd();
    16                 string[] lines = txt.Split("
    ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
    17                 if (lines.Length == 4)
    18                     ticketValue = lines[3];
    19             }
    20             var accessTokenValue = 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             else
    32                 return Redirect(OAuthCallback + clienturl + "?errcode=0&openid=" + openid);
    33         }

     注意:由第三方平台授权的网页授权,不需要将“公众号设置->功能设置->网页授权域名”特地设置为返回url对应的域名。

  • 相关阅读:
    移植ssh到mini2440
    VMware中Ubuntu安装VMware Tools步骤及问题解决方法
    Linux的网卡由eth0变成了eth1,如何修复
    mini2440移植所有驱动到3.4.2内核详细解说
    单片机的一生(感觉在说大部分人)
    mini2440移植linux-3.4.2内核详细解说
    Ubuntu中恢复桌面的上下默认面板命令
    mini2440移植最新u-boot-2012.04.01详细解说
    MyEclipse CI 2018.8.0 官方最新免费版(破解文件+激活工具+破解教程)
    cocos 获取一个骨骼动画多次显示播放
  • 原文地址:https://www.cnblogs.com/sheng9hhd/p/7281369.html
Copyright © 2011-2022 走看看