您的位置:首页 > 移动开发 > 微信开发

NET/C# 微信企业号获取Userid

2017-02-04 09:05 375 查看
#region 方法 GetQiyAccess_token(string appid, string appsecret) 调用网页 Access_token

    /// <summary>

    /// 

    /// </summary>

    /// <param name="appid"></param>

    /// <param name="appsecret"></param>

    /// <returns></returns>

    public string GetQiyAccess_token(string appid, string appsecret)

    {

        TWeixinHelper weihelper = new TWeixinHelper();

        string to = weihelper.GetData("https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=" + appid + "&corpsecret=" + appsecret);

        string access_token = "";

        string strCode = @"\""access_token\"":\s*""(?'access_token'[^""]*)""";

        Regex regex = new Regex(strCode, RegexOptions.IgnoreCase);

        if (regex.IsMatch(to))

        {

            MatchCollection matches = regex.Matches(to);

            StringBuilder stringBuilder = new StringBuilder();

            foreach (Match match in matches)

            {

                access_token = match.Groups["access_token"].Value;

            }

        }

        return access_token;

    }

    #endregion

    #region 方法 GetQiyGetUserinfo(string accesstoken, string code)  调用网页 获取用户id

    /// <summary>

    /// 调用网页 Access_token

    /// </summary>

    /// <param name="appid"></param>

    /// <param name="appsecret"></param>

    /// <param name="CODE"></param>

    /// <returns></returns>

    public string GetQiyGetUserinfo(string accesstoken, string code)

    {

        TWeixinHelper weihelper = new TWeixinHelper();

        string to = weihelper.GetData("https://qyapi.weixin.qq.com/cgi-bin/user/getuserinfo?access_token=" + accesstoken + "&code=" + code);

        string strUserId = "";

        string strCode = @"\""UserId\"":\s*""(?'UserId'[^""]*)""";

        Regex regex = new Regex(strCode, RegexOptions.IgnoreCase);

        if (regex.IsMatch(to))

        {

            MatchCollection matches = regex.Matches(to);

            StringBuilder stringBuilder = new StringBuilder();

            foreach (Match match in matches)

            {

                strUserId = match.Groups["UserId"].Value;

            }

        }

        return strUserId;

    }

    #endregion
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: