你的位置:首页 > ASP.net教程

[ASP.net教程]微信二次认证 C#


using Senparc.Weixin.Entities;using Senparc.Weixin.HttpUtility;using Senparc.Weixin.QY.AdvancedAPIs.OAuth2;using Senparc.Weixin.QY.CommonAPIs;using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI;using System.Web.UI.WebControls;namespace CRM.CIS.WeiXin{  public partial class TwoVerification : System.Web.UI.Page  {    protected void Page_Load(object sender, EventArgs e)    {      if (!IsPostBack)      {        string code = Request["code"];        string state = Request["state"];        if (!string.IsNullOrEmpty(code) && !string.IsNullOrEmpty(state))        {          string CorpId = Properties.Settings.Default.CorpId;          string Secret = Properties.Settings.Default.Secret;
//注册corpid if (!AccessTokenContainer.CheckRegistered(CorpId)) { AccessTokenContainer.Register(CorpId, Secret); } string accessToken = AccessTokenContainer.GetToken(CorpId); GetUserIdResult guir = Senparc.Weixin.QY.AdvancedAPIs.OAuth2.OAuth2Api.GetUserId(accessToken, code, 1); if (guir.errcode == 0) {
//读取职员状态 var member = Senparc.Weixin.QY.AdvancedAPIs.MailList.MailListApi.GetMember(accessToken, guir.UserId); hfaccessToken.Value = accessToken;//使用HiddenField保存了信息 hfValue.Value = guir.UserId;//HiddenField 保存了uid ObjectDataSource1.DataBind(); if (member.status == 1) { btnOK.Enabled = false; btnOK.Text = "已关注"; } else if (member.status == 2) { btnOK.Enabled = false; btnOK.Text = "已冻结"; } else { btnOK.Enabled = true; btnOK.Text = "请关注"; } } }#if DEBUG else { hfValue.Value = "MysticBoy"; ObjectDataSource1.DataBind(); }#endif } }
//这是从数据库里面查询要用ObjectDataSource1显示的内容的方法, 根据你的情况来写这段代码 public bas_Company GetCompanyInfo(string userid) { bas_Company result = null; using (CISDBEntities ef = new CISDBEntities()) { var ci = from c in ef.bas_Company where c.UserName == userid select c; if (ci.Any()) { result = ci.Single(); } } return result; } protected void btnOK_Click(object sender, EventArgs e) { string userId = hfValue.Value; string accessToken = hfaccessToken.Value; var member = Senparc.Weixin.QY.AdvancedAPIs.MailList.MailListApi.GetMember(accessToken, userId);
//根据上次保留的accessToken userId 信息查询这个会员是不是已经被关注,尤其是在手机里面点了返回后! if (member.status == 4)//是未关注 {
//https://github.com/JeffreySu/WeiXinMPSDK 里面的
// Senparc.Weixin.QY.AdvancedAPIs.Concern.ConcernApi.TwoVerification
 //截止2015年4月14日里面的参数没传对, 因此在这里使用自己的代码 var url = string.Format("https://qyapi.weixin.qq.com/cgi-bin/user/authsucc?access_token={0}&userid={1}", accessToken, userId); var result = Get.GetJson<QyJsonResult>(url); if (result.errcode == 0) { btnOK.Enabled = false; btnOK.Text = "已经关注"; } else { btnOK.Text = result.errmsg; } } else if (member.status == 1)//已经关注了 { btnOK.Enabled = false; btnOK.Text = "已关注"; } else if (member.status == 2)//被冻结了 { btnOK.Enabled = false; btnOK.Text = "已冻结"; } } }}

  由于本职工作是Linux下使用C做嵌入式应用开发,关于微信等BS方面相形见拙,请看官不要见笑