2011-03-25 26 views
1

這裏的問題,Facebook C#SDK。如何自動登錄用戶,如果他們已經從facebook.com登錄?

我想讓用戶自動登錄到我的MVC3網站,如果他們已經從facebook.com登錄。

如果他們不是從facebook.com登錄,我只是簡單地顯示一個登錄按鈕,他們可以像往常一樣點擊登錄按鈕。

我似乎無法找到一種方法來檢測用戶是否從facebook.com登錄,除非我強制應用程序直接進入Facebook的LoginUrl,這對於未登錄的用戶不利,因爲這會提示他們一個他們必須登錄的頁面。我仍然希望允許訪客無需使用Facebook帳戶。

有什麼想法?太感謝了。

編輯:

對不起,我忘了提到的情況只適用於誰批准使用我的應用程序的用戶。一旦他們批准使用我的應用程序,我就能夠檢測到他們的Facebook登錄狀態。

回答

0

答案是:你不行。至少不是沒有違反Facebook TOS,可能至少有一兩個法律。

編輯:你的原始問題聽起來像你想嘗試和劫持facebook用戶會話。您在評論中所說的內容要求您使用臉書連接/臉譜網站。

你想從這裏開始:https://developers.facebook.com/docs/guides/web

+0

我已經看到了一些網站,居然能夠做到這一點,如http://shopsocial.ly/。一旦我批准使用他們的應用程序,每當我進入該網站時,他們都會檢測我是否已登錄Facebook。 – 2011-03-25 03:59:49

+0

感謝您的澄清。那麼這是否意味着我必須使用他們的JavaScript SDK而不是我現在使用的Facebook C#SDK? – 2011-03-25 04:22:12

+0

我不這麼認爲,一些快速的谷歌搜索,我發現這一點:http://facebooksdk.codeplex.com/wikipage?title=Getting%20Started%20with%20an%20ASP.NET%20MVC%203%20Website&referringTitle=Getting%20Started – 2011-03-25 04:32:15

0
public class FacebookOAuth 
{ 
    private string _loginUrl = "https://www.facebook.com/login.php?login_attempt=1"; 
    private string _redirectUrl = "https://www.facebook.com/connect/login_success.html"; 
    private string _authorizeUrl = "https://graph.facebook.com/oauth/authorize?client_id={0}&redirect_uri={1}&scope={2}"; 
    private string _tokenUrl = "https://graph.facebook.com/oauth/access_token?code={0}&client_id={1}&redirect_uri={2}"; 
    private CookieContainer _cookieContainer = new CookieContainer(); 
    private string _httpsRefUrl = "https://facebook.com"; 


    public string Authenticate(UserInfo user) 
    { 
     // get post data 
     var postData = GetPostData(user, _loginUrl); 
     // authentificate   
     var content = GetContant(_loginUrl, postData); 
     return content; 
    } 

    private string GetPostData(UserInfo user, string loginUrl) 
    { 
     // get content from login form 
     var content = GetContant(loginUrl); 
     return GetPostDataFromContent(content, user); 
    } 

    private string GetPostDataFromContent(string content, UserInfo user) 
    { 
     var dictOfPostData = new Dictionary<string, string>(); 
     if (!string.IsNullOrEmpty(content)) 
     { 
      var doc = XDocument.Parse(content); 
      var inputs = doc.Descendants(XName.Get("input")); 
      foreach (var item in inputs) 
      { 
       var attrbuteName = item.Attributes(XName.Get("name")).FirstOrDefault(); 
       var attrbuteValue = item.Attributes(XName.Get("value")).FirstOrDefault(); 
       if (attrbuteName != null) 
       { 
        switch (attrbuteName.Value) 
        { 
         case "lsd": 
         case "default_persistent": 
         case "timezone": 
         case "lgnrnd": 
         case "lgnjs": 
         case "locale": 
          dictOfPostData.Add(attrbuteName.Value, attrbuteValue.Value); 
          break; 
         case "email": 
          dictOfPostData.Add(attrbuteName.Value, user.Login); 
          break; 
         case "pass": 
          dictOfPostData.Add(attrbuteName.Value, user.Password); 
          break; 

        } 
       } 
      } 
     } 
     return string.Join("&", dictOfPostData.Select(pair => string.Format("{0}={1}", pair.Key, pair.Value))); ; 
    } 
    /// <summary> 
    /// <see cref="GetContant(string, string, Func&lt;HttpWebResponse, string&gt;)"/> 
    /// </summary> 
    /// <param name="url"></param> 
    /// <returns></returns> 
    public string GetContant(string url) 
    { 
     return GetContant(url, string.Empty, null); 
    } 

    /// <summary> 
    /// <see cref="GetContant(string, string, Func&lt;HttpWebResponse, string&gt;)"/> 
    /// </summary> 
    /// <param name="url"></param> 
    /// <param name="postData"></param> 
    /// <returns></returns> 
    public string GetContant(string url, string postData) 
    { 
     return GetContant(url, postData, null); 
    } 

    /// <summary> 
    /// <see cref="GetContant(string, string, Func&lt;HttpWebResponse, string&gt;)"/> 
    /// </summary> 
    /// <param name="url"></param> 
    /// <param name="funcParseResponse"></param> 
    /// <returns></returns> 
    public string GetContant(string url, Func<HttpWebResponse, string> funcParseResponse) 
    { 
     return GetContant(url, string.Empty, funcParseResponse); 
    } 

    /// <summary> 
    /// Get content from web page or write post data 
    /// If post data empty call method=GET, else POST 
    /// </summary> 
    /// <param name="url">Start url</param> 
    /// <param name="postData">Post data, can be null</param> 
    /// <param name="funcParseResponse"></param> 
    /// <returns></returns> 
    public string GetContant(string url, string postData, Func<HttpWebResponse, string> funcParseResponse) 
    { 
     var content = string.Empty; 
     var encoding = Encoding.UTF8; 
     var webRequest = (HttpWebRequest)HttpWebRequest.Create(url); 

     if (!string.IsNullOrEmpty(postData)) 
     { 
      webRequest.Method = "POST"; 
     } 

     webRequest.Referer = _httpsRefUrl; 
     webRequest.UserAgent = "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; WOW64; Trident/7.0)"; 
     webRequest.Accept = "text/html, application/xhtml+xml, */*"; 
     webRequest.Headers.Add("Accept-Language", "ru"); 
     webRequest.ContentType = "application/x-www-form-urlencoded"; 
     webRequest.CookieContainer = _cookieContainer; 
     webRequest.KeepAlive = true; 
     webRequest.AllowAutoRedirect = false; 

     if (!string.IsNullOrEmpty(postData)) 
     { 
      // Write post data 
      var dataBytes = encoding.GetBytes(postData); 
      webRequest.ContentLength = dataBytes.Length; 
      webRequest.GetRequestStream().Write(dataBytes, 0, dataBytes.Length); 
     }; 

     // make request 
     using (var webResponse = (HttpWebResponse)webRequest.GetResponse()) 
     { 
      using (var stream = webResponse.GetResponseStream()) 
      { 
       var streamReader = new StreamReader(stream, encoding); 
       content = streamReader.ReadToEnd(); 
      } 
      var parseResult = funcParseResponse != null ? funcParseResponse.Invoke(webResponse) : string.Empty; 
      if (!string.IsNullOrEmpty(parseResult)) 
      { 
       return parseResult; 
      } 
      // If we have status 302 
      if (webResponse.StatusCode == HttpStatusCode.Found) 
      { 
       var redirectUrl = Convert.ToString(webResponse.Headers["Location"]); 
       // call handly 
       content = this.GetContant(redirectUrl, funcParseResponse); 
      } 
     } 
     return content; 
    } 
} 

public class UserInfo 
    { 
     public UserInfo(string login, String pwd) 
     { 
      Login = login; 
      Password = pwd; 
     } 
     public string Login { get; set; } 
     public string Password { get; set; } 

    } 
相關問題