Enterprise number obtains user information and user information
One of the most basic functions of business operations is to obtain the identity of a visitor. The traditional acquisition method is to provide a logon page for a visitor to log on.
When a user accesses a page in the enterprise number, he or she can obtain the account information of the user based on relevant APIs to match the user information stored on the Business Server.
Directory
1. Introduction
2. Sample Code
1. Introduction 1.1
To put it bluntly, Enterprise web development is mobile web development. The special point is how to obtain user identity information.
To obtain user information, follow these steps:
When you access a service page, you can obtain the user information through the OAuth interface → get userId Based on code → get information based on userId.
1.2 detailed process ① get code
API: Http://qydev.weixin.qq.com/wiki/index.php? Title = OAuth % E9 % AA % 8C % E8 % AF % 81% E6 % 8E % A5 % E5 % 8F % A3 #. E4.BC. 81. E4.B8.9A. E8.8E. B7.E5.8F. 96 code
Description: After the webpage passes OAuth2.0 verification, it is redirected to the original webpage and added code information after the url.
For example: Http://akmsg.com/a.html => OAhth2.0 => http://akmsg.com/a.html? Code = CODE & state = STATE
② Obtain userId Based on code
API: Http://qydev.weixin.qq.com/wiki/index.php? Title = OAuth % E9 % AA % 8C % E8 % AF % 81% E6 % 8E % A5 % E5 % 8F % A3 #. e6.A0. b9.E6.8D. AEcode. e8.8E. b7.E5.8F. 96. e6.88.90.E5.91.98. e4.BF. a1.E6.81. AF
Description: After you call this interface, you will get the userId. Note: userId is the encrypted account.
③ Obtain information based on userId
API: Http://qydev.weixin.qq.com/wiki/index.php? Title = % E7 % AE % A1 % E7 % 90% 86% E6 % 88% E5 % 90% 98 #. E8.8E. B7.E5.8F. 96. E6.88.90.E5.91.98
Description: After calling this interface, you will obtain the specific information of the Visitor registering with the enterprise number, such as name, number, mobile phone number, email address, and position.
④ Obtain logical User Information Based on Information
Description: The information obtained from the previous step can be used to match the business logic to obtain the user information in the business layer.
1.3 Flowchart
2. Sample Code 2.1 code (C #)
Logic: Asp.net determines the request sent by the client. If the request meets the rules on the enterprise number page, the enterprise number user identity authentication will be performed.
This function identifies three access requests:
1. The first access without code: OAuth Verification
2. There is code, no cookie: Get the information corresponding to the code
3. code and cookie: Verify the cookie
/// <Summary> /// Verify access /// </summary> public static void Auth (HttpContext webContext) {string requestURL = webContext. request. url. absoluteUri; try {// there are three types of user access pages: // 1. the first access, no code // 2. code, no cookie; // 3. code and cookie // 1. first visit, no code, no cookie: Jump to Oauth2.0 authentication if (string. isNullOrEmpty (webContext. request ["code"]) {string url = 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 ", CORPID, webContext. server. urlEncode (requestURL); webContext. response. redirect (url, false);} else if (! String. isNullOrEmpty (webContext. request ["code"]) & string. isNullOrEmpty (CookieHelper. getCookie ("WXToken") {// 2. code, no cookie: obtain userID string code = webContext according to code. request ["code"]; string userId = ""; string userInfo = ""; # region 1) Obtain userId string url = string Based on the code. format ("https://qyapi.weixin.qq.com/cgi-bin/user/getuserinfo? Access_token = {0} & code = {1} ", GetAccessToken (), code); string responseText = HttpHelper. Instance. get (url);/* API: http://qydev.weixin.qq.com/wiki/index.php? Title = OAuth % E9 % AA % 8C % E8 % AF % 81% E6 % 8E % A5 % E5 % 8F % A3 #. e6.A0. b9.E6.8D. AEcode. e8.8E. b7.E5.8F. 96. e6.88.90.E5.91.98. e4.BF. a1.E6.81. json return example of AF: {"UserId": "USERID", "DeviceId": "DEVICEID"} returned when the enterprise ID is not followed: {"OpenId": "OPENID ", "DeviceId": "DEVICEID"} returned Json sample: {"errcode": "40029", "errmsg": "invalid code"} */WeChatUserCodeEntity codeEn = JsonHelper. getEntity <WeChatUserCodeEntity> (responseText); if (codeE N. errcode> 0) {throw new Exception (codeEn. errmsg);} else if (string. IsNullOrEmpty (codeEn. UserId) {throw new Exception ("please pay attention to the enterprise number first! ");} UserId = codeEn. UserId; # endregion # region 2) get user information url = string. Format (" https://qyapi.weixin.qq.com/cgi-bin/user/get? Access_token = {0} & userid = {1} ", GetAccessToken (), userId); responseText = HttpHelper. Instance. get (url);/* API: http://qydev.weixin.qq.com/wiki/index.php? Title = % E7 % AE % A1 % E7 % 90% E6 % 86% E5 % 88% 98 #. e8.8E. b7.E5.8F. 96. e6.88.90.E5.91.98: {"errcode": 0, "errmsg": "OK", "userid": "zhangsan", "name": "Li Si ", "department": [1, 2], "position": "back-end engineer", "mobile": "15913215421", "gender": "1", "email ": "zhangsan@gzdev.com", "weixinid": "lisifordev", "avatar": "http://wx.qlogo.cn/mmopen/ajNVdqHZLLA3WJ6DSZUfiakYe37PKnQhBIeOQBO4czqrnZDS79FH5Wm5m4X69TB IcnHFlhiafvDwklOpZeXYQQ2icg/0 "," status ": 1," extattr ": {" attrs ": [{" name ":" hobbies "," value ":" Tourism "}, {"name": "card number", "value": "1234567234"}]} error Json return example: {"errcode": "40029", "errmsg ": "invalid code"} */WeChatUserInfoEntity userInfoEn = JsonHelper. getEntity <WeChatUserInfoEntity> (responseText); if (userInfoEn. errcode> 0) {throw new Exception (userInfoEn. errmsg);} userInfo = responseText; # endregion // 3. Pass userInfo to CookieHelper. SetCookie ("WXToken", userInfo,-1);} else if (! String. IsNullOrEmpty (webContext. Request ["code"]) &! String. isNullOrEmpty (CookieHelper. getCookie ("WXToken") {# region 3. code, cookie: Check cookie // TODO: AES encryption can be used to store the cookie. In this example, the decryption is performed. // CookieHelper. setCookie ("WXToken", "",-1); # endregion} else {throw new Exception ("unauthorized access! ") ;}} Catch (Exception ex) {throw ex ;}}
2.2 runtime diagram 1) when the user has followed the access
2) When the user is not accessing the enterprise address book
2.3 Dmeo download (C #)
: Http://files.cnblogs.com/files/polk6/Wechat.QYH.zip
====================================== Series of articles ==============================================
This article: Enterprise 1.3 obtains user information
Development Article navigation