C#开发——网站应用微信登录开发
1. 在微信开放平台注册开发者账号,并有一个审核已通过的网站应用,并获得相对应的appid和appsecret,申请通过登陆后,方可开始接入流程。
2.微信oauth2.0授权登录目前支持authorization_code模式,适用于拥有server端的应用授权。该模式整体流程为:
1. 第三方发起微信授权登录请求,微信用户允许授权第三方应用后,微信会拉起应用或重定向到第三方网站,并且带上授权临时票据code参数;
2. 通过code参数加上appid和appsecret等,通过api换取access_token;
3. 通过access_token进行接口调用,获取用户基本数据资源或帮助用户实现基本操作。
3.实现代码如下:
两个类对象
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; }
}
1. 若access_token已超时,那么进行refresh_token会获取一个新的access_token,新的超时时间;
2. 若access_token未超时,那么进行refresh_token不会改变access_token,但超时时间会刷新,相当于续期access_token。
refresh_token拥有较长的有效期(30天),当refresh_token失效的后,需要用户重新授权。
public actionresult wxlogin(string code,string state)
{
try
{
if (!string.isnullorwhitespace(code))
{
string url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=wx21314153e6b7381c&secret=e7edd072dd05315a7d1048002e36c267&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;
httpwebresponse response = (httpwebresponse)request.getresponse();
stream myresponsestream = response.getresponsestream();
streamreader mystreamreader = new streamreader(myresponsestream, encoding.utf8);
string retstring = mystreamreader.readtoend();
//转为json对象
user u = common.jsonhelper.jsondeserialize<user>(retstring);
if (u != null)
{
url = "https://api.weixin.qq.com/sns/oauth2/refresh_token?appid=wx21314153e6b7381c&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=wx21314153e6b7381c&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
上一篇: 设计模式学习方法