.net之微信企業號開發(二) 企業號人員身份認證與開發


前言

這里完全可以鏈接一個登錄頁面,讓用戶輸入用戶名密碼進行登錄的。。。2333

但是,這樣所就完全失去了微信企業號的意義,本來進入微信企業號的時候,就已經對人員身份進行認證了,你這里再讓別人登錄,不是顯得多余么?

於是,需要考慮的是,如何獲取微信企業號中用戶的身份,以及將用戶身份與自有系統進行關聯。

 

一、建立企業應用並配置可信域名

     在微信的管理界面里面,建立一個企業應用。建立的過程很簡單,但是這里需要注意的是,建立完以后,一定要配置可信域名!!!!並且如果你不是使用的標准端口,一定也要把端口配置進去,比如你的網址是m.xxx.com,端口是10000,那么這個可信域名就一定要配置成m.xxx.com:10000。這點一定要注意,否則會走很多彎路!!!(PS現在微信的文檔現在把端口號這一條加上了,當時我看文檔時沒有這個端口號的說明。。。。。。)

二、微信認證接口

     1.獲取企業code。

    微信企業號的認證入口為一個公用地址,采用的是url跳轉的方式進行傳參。

    比如,我的鏈接地址為 m.xxx.com:10000/WeiXin/Auth

        那么,在Auth里面就進行這樣的處理

public ActionResult Auth(string id="") {
  string url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid={0}&redirect_uri={1}&response_type=code&scope=snsapi_base&state=STATE#wechat_redirect";
  string redirecturl = "http://m.xxx.com:10000/WeiXin/GetUser/" + id;
  redirecturl = Server.UrlEncode(redirecturl);
  url = String.Format(url, m_Corpid, redirecturl); 
  Response.Redirect(url);
  return View();
}

   這里有兩個參數,一個是appid,這個是企業號的Corp_id,這個在企業號左邊菜單的“設置”中可以查詢。另外一個redirect_uri,指微信認證成功后跳轉的地址,這里我的地址是 "http://m.xxx.com:10000/WeiXin/GetUser/"。

  微信認證成功后,會自動跳轉到“http://m.xxx.com:10000/WeiXin/GetUser/"這里,並且會帶一個參數code,這個code是獲取用戶id的重要參數。

public ActionResult GetUser(string id="") {
            string code = Careysoft.Basic.Public.BConvert.ToString(Request.QueryString["code"]);
       //........
}

 

  2.獲取access_token

     微信的文檔在這里坑爹了,文檔里面並沒有這一步,而是直接跳過獲取用戶id。這也讓我當時看微信文檔覺得莫名其妙,這里一定要注意一下。

     獲取access_token的方式是http get請求的方式,返回一個json字符串,解析獲得access_token。順便說一下,我這里對json的解析采用了Newtonsoft.Json.dll這個類庫,很不錯,直接把json轉化成需要的類。代碼如下:

       string url = string.Format("https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid={0}&corpsecret={1}", m_Corpid, m_CorpSecret);
            HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
            request.Method = "GET";
            using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
            {
                StreamReader reader = new StreamReader(response.GetResponseStream());
                string strResponse = reader.ReadToEnd(); //獲取微信認證字符
                WeiXinTokenModel tokenModel = JsonConvert.DeserializeObject<WeiXinTokenModel>(strResponse);
                token = tokenModel.access_token;
            }

    這里的 corpid 和 corpsecret都可以企業號管理平台的:”設置“->"權限管理"中進行查詢。

 

    3.根據code和access_token獲取userid

    還是通過http get方式獲取,這里說一下實效性,code5分鍾內有效,只能使用一次,而access_token有效性為7200秒,沒什么好說的,直接貼碼:

       url = String.Format("https://qyapi.weixin.qq.com/cgi-bin/user/getuserinfo?access_token={0}&code={1}", access_token, code);
            request = WebRequest.Create(url) as HttpWebRequest;
            request.Method = "GET";
            using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
            {
                StreamReader reader = new StreamReader(response.GetResponseStream());
                string strResponse = reader.ReadToEnd(); //獲取微信認證字符
                WeiXinUserIdModel userModel = JsonConvert.DeserializeObject<WeiXinUserIdModel>(strResponse);
                userid = userModel.UserId;
            }

 

    4.根據用戶userid和access_token獲取用戶資料信息

    方式不變,直接貼碼(這里我只需要了用戶的手機號碼和微信企業號用戶名):

       url = String.Format("https://qyapi.weixin.qq.com/cgi-bin/user/get?access_token={0}&userid={1}", token, userid);
            request = WebRequest.Create(url) as HttpWebRequest;
            request.Method = "GET";
            using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
            {
                StreamReader reader = new StreamReader(response.GetResponseStream());
                string strResponse = reader.ReadToEnd(); //獲取微信認證字符
                mobile = strResponse;
                WeiXinUserInfoModel userInfoModel = JsonConvert.DeserializeObject<WeiXinUserInfoModel>(strResponse);
                mobile = userInfoModel.mobile;
                xm = userInfoModel.name;
            }

 

     5.根據用戶名userid和access_token,獲取用戶的open_id,或者open_id和app_id

     獲取的方式同上,但這里需要說明一下,open_id,以及open_id和appid是怎么一回事。用戶open_id,主要用於發紅包和轉賬支付時使用,一個用戶在企業號中有個以主open_id,他對應於企業的Corp_id;同時用該用戶還存在多個對應的open_id和app_id,原因是應為企業號可以創建多個應用,一個用戶加入一個應用,就存在一對app_id和open_id。這里發紅包的時候必須通過應用的app_id和open_id發送!!!這里注意,如果你使用企業號的Corp_id和open_id發送的話,可以發送成功,但是用戶沒有地方認領紅包,也就無法完成紅包功能。當然,如果使用企業轉賬的話,用哪一組open_id都可以的。

     與前面的獲取方式有所區別的是,這次獲取open_id采用的是post的方式,需要將一段json格式的字符串post過去,然后獲取返回值,貼代碼:

     獲取Corp_id對應的open_id:

       url = String.Format("https://qyapi.weixin.qq.com/cgi-bin/user/convert_to_openid?access_token={0}", token);
            request = WebRequest.Create(url) as HttpWebRequest;
            request.Method = "POST";
            request.ContentType = "application/json;charset=utf-8";
            string data = "{\"userid\":\"" + userid + "\"}";//這里注意
            byte[] byteData = UTF8Encoding.UTF8.GetBytes(data.ToString());
            request.ContentLength = byteData.Length;
            using (Stream postStream = request.GetRequestStream())
            {
                postStream.Write(byteData, 0, byteData.Length);
            }
            using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
            {
                StreamReader reader = new StreamReader(response.GetResponseStream());
                string strResponse = reader.ReadToEnd(); //獲取微信認證字符
                //mobile = strResponse;
                WeiXinOpenIdModel openIdModel = JsonConvert.DeserializeObject<WeiXinOpenIdModel>(strResponse);
                openid = openIdModel.openid;
            }

      獲取應用中的app_id和open_id(以后的紅包和轉賬功能會再繼續講解):

         url = String.Format("https://qyapi.weixin.qq.com/cgi-bin/user/convert_to_openid?access_token={0}", token);
         request = WebRequest.Create(url) as HttpWebRequest;
                request.Method = "POST";
                request.ContentType = "application/json;charset=utf-8";
                data = "{\"userid\":\"" + userid + "\",\"agentid\":" + agentid + "}"; //建立一個企業應用后,會有一個應用ID,點開應用就可以看到agentid
                byteData = UTF8Encoding.UTF8.GetBytes(data.ToString());
                request.ContentLength = byteData.Length;
                using (Stream postStream = request.GetRequestStream())
                {
                    postStream.Write(byteData, 0, byteData.Length);
                }
                using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
                {
                    StreamReader reader = new StreamReader(response.GetResponseStream());
                    string strResponse = reader.ReadToEnd(); //獲取微信認證字符
                    WeiXinOpenIdModel openIdModel = JsonConvert.DeserializeObject<WeiXinOpenIdModel>(strResponse);
                 appid = openIdModel.appid;
                 openid=openIdModel.openid;
          }

        獲取了微信用戶這些信息,基本可以滿足你對用戶認證以及微信支付的應用啦!在下一節,我准備寫一下微信回調模式的消息接口。

        下一節:.net之微信企業號開發(三) 回調模式的接口開發

          


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM