Bin 引用DLL
AppCode引用类文件
protected void Page_Load(object sender, System.EventArgs e)
{
if (!IsPostBack)
{
string userAgent = Request.UserAgent;
if (userAgent.ToLower().Contains("micromessenger"))
{
//Response.Write("欢迎您在微信中访问我。");
//code=CODE&state=STATE
if (Request.QueryString["code"] != null)
{
//第一步:定义APPID等
string APPID = "*************************";
string APPSECRET = "*************************"; ;
//第二步:通过code换取网页授权access_token
string code = Request.QueryString["code"];
ZSJson myjson = new ZSJson();
string url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=" + APPID + "&secret=" + APPSECRET + "&code=" + code + "&grant_type=authorization_code";
string strReturn = myjson.GetFunction(url);
JObject jo = JObject.Parse(strReturn);
string ACCESS_TOKEN = jo["access_token"].ToString(); //读取获取到的ACCESS_TOKEN
string OPENID = jo["openid"].ToString(); //读取获取到的OPENID
Session["openid"] = OPENID;
//第四步:拉取用户信息(需scope为 snsapi_userinfo)
string nexturl = "https://api.weixin.qq.com/sns/userinfo?access_token=" + ACCESS_TOKEN + "&openid=" + OPENID + "&lang=zh_CN";
string nextstrReturn = myjson.GetFunction(nexturl);
//Response.Write(nextstrReturn);
JObject result = JObject.Parse(nextstrReturn);
string nickname = result["nickname"].ToString();
string sex = result["sex"].ToString();
string headimgurl = result["headimgurl"].ToString();
string province = result["province"].ToString();
string city = result["city"].ToString();
/************业务逻辑(默认注册帐号,注册完成后进入某个指定页面)****************************/
string Password = "123456"; //密码
string cMD5Pass = FormsAuthentication.HashPasswordForStoringInConfigFile(Password, "MD5"); //MD5加密
SqlConn mysql = new SqlConn();
Hashtable httParam = new Hashtable();
httParam.Add("@OPType", 777);
httParam.Add("@iRole", 1);
httParam.Add("@OPENID", OPENID);
httParam.Add("@cUserName", nickname);
httParam.Add("@cMobile", "");
httParam.Add("@cPic", headimgurl);
httParam.Add("@cPassword", cMD5Pass);
string StoredtName = "tb_User_Proc";
DataTable dt = mysql.ExecuteDataTable(StoredtName, httParam, CommandType.StoredProcedure);
if (dt.Rows.Count > 0)
{
CookieManage mycookies = new CookieManage();
mycookies.DeleteCookie("UserID");
mycookies.DeleteCookie("UserName");
mycookies.DeleteCookie("UserMobile");
int cookiestime = 1440;
mycookies.WriteCookie("UserID", dt.Rows[0]["ID"].ToString(), cookiestime, true);
mycookies.WriteCookie("UserName", dt.Rows[0]["cUserName"].ToString(), cookiestime, true);
mycookies.WriteCookie("UserMobile", dt.Rows[0]["cMobile"].ToString(), cookiestime, true);
Page.ClientScript.RegisterStartupScript(Page.GetType(), "", "<script language=javascript>autoPage()</script>");
}
/*************************************/
}
else
{
//第一步:用户同意授权,获取code
//在确保微信公众账号拥有授权作用域(scope参数)的权限的前提下(服务号获得高级接口后,默认拥有scope参数中的snsapi_base和snsapi_userinfo),引导关注者打开如下页面:
string APPID = "*************************";
string webdomain = "http://" + HttpContext.Current.Request.Url.Host;
string REDIRECT_URI = webdomain + "/Default.aspx";
string url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" + APPID + "&redirect_uri=" + REDIRECT_URI + "&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect";
Response.Redirect(url);
}
}
else
{
//非微信浏览器打开(电脑,手机浏览器)
Page.ClientScript.RegisterStartupScript(Page.GetType(), "", "<script language=javascript>autoPage()</script>");
}
}
}