一、在pageload方法中获取code
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
var code = GetCode(); private string GetCode() { return HttpContext.Current.Request["code"]; }
二、如果code为空,需要重定向到微信获取code
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
//当前页面对应的外网地址 string redirect_url = "http://6018.ynedut.cn/PpWordOrder/default.aspx"; redirect_url = HttpUtility.UrlEncode(redirect_url); var url = WeixinOauth2.GetCodeUrl(redirect_url); HttpContext.Current.Response.Redirect(url); return; } #region ****************获取codeurl**************** /// <summary> /// 获取codeurl /// </summary> /// <param name="redirectUri"></param> /// <returns></returns> public static string GetCodeUrl(string redirectUri) { return string.Format( "https://open.weixin.qq.com/connect/oauth2/authorize?appid={0}&redirect_uri={1}&response_type=code&scope=snsapi_userinfo&agentid={3}&state={2}#wechat_redirect", PubConstant.AppId, redirectUri, new Random().Next(1000, 200000).ToString(), PubConstant.AgentId); } #endregion
三、根据code获取用户信息
注:code的有效时间为10分钟,并且只能使用一次,所以这里建议在load方法里直接获取用户信息,并写入cookie中
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
/// <summary> /// 获取用户信息 /// </summary> /// <param name="code"></param> /// <returns></returns> public static UserInfo GetUserInfo(string code) { var token = GetToken(); var url = String.Format("https://qyapi.weixin.qq.com/cgi-bin/user/getuserinfo?access_token={0}&code={1}", token, code); string strResponse = HttpHelper.HttpGet(url); return JsonConvert.DeserializeObject<UserInfo>(strResponse); } public class UserInfo { public string UserId { get; set; } public string DeviceId { get; set; } [JsonProperty("user_ticket")] public string User_ticket { get; set; } [JsonProperty("expires_in")] public string Expires_in { get; set; } }
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
/// <summary> /// 获取用户详细信息(包含了获取用户信息操作) /// </summary> /// <param name="code"></param> /// <returns></returns> public static UserDetailInfo GetUserDetailInfo(string code) { var userInfo = GetUserInfo(code); var url = String.Format( "https://qyapi.weixin.qq.com/cgi-bin/user/getuserdetail?access_token={0}", GetToken()); JObject obj = new JObject(); obj["user_ticket"] = userInfo.User_ticket; var postDataStr = obj.ToString(); string strResponse = HttpHelper.HttpPost(url, postDataStr); return JsonConvert.DeserializeObject<UserDetailInfo>(strResponse); } public class UserDetailInfo { /// <summary> /// 成员UserID /// </summary> [JsonProperty("userid")] public string UserId { get; set; } /// <summary> /// 成员姓名 /// </summary> [JsonProperty("name")] public string Name { get; set; } /// <summary> /// 成员所属部门 /// </summary> [JsonProperty("department")] public List<int> Department { get; set; } /// <summary> /// 职位信息 /// </summary> [JsonProperty("position")] public string Position { get; set; } /// <summary> /// 成员手机号,仅在用户同意snsapi_privateinfo授权时返回 /// </summary> [JsonProperty("mobile")] public string Mobile { get; set; } /// <summary> /// 性别。0表示未定义,1表示男性,2表示女性 /// </summary> [JsonProperty("gender")] public string Gender { get; set; } /// <summary> /// 成员邮箱,仅在用户同意snsapi_privateinfo授权时返回 /// </summary> [JsonProperty("email")] public string Email { get; set; } /// <summary> /// 头像url。注:如果要获取小图将url最后的”/0”改成”/64”即可 /// </summary> [JsonProperty("avatar")] public string Avatar { get; set; } }