码迷,mamicode.com
首页 > 微信 > 详细

c# 微信开发 《授权页面》

时间:2018-02-07 16:58:31      阅读:302      评论:0      收藏:0      [点我收藏+]

标签:传递参数   处理   help   startup   script   open   null   rect   nec   

//授权页面
public partial class wxProcess : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            string reurl = "";
            //传递参数,获取用户信息后,可跳转到自己定义的页面,想怎么处理就怎么处理
            if (Request.QueryString["reurl"] != null && Request.QueryString["reurl"] != "")
            {
                reurl = Request.QueryString["reurl"].ToString();
            }
            else
            {
                
            }

            string code = "";
            string url = "";
            string appid = ConfigurationManager.AppSettings["WeixinAppid"].TryToString();
            //弹出授权页面(如在不弹出授权页面基础下未获得openid,弹出授权页面,提示用户授权)
            if (Request.QueryString["auth"] != null && Request.QueryString["auth"] != "" && Request.QueryString["auth"] == "1")
            {
                url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" + appid
                                   + "&redirect_uri=" + reurl + "&response_type=code&scope=snsapi_userinfo&state=1#wechat_redirect";
                Response.Redirect(url);
            }
            else
            {
                url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid="
                                + appid + "&redirect_uri=" + reurl + "&response_type=code&scope=snsapi_base&state=1#wechat_redirect";
                //不弹出授权页面
                Response.Redirect(url);
            }
        }
    }
}


///需跳转页面
public partial class Index : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string code = "";
            if (Request.QueryString["code"] != null && Request.QueryString["code"] != "")
            {
                //获取微信回传的code
                code = Request.QueryString["code"].ToString();
                string openid = WeiXinCommom.Get_token(code);  //获取token
                if (!string.IsNullOrWhiteSpace(openid)) {
                    WeiXinService WeiXinService = new WeiXinService();
                    Entity UserInfo= WeixinDA.GetShowUserWeiXinInfo(openid); //获取用户信息
                    LogHelper.WriteFileLog("UserInfo", UserInfo.IsNull.ToString());
                    bool SaveUser = true;
                    if (UserInfo.IsNull) {  //如果不存在用户信息,用户已经关注了公众号,但是未注册到用户数据,重新注册
                        int CareUsCode = 11000;//关注公众号赠送积分的codeid
                        Entity CareUsData = WeixinDA.GetCategoryCodesByCodeID(CareUsCode);
                        int CareUsStatus = CareUsData.GetValue("Status").TryInt32();
                        SaveUser= WeiXinService.SaveUserInfo(openid); //返回fasle,用户未关注公众号
                        if (SaveUser) {
                            if (CareUsStatus == (int)BaseEnum.OnOff.On)//积分规则打开
                            {
                                decimal Integration = CareUsData.GetValue("Value").TryDecimal();
                                WeiXinService.AddOpenIDIntegration(openid, (int)BaseEnum.IntegrationType.CareUs, Integration);//第一次关注公众号赠送的积分
                            }
                        }
                    }
                    if (SaveUser)
                    {
                        string WeiXinInfoString = WeiXinService.GetShowUserWeiXinInfoHadTokenString(openid);
                        ClientScript.RegisterStartupScript(ClientScript.GetType(), "orderList", "<script>UserProfile.GetWeixinCode(‘" + WeiXinInfoString + "‘);</script>");
                    }
                    else {
                        string WeiXinInfoString = WeiXinService.GetShowUserWeiXinInfoHadTokenString(openid);
                        ClientScript.RegisterStartupScript(ClientScript.GetType(), "orderList", "<script>UserProfile.GetWeixinCode(‘‘);</script>");
                    }
                }
            }
        }
    }
//根据appid,secret,code获取微信openid、access token信息
        public static string Get_token(string Code)
        {
            //获取微信回传的openid、access token
            string WeixinData = WeiXinCommom.GetData("https://api.weixin.qq.com/sns/oauth2/access_token?appid=" + appid + "&secret=" + appsecret + "&code=" + Code + "&grant_type=authorization_code");
            string openid = string.IsNullOrWhiteSpace(WeixinData) ? string.Empty : JObject.Parse(WeixinData)["openid"].TryString();
            return openid;
        }

  

访问 HTTP://baidu.com.授权页面?需跳转页面

  

c# 微信开发 《授权页面》

标签:传递参数   处理   help   startup   script   open   null   rect   nec   

原文地址:https://www.cnblogs.com/yyyuguo/p/8426823.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!