2010-12-28 3 views
2

ASP.NET을 사용하고 있습니다. FB de-auth에 대한 수신기를 구현하고 싶습니다.Facebook deauthorize 콜백 사용 방법

FB 서버에서 signed_request 매개 변수를 가져옵니다. C#을 사용하여 어떻게 해독 할 수 있습니까?

감사합니다.

+0

수 duplicat :

protected void Page_Load(object sender, EventArgs e) { if (!String.IsNullOrEmpty(Request["signed_request"])) { string signed_request = Request["signed_request"]; Dictionary<string, Facebook.JSONObject> jsonDict = new Dictionary<string, Facebook.JSONObject>(); if (Helper.FacebookAPI.ValidateSignedRequest(signed_request, out jsonDict)) { if (jsonDict.ContainsKey("user_id")) { long FacebookId = jsonDict["user_id"].Integer; // delete code } } } } 

그런 다음 내 페이스 북 도우미 클래스는 다음과 같습니다 어떻게 C#에서 Canvas signed_request에 대한 OAuth 2.0을 디코딩합니까? ] (http://stackoverflow.com/questions/3433252/how-to-decode-oauth-2-0-for-canvas-signed-request-in-c) –

답변

0

CodePlex에서 Facebook C# SDK을 확인하십시오.이를 사용하거나 signed_request 암호화를 처리하는 방법을 확인하십시오. 유사한 기울기가있는 this SO 게시물도 있습니다.

아니 정말 인정 대답, 당신이 바로 온 경우

3

확실하지 댓글에 링크를 넣어하는 방법을 알고 ...하지만 난 페이스 북의 C# SDK를 참조하고이 짓하지 않습니다

을 Deauth.aspx :

namespace Helper { 
public static class FacebookAPI 
{ 
    public static Dictionary<string, Facebook.JSONObject> DecodePayload(string payload) 
    { 
     var encoding = new UTF8Encoding(); 
     var decodedJson = payload.Replace("=", string.Empty).Replace('-', '+').Replace('_', '/'); 
     var base64JsonArray = Convert.FromBase64String(decodedJson.PadRight(decodedJson.Length + (4 - decodedJson.Length % 4) % 4, '=')); 
     var json = encoding.GetString(base64JsonArray); 
     var jObject = Facebook.JSONObject.CreateFromString(json);    
     return jObject.Dictionary; 
    } 


    public static bool ValidateSignedRequest(string VALID_SIGNED_REQUEST, out Dictionary<string, Facebook.JSONObject> json) 
    { 
     string applicationSecret = ConfigurationManager.AppSettings["Secret"]; 
     string[] signedRequest = VALID_SIGNED_REQUEST.Split('.'); 
     string expectedSignature = signedRequest[0]; 
     string payload = signedRequest[1]; 

     json = DecodePayload(payload); 

     // Attempt to get same hash 
     var Hmac = SignWithHmac(UTF8Encoding.UTF8.GetBytes(payload), UTF8Encoding.UTF8.GetBytes(applicationSecret)); 
     var HmacBase64 = ToUrlBase64String(Hmac); 

     return (HmacBase64 == expectedSignature); 
    } 


    private static string ToUrlBase64String(byte[] Input) 
    { 
     return Convert.ToBase64String(Input).Replace("=", String.Empty) 
              .Replace('+', '-') 
              .Replace('/', '_'); 
    } 

    private static byte[] SignWithHmac(byte[] dataToSign, byte[] keyBody) 
    { 
     using (var hmacAlgorithm = new HMACSHA256(keyBody)) 
     { 
      hmacAlgorithm.ComputeHash(dataToSign); 
      return hmacAlgorithm.Hash; 
     } 
    } 


    public static string SerializeDict(Dictionary<string, Facebook.JSONObject> jsonDict) 
    { 
     // serialize the dictionary 
     DataContractSerializer serializer = new DataContractSerializer(jsonDict.GetType()); 

     using (StringWriter sw = new StringWriter()) 
     { 
      using (XmlTextWriter writer = new XmlTextWriter(sw)) 
      { 
       // add formatting so the XML is easy to read in the log 
       writer.Formatting = Formatting.Indented; 

       serializer.WriteObject(writer, jsonDict); 

       writer.Flush(); 

       return sw.ToString(); 
      } 
     } 
    } 



    public static string GetAuthToken() 
    { 

     string appId = ConfigurationManager.AppSettings["AppId"]; 
     string secret = ConfigurationManager.AppSettings["Secret"]; 

     string url = String.Format("https://graph.facebook.com/oauth/access_token?client_id={0}&client_secret={1}&grant_type=client_credentials", appId, secret); 

     string[] token = HttpGetData(url).Split('='); 
     return token[1]; 
    } 

    public static string HttpGetData(string url) 
    { 
     HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest; 
     using (HttpWebResponse response = request.GetResponse() as HttpWebResponse) 
     { 
      StreamReader reader = new StreamReader(response.GetResponseStream()); 
      return (reader.ReadToEnd()); 
     } 
    } 
    public static string HttpPostData(string url, string nameValuePair) 
    { 

     HttpWebRequest request = WebRequest.Create(url + "&" + nameValuePair) as HttpWebRequest; 
     request.Method = WebRequestMethods.Http.Post; 
     try 
     { 
      using (HttpWebResponse response = request.GetResponse() as HttpWebResponse) 
      { 
       StreamReader reader = new StreamReader(response.GetResponseStream()); 
       return (reader.ReadToEnd()); 
      } 
     } 
     catch (WebException ex) 
     { 
      return ex.Message; 
     } 
    } 
}}