Home > Mobile >  Small program for openid, backend server for c #
Small program for openid, backend server for c #

Time:09-20

WeChat applet access to unique openid user code, a key official document, online to find the basic were written according to the website document strategy for half a day, not very useful, now finishing a detailed documentation, for the small program for beginners to learn,
The first step: first of all, is a small program, the code is simple, but it is not easy to find the key, app. Js, call wx. The login (), access code, and then call wx. Request, must look good, url is not official website "https://api.weixin.qq.com/sns/jscode2session", this is can't directly access address applet, must have the background support, is that you need to write a daemon, through the background code to access to the official web site, the url to here is what you call the official interface code, after the call is successful, the bottom shows the openid


Url to the second step, the small program interface address code, is my own writing background program, note that here the official interface has four parameters, the four parameters can end the value of small programs, also can own background replication, look at his/her needs, the four parameters are respectively appid and secret (login his account for the official background), js_code, to get their code values, is a small program end and write authorization_code grant_type directly, can,
Here is the c # code, I use the get method to get the data access interface, and then in json format is returned to the small program end


In addition to the openid, simple and get the user information, the method of getuserinfo, this code and website, https://mp.weixin.qq.com/debug/wxadoc/dev/api/open.html#wxgetuserinfoobject, careful reading

GetUserInfo: function (cb) {
Var that=this
If (this. GlobalData. The userInfo) {
Typeof cb=="function" & amp; & Cb (enclosing globalData. The userInfo)
} else {
//call the login interface
Wx. Login ({
Success: the function (res) {
Var code=res. Code
If (res) code) {
//get the user basic information
Wx. GetUserInfo ({
Success: the function (res) {
That. GlobalData. The userInfo=res. The userInfo
That. GlobalData. RawData=https://bbs.csdn.net/topics/res.rawData
That. GlobalData. Signature=res. People like
That. GlobalData. EncryptedData=https://bbs.csdn.net/topics/res.encryptedData
That. GlobalData. Iv=res. Iv
Typeof cb=="function" & amp; & Cb (that) globalData) the userInfo)
}
})
}

}
})
}
},
GlobalData: {
The userInfo: null,
RawData: null,
Signature: null,
EncryptedData: null,
Iv: null,
}


Code documentation address: a small program that provides the client and the c # code, relatively complete http://download.csdn.net/download/springpp1990/10244829

CodePudding user response:

At first it mentally, but overall is simple

CodePudding user response:

After download, change their correlation ID can use directly?

CodePudding user response:

Thank you, very good c #, can achieve! Your code is a little small flaws, I improved a bit
Small program end:
OnLoad: function () {
Wx. Login ({
Success (res) {
If (res) code) {
The console. The log (" user login credentials "+ res. Code).
Wx. Request ({
Url: 'http://localhost:59132/api/user',
Data: {
Code: res. Code,
},
Success (res) {
The console. The log (" for openid "+ res. Data);
}
})
}
}
})
C # client:
[HttpGet]
Public string login (string code)
{
String js_code=code. The ToString ();
String serviceAddress="HTTP://https://api.weixin.qq.com/sns/jscode2session? Appid=wx361987b5ef220c26 & amp; Secret=be34f824aaaf286979111d61ad7e3e64 & amp; Js_code js_code="+ +" & amp; Grant_type=authorization_code ";
HttpWebRequest request=(HttpWebRequest) WebRequest. Create (serviceAddress);
Request. The Method="GET";
Request. The ContentType="text/HTML. Charset=utf-8 ";
HttpWebResponse response=(HttpWebResponse) request. The method GetResponse ();
Stream myResponseStream=response. GetResponseStream ();
StreamReader myStreamReader=new StreamReader (myResponseStream, Encoding UTF8);
String retString=myStreamReader. ReadToEnd ();
MyStreamReader. Close ();
MyResponseStream. Close ();
Return retString;
}

CodePudding user response:

Upstairs the appid secret written really wild

CodePudding user response:

How to get the openId written to the database?

CodePudding user response:

W the whole software development/ASP.NET
Bbs1 and put the opentid written to the database should be how to write?

CodePudding user response:

C # client:
[HttpGet]
Public string login (string code)

Vs how to create this file? Please step down, thank you!

CodePudding user response:

The original poster, the logic of writing is very clear, and finally know how to through WeChat logged in the registered users,
There are two don't understand, it is through a small program after landing the code every time (get), to take the openId, the openId is each WeChat user permanent unique identification?
The second is how to obtain WeChat user ni said and phone?

CodePudding user response:

This grave dug, ah! Not now! Small program login is not allowed in such background, must open the user experience, let the user login only on their own will,
  • Related