用c#开发微信(2)扫描二维码,用户授权后获取用户基本信息 (源码下载)
本文将介绍基于Senparc.Weixin微信开发框架来实现网页授权来获取用户基本信息。先生成包含授权及回调url信息的二维码;用户用微信扫描之后,被要求授权以获取Ta的用户基本信息;用户授权后,通过回调url页面获取并显示用户的基本信息;在这个页面上加一个按钮,点点击按钮后,把用户的基本信息保存到数据库。
下面介绍详细的步骤:
1. 生成二维码
下面这个url是一个授权页面,包含了回调的url参数redirect_uri:
注意,要把appid和redirect_url换成你自己的
打开 http://cli.im/url ,输入上面的url,生成二维码图片
2. 新建一个二维码的页面
<form id="form1" runat="server">
<div style="align-content:center;align-items:center">
<h2>OAuth2.0授权测试</h2>
<img src="../Images/QrCode.png" height="190" width="190" alt="" />
</div>
</form>
把上面生成的二维码图片放到页面上。
3. 新建上面提到的回调页面:
前台:
<form id="form1" type="post" runat="server">
<div>
<p>下面是通过授权得到的您的部分个人信息:</p>
<p>
nickname:
<label>
<%=(ViewState["WeixinUserInfo"] as Youda.ViewEntity.UserInfoEntity).NickName%></label>
</p>
<p>
country:
<%=(ViewState["WeixinUserInfo"] as Youda.ViewEntity.UserInfoEntity).Country %>
</p>
<p>
province:
<%=(ViewState["WeixinUserInfo"] as Youda.ViewEntity.UserInfoEntity).Province %>
</p>
<p>
city:
<%= (ViewState["WeixinUserInfo"] as Youda.ViewEntity.UserInfoEntity).City %>
</p>
<p>
sex:
<%= (ViewState["WeixinUserInfo"] as Youda.ViewEntity.UserInfoEntity).Sex %>
</p>
<p>
头像(直接调用可能看不到,需要抓取):<br />
<img src="<%=(ViewState["WeixinUserInfo"] as Youda.ViewEntity.UserInfoEntity).HeadImgUrl %>" />
</p>
<p>
<input style="height:50px;width:90px" type="submit" data-theme="b" value="点赞" id="bsubmit" rel="external" />
</p>
</div>
</form>
注意这里的type一定要设成post: <form id="form1" type="post" runat="server"> , 不然页面会多次被调用,以至于下面的用code取access_token时,报40029 invalid code的错误。因为第一次取完之后,立马再去取就会报40029的错误。
后台:
private string appId = ConfigurationManager.AppSettings["appID"];
private string appSecret = ConfigurationManager.AppSettings["appSecret"];
ILog log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
protected void Page_Load(object sender, EventArgs e)
{
log.Info(Request.HttpMethod);
if (Page.IsPostBack)
{
try
{
if (ViewState["WeixinUserInfo"] == null) Response.End();
var userInfo = ViewState["WeixinUserInfo"] as UserInfoEntity;
log.Info(userInfo.NickName);
new UserInfoBll().Create(userInfo);
Response.Write("保存成功");
Response.End();
}
catch (Exception ex)
{
log.Error(ex.Message, ex);
Response.End();
}
}
else
{
if (ViewState["WeixinUserInfo"] == null)
{
log.Info(Request.Url);
string code = Request["code"];
if (string.IsNullOrEmpty(code))
{
Response.Write("您拒绝了授权!");
Response.End();
}
log.Info(code);
OAuthAccessTokenResult result = null;
//通过,用code换取access_token
try
{
result = result = OAuthApi.GetAccessToken(appId, appSecret, code);
}
catch (ErrorJsonResultException jex)
{
log.Error(jex.Message, jex);
Response.Write("0 " + " , error: " + jex.Message);
Response.End();
}
catch (Exception ex)
{
log.Error(ex.Message, ex);
Response.Write("1 " + " code: " + code + " , error: " + ex.Message);
Response.End();
}
if (result.errcode != ReturnCode.请求成功)
{
Response.Write("2 " + result.errmsg);
Response.End();
}
//下面2个数据也可以自己封装成一个类,储存在数据库中(建议结合缓存)
//如果可以确保安全,可以将access_token存入用户的cookie中,每一个人的access_token是不一样的
//Session["OAuthAccessTokenStartTime"] = DateTime.Now;
//Session["OAuthAccessToken"] = result;
//因为第一步选择的是OAuthScope.snsapi_userinfo,这里可以进一步获取用户详细信息
try
{
OAuthUserInfo userInfo = OAuthApi.GetUserInfo(result.access_token, result.openid);
var user = new UserInfoEntity()
{
City = userInfo.city,
Province = userInfo.province,
Country = userInfo.country,
HeadImgUrl = userInfo.headimgurl,
//Language = userInfo.language,
//Subscribe_time = userInfo.subscribe_time,
Sex = (Sex)userInfo.sex,
NickName = userInfo.nickname,
OpenId = userInfo.openid
};
log.Info(user.NickName);
ViewState["WeixinUserInfo"] = user;
//Response.Write(ViewState["WeixinUserInfo"]);
//Response.End();
}
catch (ErrorJsonResultException ex)
{
log.Error(ex.Message, ex);
Response.Write("3 " + ex.Message);
Response.End();
}
}
}
}
1) 根据回调url上的code取access_token
result = OAuthApi.GetAccessToken(appId, appSecret, code);
2) 根据access_token取用户信息
OAuthUserInfo userInfo = OAuthApi.GetUserInfo(result.access_token, result.openid);
3)在页面上显示用户信息
var user = new UserInfoEntity()
{
City = userInfo.city,
Province = userInfo.province,
Country = userInfo.country,
HeadImgUrl = userInfo.headimgurl,
Sex = (Sex)userInfo.sex,
NickName = userInfo.nickname,
OpenId = userInfo.openid
};
ViewState["WeixinUserInfo"] = user;
注意显示到页面上的实体一定要记得序列化,否则显示不出来:
[Serializable]
public class UserInfoEntity : IViewModel<UserInfoEntity, UserInfo>
4) 当点击页面上的 点赞按钮后,保存用户的信息
if (Page.IsPostBack)
{
var userInfo = ViewState["WeixinUserInfo"] as UserInfoEntity;
log.Info(userInfo.NickName);
new UserInfoBll().Insert(userInfo);
Response.Write("保存成功");
Response.End();
}
数据访问框架层(ORM)采用了.NET平台的Entity Framework来实现;实体数据的转换使用开源的Object-Object Mapping工具Automapper。
4. 下载源码
http://yunpan.cn/cjapF4dFvngiu 访问密码 ca16
使用源码前,要先创建数据库(ORM/db.sql),修改config文件(里面是xxxx的)