zoukankan      html  css  js  c++  java
  • 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; }
    
        }
  • 相关阅读:
    angular div contenteditable 属性,实现数据双向绑定
    node最简单的本地服务搭建
    picker-view、微信小程序自定义时间选择器(非官方)
    微信小程序wx.switchTab跳转到tab页面后onLoad里面的方法不执行
    小程序拨号功能,小程序点击按钮实现打电话功能
    css换行后缩进,css缩进技巧
    小程序循环列表,点击展开收起/关闭效果
    最新前端面试题-前端必备技能-前端技术汇总
    mapreduce处理天气数据
    基于Canal的数据感知服务平台
  • 原文地址:https://www.cnblogs.com/SmilePastaLi/p/6878244.html
Copyright © 2011-2022 走看看