2012-11-30 78 views
3

我在.net中使用FB SDK,在我的網站上有多個用戶配置的Facebook電子郵件。 每當用戶想在FB上發佈任何消息時,他可以選擇任何電子郵件,並且應該能夠在FB上登錄。如何使用FB C#SDK強制登錄Facebook?

問題是,如果用戶已經登錄到FB但想要在其他FB賬戶上發佈消息,我們無法向他顯示登錄屏幕,並且該消息會在已登錄的帳戶上發佈。

即使我們有用戶auth_type=reauthenticate,但這也無助於每次顯示登錄屏幕。我需要一個類似twitter的機制來強制在FB上登錄。

任何人都可以請提供幫助嗎?

回答

1
  1. Facebook應用 - 記你的App ID和App祕密
  2. Json.NET在賓安裝的。從這裏下載:http://json.codeplex.com
  3. 以下引用需要被添加到您的應用程序將設置頁面:使用Newtonsoft.Json.Linq

    ; 使用Newtonsoft.Json.Serialization; using System.Collections.Generic;使用System.Security.Cryptography的 ;

  4. 這是HTML內容:

    <div id="fb-root"></div> 
    
    function runLogin() { 
    FB.init({ 
    appId : 'ENTERYOURAPPIDHERE', 
    status : true, 
    cookie : true, 
    xfbml : true, 
    channelURL: 'ENTERTHEPAGEYOURAPPURLPOINTSTOHERE', // channel.html file 
    oauth : true 
    }); 
    
    <div id="dontLike"> 
        PAGE IS <b>NOT</b> Liked 
    </div> 
    
    </form> 
    

打開您的頁面代碼隱藏。我們驗證了代碼簽名的請求。

public bool ValidateSignedRequest() 
    { 
     var VALID_SIGNED_REQUEST = Request.Form["signed_request"]; 

     string applicationSecret = "YOURAPPSECRET"; 
     string[] signedRequest = VALID_SIGNED_REQUEST.Split('.'); 
     string expectedSignature = signedRequest[0]; 
     string payload = signedRequest[1]; 

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

     return (HmacBase64 == expectedSignature); 
    } 

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

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

public Dictionary<string, string> DecodePayload(string payload) 
    { 
     //Remove the bad part of signed_request 
     //Begin 
     string[] sB64String = payload.Split('.'); 
     payload = payload.Replace((sB64String[0] + "."), string.Empty); 
     //End 
     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 = JObject.Parse(json); 
     var parameters = new Dictionary<string, string>(); 

     parameters.Add("page", ((bool)jObject["page"]["liked"]).ToString()); 
     parameters.Add("admin", ((bool)jObject["page"]["admin"]).ToString()); 
     return parameters; 
    } 

protected void pageLike() 
    { 
     string pageLiked = string.Empty; 
     var signed_request = Request.Form["signed_request"]; 
     var json = DecodePayload(signed_request); 


     foreach (KeyValuePair<string, string> objKVP in json) 
     { 
      //Note You can also see if a user is an admin by replacing the objKVP.Key with admin 
      if (objKVP.Key == "page" && objKVP.Value == "True") 
      { 
       Response.Redirect("https://www.YOURSITE.com/facebook/app/pageLiked.aspx"); 
       litJson.Text += objKVP.Key + " - " + objKVP.Value + "<br />"; 
      } 
     } 
    } 

    protected void Page_Load(object sender, EventArgs e) 
     { 
     pageLike(); 
    } 

您可以在此鏈接看到這個例子:http://blog.daniellecopp.com/2012/03/19/detect-if-facebook-user-likes-your-page-with-asp-net-2/#comment-52

+1

不錯,但你可能需要提供你的答案該鏈接的總結應該是鏈接失效 –

+0

感謝您的回覆,我們需要實現那種做法。 – DiegoRomo