当前位置: 移动技术网 > IT编程>开发语言>c# > C#实现的微信网页授权操作逻辑封装示例

C#实现的微信网页授权操作逻辑封装示例

2019年07月18日  | 移动技术网IT编程  | 我要评论

本文实例讲述了c#实现的微信网页授权操作逻辑封装。分享给大家供大家参考,具体如下:

一、微信网页授权登录

前提:

1.已经获取的接口权限,如果是测试账号就已经有权限了

2.配置接口的授权域名

更多说明可以参考方倍工作室:http://www.cnblogs.com/txw1958/p/weixin71-oauth20.html

或者官网api:http://mp.weixin.qq.com/wiki/17/c0f37d5704f0b64713d5d2c37b468d75.html

步骤:

1.用户同意授权,获取code

2.根据code 获取access_token及当前操作用户的openid、unionid

3.根据openid获取用户基本信息(如果需要的话)

注:如果想在网站使用扫一扫,授权登录,可以讲 _oauth.getcodeurl() 授权地址生成二维码来使用

c#封装微信网页授权登录使用实例:

string appid = "wx145b4a8fd07b24e8";
string appsecrect = "fe6951dcb99772411c42f724b1336065";
string redirect_url = "配置域名下的回调地址";
oauthmanage _oauth = null;
/// <summary>
///控制器构造函数
/// </summary>
public usercontroller()
{
  _oauth = new oauthmanage(appid, appsecrect, redirect_url);
}
/// <summary>
/// 授权登录
/// </summary>
/// <returns></returns>
public actionresult authlogin()
{
  viewbag.url = _oauth.getcodeurl();
  return view();
}
/// <summary>
/// 回调处理
/// </summary>
/// <returns></returns>
public actionresult oauthhandle()
{
  string result = "";
  //注册事件处理
  _oauth.onerror = (e) =>
  {
    string msg = "";
    exception inner = e;
    while (inner != null)
    {
      msg += inner.message;
      inner = inner.innerexception;
    }
    result = msg;
    logoperate.write(msg);
  };
  _oauth.ongettokensuccess = (token) =>
  {
    result += "<br/>";
    result += token.tojsonstring();
  };
  _oauth.ongetuserinfosuccess = (user) =>
  {
    result += "<br/>";
    result += user.tojsonstring();
  };
  //第二步
  _oauth.getaccess_token();
  //第三步
  _oauth.getuserinfo();
  //显示结果
  viewbag.msg = result;
  return view();
}

封装代码类定义:

namespace wxpackage
{
  /// <summary>
  /// 网页授权逻辑处理,
  /// 处理三步操作,处理成功,返回用户基本信息
  /// </summary>
  public class oauthmanage
  {
    #region 基本信息定义
    /// <summary>
    /// 公众号的唯一标识
    /// </summary>
    private string appid;
    /// <summary>
    /// 公众号的appsecret
    /// </summary>
    private string secret;
    /// <summary>
    /// 回调url地址
    /// </summary>
    private string redirect_uri;
    /// <summary>
    /// 获取微信用户基本信息使用snsapi_userinfo模式
    /// 如果使用静默授权,无法获取用户基本信息但可以获取到openid
    /// </summary>
    private string scope;
    public oauthmanage(string appid, string appsecret, string redirect_uri, bool isuserinfo = true)
    {
      this.appid = appid;
      this.secret = appsecret;
      this.redirect_uri = redirect_uri;
      this.scope = isuserinfo ? "snsapi_userinfo" : "snsapi_base";
    }
    #endregion
    #region 请求过程信息
    /// <summary>
    /// 第一步获取到的code 值
    /// </summary>
    public string code { get; set; }
    /// <summary>
    /// 第二步获取到的access_token及相关数据
    /// </summary>
    public oauthaccess_token tokendata = null;
    #endregion
    #region 事件定义
    /// <summary>
    /// 当处理出现异常时,触发
    /// </summary>
    public action<exception> onerror = null;
    /// <summary>
    /// 当获取accesstoken成功是触发
    /// </summary>
    public action<oauthaccess_token> ongettokensuccess = null;
    /// <summary>
    /// 当获取用户信息成功时触发
    /// </summary>
    public action<oauthuser> ongetuserinfosuccess = null;
    #endregion
    #region 第二步,回调处理
    /// <summary>
    /// 第二步,通过code换取网页授权access_token
    /// </summary>
    public void getaccess_token()
    {
      try
      {
        //1.处理跳转
        this.code = reqhelper.getstring("code");
        if (string.isnullorempty(this.code))
          throw new exception("获取code参数失败或用户禁止授权获取基本信息");
        //1.发送获取access_token请求
        string url = getaccess_tokenurl();
        string result = nethelper.get(url);
        //2.解析相应结果
        tokendata = jsonconvert.deserializeobject<oauthaccess_token>(result);
        if (tokendata == null)
          throw new exception("反序列化结果失败,返回内容为:" + result);
        //3.获取成功
        if (ongettokensuccess != null)
          ongettokensuccess(tokendata);
      }
      catch (exception ex)
      {
        error("第二步,通过code换取网页授权access_token异常", ex);
      }
    }
    /// <summary>
    /// 刷新当前access_token
    /// </summary>
    public oauthaccess_token refreshaccess_token()
    {
      try
      {
        //1.发送请求
        string url = getreferesh_tokenurl();
        string result = nethelper.get(url);
        //2.解析结果
        oauthaccess_token token = jsonconvert.deserializeobject<oauthaccess_token>(result);
        if (token == null)
          throw new exception("反序列化结果失败,返回内容:" + result);
        return token;
      }
      catch (exception ex)
      {
        error("刷新当前access_token失败", ex);
        return null;
      }
    }
    #endregion
    #region 第三步,获取用户基本信息
    /// <summary>
    /// 第三步,获取基本信息
    /// </summary>
    public void getuserinfo()
    {
      try
      {
        //1.发送get请求
        string url = getuserinfourl();
        string result = nethelper.get(url);
        //2.解析结果
        oauthuser user = jsonconvert.deserializeobject<oauthuser>(result);
        if (user == null)
          throw new exception("反序列化结果失败,返回内容:" + result);
        //3.获取成功
        if (ongetuserinfosuccess != null)
          ongetuserinfosuccess(user);
      }
      catch (exception ex)
      {
        error("第三步、获取用户基本信息异常", ex);
      }
    }
    #endregion
    #region 静态方法
    /// <summary>
    /// 验证授权凭证是否有效
    /// </summary>
    /// <param name="access_token">access_token</param>
    /// <param name="openid">用户针对当前公众号的openid</param>
    /// <returns></returns>
    public static bool checkwebaccess_token(string access_token, string openid)
    {
      try
      {
        string url = string.format("https://api.weixin.qq.com/sns/auth?access_token={0}&openid={1}",
       access_token,
       openid);
        string result = nethelper.get(url);
        jobject obj = jobject.parse(result);
        int errcode = (int)obj["errcode"];
        return errcode == 0;
      }
      catch (exception ex)
      {
        throw new exception("," + ex.message);
      }
    }
    #endregion
    #region 获取请求连接
    /// <summary>
    /// 获取code的url 地址
    /// </summary>
    /// <returns></returns>
    public string getcodeurl()
    {
      string url = string.format("https://open.weixin.qq.com/connect/oauth2/authorize?appid={0}&redirect_uri={1}&response_type=code&scope={2}&state=state#wechat_redirect",
        this.appid,
        securityhelper.urlencode(this.redirect_uri),
        this.scope);
      return url;
    }
    /// <summary>
    /// 获取access_token的url地址
    /// </summary>
    /// <returns></returns>
    private string getaccess_tokenurl()
    {
      string url = string.format("https://api.weixin.qq.com/sns/oauth2/access_token?appid={0}&secret={1}&code={2}&grant_type=authorization_code",
        this.appid,
        this.secret,
        this.code);
      return url;
    }
    /// <summary>
    /// 获取刷新accesstoke的地址
    /// </summary>
    /// <returns></returns>
    private string getreferesh_tokenurl()
    {
      string url = string.format("https://api.weixin.qq.com/sns/oauth2/refresh_token?appid={0}&grant_type=refresh_token&refresh_token={1}",
        this.appid,
        this.tokendata.refresh_token
        );
      return url;
    }
    /// <summary>
    /// 获取用户基本信息地址
    /// </summary>
    /// <returns></returns>
    private string getuserinfourl()
    {
      string url = string.format("https://api.weixin.qq.com/sns/userinfo?access_token={0}&openid={1}&lang=zh_cn",
        this.tokendata.access_token,
        this.tokendata.openid);
      return url;
    }
    #endregion
    private void error(string msg, exception inner)
    {
      if (this.onerror != null)
      {
        this.onerror(new exception(msg, inner));
      }
    }
  }
  /// <summary>
  /// 授权之后获取用户基本信息
  /// </summary>
  public class oauthuser
  {
    public string openid { get; set; }
    public string nickname { get; set; }
    public int sex { get; set; }
    public string province { get; set; }
    public string city { get; set; }
    public string country { get; set; }
    public string headimgurl { get; set; }
    /// <summary>
    /// 用户特权信息,json 数组
    /// </summary>
    public jarray privilege { get; set; }
    public string unionid { get; set; }
  }
  /// <summary>
  /// 获取access_token或者刷新返回的数据对象
  /// </summary>
  public class oauthaccess_token
  {
    public string access_token { get; set; }
    public int expires_in { get; set; }
    public string refresh_token { get; set; }
    /// <summary>
    /// 用户针对当前公众号的唯一标识
    /// 关注后会产生,返回公众号下页面也会产生
    /// </summary>
    public string openid { get; set; }
    public string scope { get; set; }
    /// <summary>
    /// 当前用户的unionid,只有在用户将公众号绑定到微信开放平台帐号后
    /// </summary>
    public string unionid { get; set; }
  }
}

更多关于c#相关内容感兴趣的读者可查看本站专题:《c#常见控件用法教程》、《winform控件用法总结》、《c#数据结构与算法教程》、《c#面向对象程序设计入门教程》及《c#程序设计之线程使用技巧总结

希望本文所述对大家c#程序设计有所帮助。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网