微信第三方登陸

 1 string returnUrl = string.Empty;
 2             string appId = ConfigurationManager.AppSettings["wx_AppKey"];
 3             string appSecret = ConfigurationManager.AppSettings["wx_AppSecret"];
 4 
 5             var weixinOAuth = new WeiXinOAuth();
 6             string code = Request.QueryString["code"];
 7             string state = Request.QueryString["state"];
 8 
 9             if (string.IsNullOrEmpty(code) || code == "authdeny")
10             {
11                 if (string.IsNullOrEmpty(code))
12                 {
13                     //發起受權(第一次微信握手)
14                     string authUrl = weixinOAuth.GetWeiXinCode(appId, appSecret, Server.UrlEncode(Request.Url.ToString()), true);
15                     Response.Redirect(authUrl, true);
16                 }
17                 else
18                 {
19                     // 用戶取消受權
20                     ViewData["StatusMessageData"] = new StatusMessageData(StatusMessageType.Error, "受權失敗!");
21                 }
22             }
23             else
24             {
25                 //獲取微信的Access_Token(第二次微信握手)
26                 var modelResult = weixinOAuth.GetWeiXinAccessToken(appId, appSecret, code);
27                 //獲取微信的用戶信息(第三次微信握手)
28                 var userInfo = weixinOAuth.GetWeiXinUserInfo(modelResult.SuccessResult.access_token, modelResult.SuccessResult.openid);
29                 //用戶信息(判斷是否已經獲取到用戶的微信用戶信息)
30                 if (userInfo.Result && userInfo.UserInfo.openid != "")
31                 {
32                     //根據OpenId判斷數據庫是否存在,若是存在,直接登陸便可
33                     if (CurrentUser != null)
34                     {
35                         if (AccountBindings.GetUser(2, userInfo.UserInfo.openid) == null)
36                         {
37                             AccountBinding account = new AccountBinding();
38                             account.AccountType = 2;
39                             account.Identification = userInfo.UserInfo.openid;
40                             account.UserID = CurrentUser.UserID;
41                             account.OauthToken = modelResult.SuccessResult.access_token;
42                             account.OauthTokenSecret = appSecret;
43                             AccountBindings.CreateAccountBinding(account);
44 
45                             ViewData["StatusMessageData"] = new StatusMessageData(StatusMessageType.Success, "綁定成功");
46                         }
47                         else
48                         {
49                             ViewData["StatusMessageData"] = new StatusMessageData(StatusMessageType.Hint, "此帳號已在網站中綁定過,不可再綁定其餘網站帳號");
50                         }
51                     }
52                     else
53                     {
54                         User systemUser = AccountBindings.GetUser(2, userInfo.UserInfo.openid);
55                         if (systemUser != null)
56                         {
57                             FormsAuthentication.SetAuthCookie(systemUser.UserName, true);
58                             returnUrl = SiteUrls.Instance().UserDomainHome(systemUser);
59                             ViewData["StatusMessageData"] = new StatusMessageData(StatusMessageType.Success, "登陸成功");
60                         }
61                         else
62                         {
63                             ViewData["StatusMessageData"] = new StatusMessageData(StatusMessageType.Success, string.Empty);
64                             ViewData["UserName"] = WebUtils.UrlEncode(userInfo.UserInfo.nickname);
65                             string nickName = WebUtils.UrlEncode(userInfo.UserInfo.nickname);
66                             string figureurl = userInfo.UserInfo.headimgurl;
67                             ViewData["FirstLogin"] = true;
68                             returnUrl = SPBUrlHelper.Action("ThirdRegister", "ChannelUser", new RouteValueDictionary { { "accountType", 2 }, { "openID", userInfo.UserInfo.openid }, { "OauthToken", modelResult.SuccessResult.access_token }, { "OauthTokenSecret", appSecret }, { "nickName", nickName }, { "figureurl", figureurl } });
69                         }
70                     }
71                 }
72                 else
73                 {
74                     ViewData["StatusMessageData"] = new StatusMessageData(StatusMessageType.Error, "參數錯誤,受權失敗!");
75                 }
76             }
77 
78             ViewData["returnUrl"] = returnUrl;
79             return View("Pages/User/LoginCallback.aspx");
  1 using System;
  2 using System.Collections.Generic;
  3 using System.Linq;
  4 using System.Text;
  5 using System.Web;
  6 using System.Net;
  7 using System.IO;
  8 using System.Collections.Specialized;
  9 using System.Text.RegularExpressions;
 10 using System.Globalization;
 11 using System.Web.Security;
 12 using System.Web.UI;
 13 using System.Web.UI.WebControls;
 14 using System.Web.UI.WebControls.WebParts;
 15 using System.Web.UI.HtmlControls;
 16 using SpaceBuilder.Common;
 17 using Jayrock.Json.Conversion;
 18 using System.Configuration;
 19 
 20 
 21 public class WeiXinOAuth
 22 {
 23     public enum Method { GET, POST, PUT, DELETE };
 24     /// <summary>
 25     /// 獲取微信Code
 26     /// </summary>
 27     /// <param name="appId">微信AppId</param>
 28     /// <param name="appSecret">微信AppSecret</param>
 29     /// <param name="redirectUrl">返回的登陸地址,要進行Server.Un編碼</param>
 30     /// <param name="isWap">true=微信內部登陸 false=pc網頁登陸</param>
 31     public string GetWeiXinCode(string appId, string appSecret, string redirectUrl, string state, bool isWap)
 32     {
 33         var r = new Random();
 34         //微信登陸受權
 35         //string url = "https://open.weixin.qq.com/connect/qrconnect?appid=" + appId + "&redirect_uri=" + redirectUrl +"&response_type=code&scope=snsapi_login&state=STATE#wechat_redirect";
 36         //微信OpenId受權
 37         //string url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" + appId + "&redirect_uri=" + redirectUrl +"&response_type=code&scope=snsapi_login&state=STATE#wechat_redirect";
 38         //微信用戶信息受權
 39         var url = "";
 40         if (isWap)
 41         {
 42             url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" + appId + "&redirect_uri=" +
 43                   redirectUrl + "&response_type=code&scope=snsapi_userinfo&state=" + state + "#wechat_redirect";
 44         }
 45         else
 46         {
 47             url = "https://open.weixin.qq.com/connect/qrconnect?appid=" + appId + "&redirect_uri=" + redirectUrl +
 48                   "&response_type=code&scope=snsapi_login&state=" + state + "#wechat_redirect";
 49         }
 50         return url;
 51     }
 52     /// <summary>
 53     /// 經過code獲取access_token
 54     /// </summary>
 55     /// <param name="appId"></param>
 56     /// <param name="appSecret"></param>
 57     /// <param name="code"></param>
 58     /// <returns></returns>
 59     public WeiXinAccessTokenResult GetWeiXinAccessToken(string appId, string appSecret, string code)
 60     {
 61         string url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=" + appId + "&secret=" + appSecret +
 62           "&code=" + code + "&grant_type=authorization_code";
 63         string jsonStr = _WebRequest(Method.GET, url, ""); 
 64         var result = new WeiXinAccessTokenResult();
 65         if (jsonStr.Contains("errcode"))
 66         {
 67             var errorResult = (WeiXinHelper.WeiXinErrorMsg)JsonConvert.Import(typeof(WeiXinHelper.WeiXinErrorMsg), jsonStr);
 68             result.ErrorResult = errorResult;
 69             result.Result = false;
 70         }
 71         else
 72         {
 73             var model = (WeiXinAccessTokenModel)JsonConvert.Import(typeof(WeiXinAccessTokenModel), jsonStr);
 74             result.SuccessResult = model;
 75             result.Result = true;
 76         }
 77         return result;
 78     }
 79     /// <summary>
 80     /// 拉取用戶信息
 81     /// </summary>
 82     /// <param name="accessToken"></param>
 83     /// <param name="openId"></param>
 84     /// <returns></returns>
 85     public WeiXinHelper.WeiXinUserInfoResult GetWeiXinUserInfo(string accessToken, string openId)
 86     {
 87         string url = "https://api.weixin.qq.com/sns/userinfo?access_token=" + accessToken + "&openid=" + openId + "⟨=zh_CN";
 88         string jsonStr = _WebRequest(Method.GET ,url,"");
 89         var result = new WeiXinHelper.WeiXinUserInfoResult();
 90         if (jsonStr.Contains("errcode"))
 91         {
 92             var errorResult = (WeiXinHelper.WeiXinErrorMsg)JsonConvert.Import(typeof(WeiXinHelper.WeiXinErrorMsg), jsonStr);
 93             result.ErrorMsg = errorResult;
 94             result.Result = false;
 95         }
 96         else
 97         {
 98             var userInfo = (WeiXinHelper.WeiXinUserInfo)JsonConvert.Import(typeof(WeiXinHelper.WeiXinUserInfo), jsonStr);
 99             result.UserInfo = userInfo;
100             result.Result = true;
101         }
102         return result;
103     }
104 
105 
106     private string _WebRequest(Method method, string url, string postData)
107     {
108         HttpWebRequest webRequest = null;
109         StreamWriter requestWriter = null;
110         string responseData = "";
111 
112         webRequest = System.Net.WebRequest.Create(url) as HttpWebRequest;
113         webRequest.Method = method.ToString();
114         webRequest.ServicePoint.Expect100Continue = false;
115 
116         if (method == Method.POST)
117         {
118             webRequest.ContentType = "application/x-www-form-urlencoded";
119             requestWriter = new StreamWriter(webRequest.GetRequestStream());
120             try
121             {
122                 requestWriter.Write(postData);
123             }
124             catch
125             {
126                 throw;
127             }
128             finally
129             {
130                 requestWriter.Close();
131                 requestWriter = null;
132             }
133         }
134 
135         responseData = _WebResponseGet(webRequest);
136 
137         webRequest = null;
138 
139         return responseData;
140 
141     }
142 
143     private string _WebResponseGet(HttpWebRequest webRequest)
144     {
145         StreamReader responseReader = null;
146         string responseData = "";
147         try
148         {
149             responseReader = new StreamReader(webRequest.GetResponse().GetResponseStream());
150             responseData = responseReader.ReadToEnd();
151         }
152         catch
153         {
154             throw;
155         }
156         finally
157         {
158             webRequest.GetResponse().GetResponseStream().Close();
159             responseReader.Close();
160             responseReader = null;
161         }
162 
163         return responseData;
164     }
165 }
166 
167 /// <summary>
168 /// 經過code獲取access_token 請求成功的實體
169 /// </summary>
170 public class WeiXinAccessTokenModel
171 {
172     /// <summary>
173     /// 接口調用憑證
174     /// </summary>
175     public string access_token { get; set; }
176     /// <summary>
177     /// access_token接口調用憑證超時時間,單位(秒)
178     /// </summary>
179     public int expires_in { get; set; }
180     /// <summary>
181     /// 用戶刷新access_token
182     /// </summary>
183     public string refresh_token { get; set; }
184     /// <summary>
185     /// 受權用戶惟一標識
186     /// </summary>
187     public string openid { get; set; }
188     /// <summary>
189     /// 用戶受權的做用域,使用逗號(,)分隔
190     /// </summary>
191     public string scope { get; set; }
192 }
193 
194 public class WeiXinAccessTokenResult
195 {
196     public WeiXinAccessTokenModel SuccessResult { get; set; }
197     public bool Result { get; set; }
198 
199     public WeiXinHelper.WeiXinErrorMsg ErrorResult { get; set; }
200 }
201 
202 /// <summary>
203     /// 微信幫助類
204     /// </summary>
205 public class WeiXinHelper
206     {
207         /// <summary>
208         /// 微信錯誤訪問的狀況
209         /// </summary>
210         public class WeiXinErrorMsg
211         {
212             /// <summary>
213             /// 錯誤編號
214             /// </summary>
215             public int errcode { get; set; }
216             /// <summary>
217             /// 錯誤提示消息
218             /// </summary>
219             public string errmsg { get; set; }
220         }
221  
222         /// <summary>
223         /// 獲取微信用戶信息
224         /// </summary>
225         public class WeiXinUserInfoResult
226         {
227             /// <summary>
228             /// 微信用戶信息
229             /// </summary>
230             public WeiXinUserInfo UserInfo { get; set; }
231             /// <summary>
232             /// 結果
233             /// </summary>
234             public bool Result { get; set; }
235             /// <summary>
236             /// 錯誤信息
237             /// </summary>
238             public WeiXinErrorMsg ErrorMsg { get; set; }
239         }
240  
241         /// <summary>
242         /// 微信受權成功後,返回的用戶信息
243         /// </summary>
244         public class WeiXinUserInfo
245         {
246             /// <summary>
247             /// 用戶的惟一標識
248             /// </summary>
249             public string openid { get; set; }
250             /// <summary>
251             /// 用戶暱稱
252             /// </summary>
253             public string nickname { get; set; }
254             /// <summary>
255             /// 用戶的性別,值爲1時是男性,值爲2時是女性,值爲0時是未知
256             /// </summary>
257             public string sex { get; set; }
258             /// <summary>
259             /// 用戶我的資料填寫的省份
260             /// </summary>
261             public string province { get; set; }
262             /// <summary>
263             /// 普通用戶我的資料填寫的城市
264             /// </summary>
265             public string city { get; set; }
266             /// <summary>
267             /// 國家,如中國爲CN
268             /// </summary>
269             public string country { get; set; }
270             /// <summary>
271             /// 用戶頭像,最後一個數值表明正方形頭像大小(有0、4六、6四、9六、132數值可選,0表明640*640正方形頭像),用戶沒有頭像時該項爲空
272             /// </summary>
273             public string headimgurl { get; set; }
274             /// <summary>
275             /// 用戶特權信息,json 數組,如微信沃卡用戶爲(chinaunicom)
276             /// </summary>
277             public string[] privilege { get; set; }
278             /// <summary>
279             /// 用戶統一標識,針對一個微信開放平臺賬號下的應用,同一用戶的unionid是惟一的
280             /// </summary>
281             public string unionid { get; set; }
282         }
283     }
相關文章
相關標籤/搜索