zoukankan      html  css  js  c++  java
  • 微信用户授权,取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);
    
                        //......
                    }
                }
  • 相关阅读:
    bzoj1101 [POI2007]Zap
    bzoj2648/2716 kdtree
    bzoj2850巧克力王国
    【bzoj1193】[HNOI2006]马步距离
    bzoj 4401 块的计数 思想+模拟+贪心
    【bzoj2751】[HAOI2012]容易题(easy) 数论,简单题
    Ubuntu分区小知识与分区方案
    Ubuntu16.04安装x11VNC远程桌面
    Ubuntu用户权限管理(chown, chmod)
    Ubuntu新建用户组
  • 原文地址:https://www.cnblogs.com/shenyixin/p/4633352.html
Copyright © 2011-2022 走看看