Recently, the development of micro-trust public platform, to do a record, has also developed before, this development and forget, engaged in a half-day, or make a note for good.
Note that the framework develops a micro-trust public platform for MVC. Scene for, in the template page to get the user OpenID, want to verify the page, integrated template page on it.
Add the following code to the _layout.cshtml
Adding Getopenid method in class Adminutil
#region Get OpenID
///<summary>
///get OpenID
///</summary> public
static string Getopenid (String redirect_url, string code)
{
string AppID = Wxmodel.appid;
string appsecret = Wxmodel.appsecret;
String OpenID = "";
OpenID = Wxapi.getopenid (AppID, Redirect_url, Code, Appsecret);
return OpenID;
}
#endregion
Adding Getopenid method in class Wxapi
#region get OpenID///<summary>///get OpenID///</summary> public static string Getopenid (string AppID, String Redirect_url, String code, String screct) {string Strjson
= ""; if (string.
IsNullOrEmpty (code)) {Redirect_url = Httputility.urlencode (Redirect_url); HttpContext.Current.Response.Redirect (String. Format ("https://open.weixin.qq.com/connect/oauth2/authorize?appid={0}&redirect_uri={1}&response_type= CODE&SCOPE=SNSAPI_BASE&STATE={2} #wechat_redirect ", AppID, Redirect_url, New Random (). Next (1000, 200000).
ToString ())); else {Strjson = Httprequestutil.requesturl (string. Format ("Https://api.weixin.qq.com/sns/oauth2/access_token?appid={0}&secret={1}&code={2}&grant_type
=authorization_code ", AppID, Screct, code));
Return Tools.getjsonvalue (Strjson, "OpenID"); } #endregion
public static class Wxmodel {public static string Access_token;
public static string AppID;
public static string Appsecret; }
///<summary>///Tool class///</summary> public class Tools {#re Gion gets the value of a node of a JSON string///<summary>///Gets the value of a node of a JSON string///</summary> public static string Getjs Onvalue (String jsonstr, String key) {string result = string.
Empty; if (!string. IsNullOrEmpty (JSONSTR)) {key = "\" "+ Key.
Trim (' "") + "\"; int index = Jsonstr.indexof (key) + key.
Length + 1; if (Index > key.
Length + 1) {//First truncate comma, if last, truncate "}" number, take minimum int end = Jsonstr.indexof (', ', index);
if (end = = 1) {end = Jsonstr.indexof ('} ', index);
result = jsonstr.substring (index, End-index); result = result. Trim (new char[] {' "', ', ', '} '});
Filter quotes or spaces} return result; }
#endregion}
public class Httprequestutil {#region request URL, do not send data///<summary>///request URL, do not send data///</summary
> public static string requesturl (string url) {return Requesturl (URL, "POST"); #endregion #region Request URL, do not send data///<summary>///request URL, do not send data///</summary> public static string Requesturl (string url, string method) {//set parameter HttpWebRequest request = WebRequest.Create (U
RL) as HttpWebRequest;
Cookiecontainer Cookiecontainer = new Cookiecontainer (); Request.
Cookiecontainer = Cookiecontainer; Request.
AllowAutoRedirect = true; Request.
Method = method; Request.
ContentType = "text/html"; Request.
Headers.add ("CharSet", "Utf-8"); Send request and get corresponding response data httpwebresponse response = Request.
GetResponse () as HttpWebResponse; Until request. The GetResponse () program only starts sending post requests to the target Web page Stream responsestream = response.
GetResponseStream (); StreamReader sr = new STREamreader (Responsestream, Encoding.default); Returns the resulting web page (HTML) code string content = Sr.
ReadToEnd ();
return content;
} #endregion}
Note: You need to set the authorization callback domain in the micro-trust public platform
The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.