C# 获取关注微信公众号后访问人的openid进行功能扩展

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Net;
using System.Web.Script.Serialization;

namespace WebClientSearchApp
{
    public partial class GetWeiXinCode : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!base.IsPostBack)
            {
                string str = base.Server.UrlEncode("http://www.xxxx.com.cn/WeiXin/GetWeiXinCode.aspx");
                string text = base.Request.QueryString["Code"];
                if (string.IsNullOrEmpty(text))
                {
                    string url = string.Format("https://open.weixin.qq.com/connect/oauth2/authorize?appid={0}&redirect_uri=" + str + "&response_type=code&scope=snsapi_base&state=STATE#wechat_redirect", ConfigurationManager.AppSettings["AppId"]);
                    base.Response.Redirect(url);
                    return;
                }
                WebClient webClient = new WebClient();
                webClient.Encoding =System.Text.Encoding.UTF8;
                string address = string.Format("https://api.weixin.qq.com/sns/oauth2/access_token?appid={0}&secret={1}&code={2}&grant_type=authorization_code", ConfigurationManager.AppSettings["AppId"], ConfigurationManager.AppSettings["AppSecret"], text);
                string input = webClient.DownloadString(address);
                JavaScriptSerializer javaScriptSerializer = new JavaScriptSerializer();
                Dictionary<string, string> dictionary = javaScriptSerializer.Deserialize<Dictionary<string, string>>(input);
                string text2;
                if (!dictionary.TryGetValue("access_token", out text2))
                {
                    return;
                }
                string opentid = dictionary["openid"];
                using (ClientSearchDataContext clientSearchDataContext = new ClientSearchDataContext())
                {
                    IQueryable<WeiXinApiToken> source = from p in clientSearchDataContext.WeiXinApiToken
                                                                                            where p.OpenID == opentid
                                                                                            select p;
                    if (source.Count<WeiXinApiToken>() > 0)
                    {
                        base.Response.Redirect("http://www.xxxx.com.cn/WeiXin/UserBind.aspx?status=succes&openid= " + opentid);
                    }
                    else
                    {
                        base.Response.Redirect("http://www.xxxx.com.cn/WeiXin/UserBind.aspx?openid=" + opentid);
                    }
                }
            }
        }
    }
}

config 配置文件:

  <appSettings>
    <add key="SqlserverConnectionString" value="data source=192.168.33.13;User ID=sa;pwd=sa;Initial Catalog=xxxx;" />
    <add key="DBConnectionError" value="数据库连接失败。" />
    <add key="MaxWeeks" value="13" />
    <add key="MinPWD" value="8" />
    <add key="ThemeRoot" value="Default" />
    <add key="MiniteInterval" value="15" />
    <add key="TimeLimit" value="2013-07-20" />
   <!--微信的Token-->

   <add key="WeixinToken" value="haixinda"/>
    //开发者ID(AppID)
   <add key="AppId" value="wx55d5944a0be2d2e6b906"/>
   //开发者密匙                                                                                                                                                                                     
   <add key="AppSecret" value="444444444444"/>
  </appSettings>
AppId、AppSecret 都可以登陆微信公众平台后找到

获取访问公众号链接得到的openid

http://www.xxxx.com.cn/WeiXin/UserBind.aspx?openid=oepJesytIaBw4BSxENiv8geHddfdffdCE



猜你喜欢

转载自blog.csdn.net/kongwei521/article/details/79741509