C#開發——網站應用微信登陸開發

1. 在微信開放平臺註冊開發者帳號,並有一個審覈已經過的網站應用,並得到相對應的AppID和AppSecret,申請經過登錄後,方可開始接入流程。json

2.微信OAuth2.0受權登陸目前支持authorization_code模式,適用於擁有server端的應用受權。該模式總體流程爲:api

1. 第三方發起微信受權登陸請求,微信用戶容許受權第三方應用後,微信會拉起應用或重定向到第三方網站,而且帶上受權臨時票據code參數;
2. 經過code參數加上AppID和AppSecret等,經過API換取access_token;
3. 經過access_token進行接口調用,獲取用戶基本數據資源或幫助用戶實現基本操做。

3.實現代碼以下:微信

 

兩個類對象cookie

public class user
{
public string access_token { get; set; } //接口調用憑證
public int expires_in { get; set; } //access_token接口調用憑證超時時間,單位(秒)
public string refresh_token { get; set; }  //用戶刷新access_token
public string openid { get; set; } //受權用戶惟一標識
public string scope { get; set; } //用戶受權的做用域,使用逗號(,)分隔
public string unionid { get; set; } //當且僅當該網站應用已得到該用戶的userinfo受權時,纔會出現該字段
}
public class Errmsg
{
public string errcode { get; set; }
public string errmsg { get; set; }
}app

1. 若access_token已超時,那麼進行refresh_token會獲取一個新的access_token,新的超時時間;網站

2. 若access_token未超時,那麼進行refresh_token不會改變access_token,但超時時間會刷新,至關於續期access_token。url

refresh_token擁有較長的有效期(30天),當refresh_token失效的後,須要用戶從新受權。code

 

public ActionResult WxLogin(string code,string state)
{
try
{
if (!string.IsNullOrWhiteSpace(code))
{
string Url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=******&secret=*******&code=" + code + "&grant_type=authorization_code";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
request.Proxy = null;
request.KeepAlive = false;
request.Method = "GET";
request.ContentType = "application/json; charset=UTF-8";
request.AutomaticDecompression = DecompressionMethods.GZip;orm

HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream myResponseStream = response.GetResponseStream();
StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.UTF8);
string retString = myStreamReader.ReadToEnd();server

//轉爲json對象
user u = Common.JsonHelper.JsonDeserialize<user>(retString);
if (u != null)
{
  Url = "https://api.weixin.qq.com/sns/oauth2/refresh_token?appid=******&grant_type=refresh_token&refresh_token=" + u.refresh_token;
  request = (HttpWebRequest)WebRequest.Create(Url);
  request.Proxy = null;
  request.KeepAlive = false;
  request.Method = "GET";
  request.ContentType = "application/json; charset=UTF-8";
  request.AutomaticDecompression = DecompressionMethods.GZip;

  response = (HttpWebResponse)request.GetResponse();
  myResponseStream = response.GetResponseStream();
  myStreamReader = new StreamReader(myResponseStream, Encoding.UTF8);
  retString = myStreamReader.ReadToEnd();
  user us = Common.JsonHelper.JsonDeserialize<user>(retString);
  TUsers user = new TUsersLogic().GetTUsers(new TUsers() { OpenIDTwo = us.openid });
  if (user != null && user.ID > 0)
  {
    System.Web.HttpCookie cookie = new System.Web.HttpCookie("CurrentUser-" + "ID");
    cookie.Value = user.ID.ToString();
    cookie.Expires = DateTime.Now.AddDays(7);
    System.Web.HttpContext.Current.Response.AppendCookie(cookie);
    return Redirect("/home/index");
  }
  
  return View();
}
else
{
  Errmsg emsg= Common.JsonHelper.JsonDeserialize<Errmsg>(retString);
  return Json(new { errcode = emsg.errcode, errmsg = emsg.errmsg }, JsonRequestBehavior.AllowGet);
}
}
string url = Common.UrlHelper.CurrentUrl;
return Redirect("https://open.weixin.qq.com/connect/qrconnect?appid=*******&redirect_uri=" + url + "&response_type=code&scope=snsapi_login&state=STATE#wechat_redirect");

}
catch (Exception e)
{
return Json(new { errormessage = e.Message });
}
}

請求參數及連接就不一一寫了,參考連接:https://open.weixin.qq.com/cgi-bin/showdocument?action=dir_list&t=resource/res_list&verify=1&id=open1419316505&token=&lang=zh_CN

相關文章
相關標籤/搜索