• asp.net mvc 页面微信网页授权


    之前做过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));
                }
    }
    public void RedirectUrl(string url)
            {
                this.Response.Clear();//这里是关键,清除在返回前已经设置好的标头信息,这样后面的跳转才不会报错
                this.Response.BufferOutput = true;//设置输出缓冲
                if (!this.Response.IsRequestBeingRedirected)//在跳转之前做判断,防止重复
                {
                    this.Response.Redirect(url, true);
                    return;
                }
            }
    View Code
     #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
    View Code
    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; }
    
        }
  • 相关阅读:
    (16)JavaScript的流程控制(js的循环)
    (15)javaScript入门
    (14)定位布局(子级在父级中自由定位 父级在页面中自由定位)
    (0-1)CSS 标签语法的属性
    ACM/ICPC 之 双向链表_构造列表-模拟祖玛 (TSH OJ-Zuma(祖玛))
    手记-数学分析(高等数学)中有关算法效率的公式列举(O,Θ,Ω)
    2014北大研究生推免机试(校内)-复杂的整数划分(DP进阶)
    整数划分问题-解法汇总(暂有DP-递归)
    2014北大研究生推免机试(校内)-垃圾炸弹(基础枚举)
    ACM/ICPC 之 BFS-广搜进阶-八数码(经典)(POJ1077+HDU1043)
  • 原文地址:https://www.cnblogs.com/SmilePastaLi/p/6878244.html
Copyright © 2020-2023  润新知