微信企業號的用戶是需要驗證的,因此能關注企業號的用戶其實就是已經通過驗證的用戶,但企業應用中打開一個網頁,在這個網頁中如何根據微信用戶的信息創建web應用中最長使用的session呢?微信用戶如何和web的session關聯起來呢?
例如:一個應用,根據不同的人員,顯示不同的內容,各個網頁之間需要session來傳遞一些信息,在微信企業號中如何處理呢?
這個問題需要涉及的接口是OAuth2驗證接口,需要配置可信域名,初始化session。
一下以一個帶有URL的菜單為例進行說明
**1根據OAuth2驗證接口改寫URL**
例如需要跳轉到http://abc.def.com.cn:8082/index.aspx頁面,則根據[OAuth驗證接口](http://qydev.weixin.qq.com/wiki/index.php?title=OAuth%E9%AA%8C%E8%AF%81%E6%8E%A5%E5%8F%A3)說明,菜單的URL應該是
https://open.weixin.qq.com/connect/oauth2/authorize?appid=CORPID&redirect_uri=http://abc.def.com.cn:8082/index.aspx&response_type=code&scope=SCOPE&state=STATE#wechat_redirect
其中appid為corpid,請改為自己實際的參數值,response_type固定為code,scope固定為snsapi_base,#wechat_redirect不用改,直接加上就可以了。redirect_uri是需要跳轉的URL,但需要urlencode處理,http://abc.def.com.cn:8082/index.aspx經過urlencode處理后為:http%3a%2f%2fabc.def.com.cn%3a8082%2findex.aspx,state不是必選的,可以填寫a-zA-Z0-9的參數值組成的數據
因此菜單的URL應該為
https://open.weixin.qq.com/connect/oauth2/authorize?appid=myappid&redirect_uri=http%3a%2f%2fabc.def.com.cn%3a8082%2findex.aspx&response_type=code&scope=SCOPE&state=a#wechat_redirect
appid是myappid
response_type固定為code,scope固定為snsapi_base,state是a,?
redirect_uri是http://abc.def.com.cn:8082/index.aspx,
經過urlencode后是http%3a%2f%2fabc.def.com.cn%3a8082%2findex.aspx
這樣配置菜單的連接后,在微信中打開時,http://abc.def.com.cn:8082/index.aspx就會多一個查詢字符串code,根據code就可以獲取到打開這個微信用戶的信息,然后就可以初始化web應用的session了。
**2需要配置可信域名**
再按照以第一步處理后,在微信端打開連接,會出現一個錯誤,這個是因為沒有配置可信域名。
**redirect uri 參數錯誤**
需要在微信管理端配置可信域名,如果redirect_uri有端口號,那'可信域名'也必須加上端口號OAuth2驗證接口
例如根據需要跳轉的http://abc.def.com.cn:8082/index.aspx,配置可信域名如下,注意不要http

**3初始化session**
在進行了以上處理后,用戶在點擊菜單時,跳轉的連接就會變為http://abc.def.com.cn:8082/index.aspx?code=3c452771ddfc0e75097d0509e0e555
也就是說多了一個查詢字符串code,根據code就可以取到這個微信用戶的UserId信息。具體參考[根據code獲取成員信息](http://qydev.weixin.qq.com/wiki/index.php?title=OAuth%E9%AA%8C%E8%AF%81%E6%8E%A5%E5%8F%A3)
核心代碼:
~~~
/// <summary>
/// 根據code獲取成員信息
/// </summary>
/// <param name="userid"></param>
/// <returns></returns>
public static string GetUserInfo(string CODE)
{
// https://qyapi.weixin.qq.com/cgi-bin/user/getuserinfo?access_token=ACCESS_TOKEN&code=CODE
string urlFormat = "https://qyapi.weixin.qq.com/cgi-bin/user/getuserinfo?access_token={0}&code={1}";
var url = string.Format(urlFormat, BLLAccessToken.GetAccessToken(), CODE);
string UserId = string.Empty;
WebUtils wut = new WebUtils();
//數據不用加密發送
LogInfo.Info("根據code獲取成員信息: " + CODE);
string sendResult = wut.DoGet(url);
OAuthResult tempAccessTokenjson = Tools.JsonStringToObj<OAuthResult>(sendResult);
if (tempAccessTokenjson.HasError())
{
LogInfo.Error("根據code獲取成員信息返回錯誤: " + Tools.ToJsonString<OAuthResult>(tempAccessTokenjson));
}
else
{
UserId = tempAccessTokenjson.UserId;
}
return UserId;
}
~~~
index.aspx網頁后端代碼:
~~~
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
string code = Request.QueryString["code"].ToLower().Trim();
if (!string.IsNullOrEmpty(code))
{
if (HttpContext.Current.Session["usersession"] != null) //session信息已經存在,直接返回
{
new AppException("usersession已經存在不需要在處理");
return;
}
string username= BLLUser.GetUserInfo(code);
if (!string.IsNullOrEmpty(username))
{
initSession(username);
new AppException("初始化initSession,code=" + code + ",username=" + username);
}
else
{
new AppException("收到信息異常username為空");
}
}
else {
new AppException("收到信息異常code為空");
}
}
}
~~~