ACCESSasp.net 、C#达成微信公司号OAuth2认证

  以微信公司号作为入口的使用,差不多都会赶上需求运用体系中个人音信和微信用户关联难点、从而进行其余事情处理。近期所做项目应用在店铺号广播宣布录添加自定义字段存入应用连串用户消息表中绝无仅有标识UserGuid实行关联。那么哪些获得微信公司号通信录存款和储蓄的自定义字段、从而完成应用用户音信和微信账号关联?把贯彻方案大概整理一下,如有不足,还望建议:

1:在店铺号报纸公布录内添加用户Guid唯一字段。

ACCESS 1

 

2:创制应用并启用回调格局

 

 

 

 

ACCESS 2

3:以微信为进口的行使注明用户的法子,首先在用户访问应用主页时判断Cookie是或不是有UserGuid消息,假如没有则调用微信OAuth2认证接口。redirectUrl参数是回调页面地址,通过验证后会重临redirectUrl?code=XXXX音讯。注:RedirectUrl回调地址必须是外网能够访问的。

 1 if (Request.Cookies["UserGuid"] == null)
 2             {
 3                 string httpUrl =
 4                     "https://open.weixin.qq.com/connect/oauth2/authorize?appid=XX&redirect_uri=URL&response_type=code&scope=snsapi_base&state=";
 5                 Response.Redirect(httpUrl);
 6             }
 7             else
 8             {
 9                 //TODO 获取userguid之后处理步骤 Request.Cookies["UserGuid"].ToString();
10                  
11             }

4:回调页面处理思路

4.1率先获得通过认证后的Code参数,之后依照卖家号的CorpID和Secret获取AccessToken。

Https请求形式: GET

https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=id&corpsecret=secrect

中标时再次回到:

{

   “access_token”:
“XXX…………”,

   “expires_in”:
7200

}

4.2获得token之后,遵照code和accesstoken字符串参数获取微信用户音信,accesstoken7200秒内卓有作用,code参数五分钟内立见成效且仅能利用三回。

Https请求方式: GET

https://qyapi.weixin.qq.com/cgi-bin/user/getuserinfo?access_token=ACCESS_TOKEN&code=CODE

表达为同盟社成员时回来

{

   “UserId”:”USERID”,

   “DeviceId”:”DEVICEID”

}

呼吁败北时回来

{

   “errcode”:
“40029”,

   “errmsg”: “invalid
code”

}

4.3获取UerID之后,根据access_token和UerID获取用户通信录中音信。自定义字段在extattr属性中。

Https请求格局: GET

https://qyapi.weixin.qq.com/cgi-bin/user/get?access\_token=ACCESS\_TOKEN&userid=UerID

事业有成时回来

{

   “errcode”: 0,

   “errmsg”: “ok”,

   “userid”:
“zhangsan”,

   “name”: “XX”,

   “department”:
[2],

   “position”: “XX”,

   “mobile”: “XX”,

   “gender”: “1”,

   “email”: “XX”,

   “weixinid”: “XX”,
 

   “avatar”:
“XX”,

   “status”: 1,

   “extattr”:
{“attrs”:[{“name”:”用户自定义字段名”,”value”:”自定义字段值”}]}

}

4.4拿走用户自定义字段UserGuid后存入cookie,并再次回到应用主页面。主页面此时验证Cookie,userguid消息。

5:回调页面代码C#

 

 1 protected void Page_Load(object sender, EventArgs e)
 2         {
 3             
 4             try
 5             {
 6                 //1.微信主页判断是否存在Cookie信息,如果存在则不需要再次验证
 7                 //2.如果不存在,则跳转到微信OAuth统一认证页面认证,之后到此页面获取用户信息存入Cookie返回主页
 8                 //3.获取Code,此code只能使用一次。
 9                 string code = Request["code"];
10 
11                 //4.根据企业ID:XX,Secret:XXX,获取token
12                 string accessTokenUrl = "https://qyapi.weixin.qq.com/cgi-bin/gettoken";
13                 string accessTokenResult = HttpGet(accessTokenUrl, "corpid=XX&corpsecret=XXXX");
14                 JavaScriptSerializer js = new JavaScriptSerializer();
15                 token mytoken = js.Deserialize<token>(accessTokenResult);
16                 string tokenValue = mytoken.access_token;
17 
18                 //5.获取UserInfo,包含userId
19                 string userinfoUrl = "https://qyapi.weixin.qq.com/cgi-bin/user/getuserinfo";
20                 string userinfojson = HttpGet(userinfoUrl, "access_token=" + tokenValue + "&code=" + code);
21                 userinfo userinfo1 = js.Deserialize<userinfo>(userinfojson);
22                 string UserId = userinfo1.UserId;
23 
24                 //6.根据userId获取通讯录中用户的详细信息
25                 string userinfodetailURL = "https://qyapi.weixin.qq.com/cgi-bin/user/get";
26                 string userinfodetailResult = HttpGet(userinfodetailURL, "access_token=" + tokenValue + "&userid=" + UserId);
27                 JObject jObject = (JObject)JsonConvert.DeserializeObject(userinfodetailResult);
28                 JObject ja = (JObject)JsonConvert.DeserializeObject(jObject["extattr"].ToString());
29                 JArray jObject1 = (JArray)JsonConvert.DeserializeObject(ja["attrs"].ToString());
30                 //txtReturn2.Text = ja[0]["MainOrganization"].ToString();
31                 
32                 //测试结果
33                 //Response.Write("1:token:" + accessTokenResult + "<br/>2:tokenValue:" + tokenValue + "<br/>3:code:" + code + "<br/>4:userinfojson:" + userinfojson + "<br/>5.UserId:" + UserId + "<br/>6.userinfodetailResult:" + userinfodetailResult + "<br/>7:数组:" + jObject1[0]["value"].ToString());
34 
35                 //7.将Guid存入cookie
36                 string userguid = jObject1[0]["value"].ToString();
37                 Response.Cookies["UserGuid"].Value = userguid;
38                 Response.Cookies["UserGuid"].Expires = DateTime.Now.AddDays(1);
39                 //todo:跳转到微信主页
40                 Response.Redirect("XX", true);
41             }
42             catch (Exception ex)
43             {
44                 //TODO 处理异常
45 
46             }
47         }
48         /// <summary>
49         /// 发送HttpGet请求,返回string格式的json数据
50         /// </summary>
51         /// <param name="Url"></param>
52         /// <param name="postDataStr"></param>
53         /// <returns></returns>
54         public string HttpGet(string Url, string postDataStr)
55         {
56             HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url + (postDataStr == "" ? "" : "?") + postDataStr);
57             request.Method = "GET";
58             request.ContentType = "text/html;charset=UTF-8";
59             HttpWebResponse response = (HttpWebResponse)request.GetResponse();
60             Stream myResponseStream = response.GetResponseStream();
61             StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8"));
62             string retString = myStreamReader.ReadToEnd();
63             myStreamReader.Close();
64             myResponseStream.Close();
65             return retString;
66         }

 

6方面代码涉及到的多少个类,为拍卖回来string类型的Json串,将json格式连串化为类。类的格式需和json保持一致。代码如下:

ACCESS 3ACCESS 4

 1 public class userinfo
 2     {
 3         public string UserId { get; set; }
 4         public string DeviceId { get; set; }
 5     }
 6 
 7 public class token
 8     {
 9 
10         public string access_token { get; set; }
11         public int expires_in { get; set; }
12        
13     }

View Code

 

相关文章