在APP首页:
if 没有登录 { <script src="http://tjs.sjs.sinajs.cn/t35/apps/opent/js/frames/client.js" language="JavaScript"></script> <script> function authLoad() { App.AuthDialog.show({ client_id: '2069027236', //必选,appkey redirect_uri: 'http://apps.weibo.com/randytest', //必选,授权后的回调地址,注意是:apps.weibo.com这域名的 height: 1 //可选,默认距顶端120px }); } //authLoad(); </script> }
然后会弹出一个登录的DIV,登录后会自动返回到app首页,然后在app page load的代码里写:
string signedRequest = Request.Params["signed_request"]; ViewBag.Message = ValidateSignedRequest(signedRequest);
public string App_Secret = "xxxxxxxxxxxxxxxx"; private string ValidateSignedRequest(string signedRequest) { string decodedPayload = ""; string[] signedRequestArray = signedRequest.Split('.'); string expectedSignature = signedRequestArray[0]; string payload = signedRequestArray[signedRequestArray.Length - 1]; byte[] Hmac = SignWithHmac(UTF8Encoding.UTF8.GetBytes(payload), UTF8Encoding.UTF8.GetBytes(App_Secret)); string HmacBase64 = ToUrlBase64String(Hmac); bool result = (HmacBase64 == expectedSignature); if (result) { decodedPayload = System.Text.UTF8Encoding.UTF8.GetString(FromBase64ForUrlString(payload)); } return decodedPayload; } private byte[] SignWithHmac(byte[] dataToSign, byte[] keyBody) { using (System.Security.Cryptography.HMACSHA256 hmacAlgorithm = new System.Security.Cryptography.HMACSHA256(keyBody)) { hmacAlgorithm.ComputeHash(dataToSign); return hmacAlgorithm.Hash; } } private string ToUrlBase64String(byte[] Input) { return Convert.ToBase64String(Input).Replace("=", String.Empty).Replace('+', '-').Replace('/', '_'); } private byte[] FromBase64ForUrlString(string base64ForUrlInput) { int padChars = (base64ForUrlInput.Length % 4) == 0 ? 0 : (4 - (base64ForUrlInput.Length % 4)); StringBuilder result = new StringBuilder(base64ForUrlInput, base64ForUrlInput.Length + padChars); result.Append(String.Empty.PadRight(padChars, '=')).Replace('-', '+').Replace('_', '/'); return Convert.FromBase64String(result.ToString()); }
这样ValidateSignedRequest 返回的就有user的资料了。如下这样的:
{"user":{"country":"cn","locale":""},"algorithm":"HMAC-SHA256","issued_at":1343130106,"expires":1343216506,"oauth_token":"2.00DBErpBCV7BQC169c3e7800XXXXX","user_id":1682372793,"referer":null}
搞定,然后你懂的