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; }

    }

 

转载于:https://www.cnblogs.com/SmilePastaLi/p/6878244.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值