• 微信用户授权,取openid


    var appid = "wxb5f2540cff5c72be";
                var secret = "3de016d0c294b82a5c74ce3fc4865271";
    
                var openid = Response.Cookies["openid"];
                if (openid!=null&&openid.Value!=null&&!string.IsNullOrEmpty(openid.Value.ToString()))
                {
                    //........
                }
                else
                {
                    //取用户openid
                    var code = Request.QueryString["Code"];
                    if (string.IsNullOrEmpty(code))
                    {
                        var url = string.Format("https://open.weixin.qq.com/connect/oauth2/authorize?appid={0}&redirect_uri=http%3a%2f%2fbsbw2011.gicp.net%2flogin.aspx&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect", appid);
                        Response.Redirect(url);
                    }
                    else
                    {
                        var client = new System.Net.WebClient();
                        client.Encoding = System.Text.Encoding.UTF8;
    
                        var url = string.Format("https://api.weixin.qq.com/sns/oauth2/access_token?appid={0}&secret={1}&code={2}&grant_type=authorization_code", appid, secret, code);
                        var data = client.DownloadString(url);
    
                        var serializer = new JavaScriptSerializer();
                        var obj = serializer.Deserialize<Dictionary<string, string>>(data);
                        string accessToken;
                        if (!obj.TryGetValue("access_token", out accessToken))
                            return;
    
                        var opentid = obj["openid"];
                        HttpCookie openidCookie = new HttpCookie("openid");
                        openidCookie.Value = opentid;
                        openidCookie.Expires = DateTime.Now.AddYears(1);
                        this.Response.Cookies.Add(openidCookie);
    
                        //......
                    }
                }
  • 相关阅读:
    Python面向对象编程
    Python模块
    Python函数式编程(把函数作为参数传入)
    Python函数高级特性
    Python函数基础
    连续数字或英文字符文本强制换行
    flex布局文本过长不显示省略号
    在div中放一个相同大小的svg,实际显示的位置svg偏下
    设置git push默认branch
    c# using
  • 原文地址:https://www.cnblogs.com/shenyixin/p/4633352.html
Copyright © 2020-2023  润新知