2011-09-23 46 views
2

我正在使用DotNetOpenAuth v3.5.0.10357,並且每次用戶通過Facebook進行身份驗證時,我都會得到一個不同的聲明標識符。該令牌看起來是加密的,所以我認爲DNOA以某種方式加密令牌以及到期。任何人都可以確認嗎?還是我用錯了:DotNetOpenAuth來自Facebook的聲稱標識符絕不相同

public ActionResult FacebookLogOn(string returnUrl) 
    { 
     IAuthorizationState authorization = m_FacebookClient.ProcessUserAuthorization(); 
     if (authorization == null) 
     { 
      // Kick off authorization request 
      return new FacebookAuthenticationResult(m_FacebookClient, returnUrl); 
     } 
     else 
     { 
      // TODO: can we check response status codes to see if request was successful? 
      var baseTokenUrl = "https://graph.facebook.com/me?access_token="; 
      var requestUrl = String.Format("{0}{1}", baseTokenUrl, Uri.EscapeDataString(authorization.AccessToken)); 
      var claimedIdentifier = String.Format("{0}{1}", baseTokenUrl, authorization.AccessToken.Split('|')[0]); 

      var request = WebRequest.Create(requestUrl); 
      using (var response = request.GetResponse()) 
      { 
       using (var responseStream = response.GetResponseStream()) 
       { 
        var graph = FacebookGraph.Deserialize(responseStream); 
        var token = RelyingPartyLogic.User.ProcessUserLogin(graph, claimedIdentifier); 
        this.FormsAuth.SignIn(token.ClaimedIdentifier, false); 
       } 
      } 

      return RedirectAfterLogin(returnUrl); 
     } 
    } 

下面是FacebookAuthenticationResult代碼:

public class FacebookAuthenticationResult : ActionResult 
{ 
    private FacebookClient m_Client; 
    private OutgoingWebResponse m_Response; 

    public FacebookAuthenticationResult(FacebookClient client, string returnUrl) 
    { 
     m_Client = client; 

     var authorizationState = new AuthorizationState(new String[] { "email" }); 
     if (!String.IsNullOrEmpty(returnUrl)) 
     { 
      var currentUri = HttpContext.Current.Request.Url; 
      var path = HttpUtility.UrlDecode(returnUrl); 

      authorizationState.Callback = new Uri(String.Format("{0}?returnUrl={1}", currentUri.AbsoluteUri, path)); 

     } 
     m_Response = m_Client.PrepareRequestUserAuthorization(authorizationState); 
    } 

    public FacebookAuthenticationResult(FacebookClient client) : this(client, null) { } 

    public override void ExecuteResult(ControllerContext context) 
    { 
     m_Response.Send(); 
    } 
} 

而且,我使用包括DNOA樣品中RelyingPartyLogic項目,但我添加了ProcessUserLogin一個重載的具體Facebook的:

public static AuthenticationToken ProcessUserLogin(FacebookGraph claim, string claimedIdentifier) 
    { 
     string name = claim.Name; 
     string email = claim.Email; 
     if (String.IsNullOrEmpty(name)) 
      name = String.Format("{0} {1}", claim.FirstName, claim.LastName).TrimEnd(); 
     return ProcessUserLogin(claimedIdentifier, "http://facebook.com", email, name, claim.Verified); 
    } 

看起來好像FacebookClient從WebServerClient繼承,但我找了GitHub上的來源,我沒有看到一個分支或相關標籤(或至少未標記)與相應的v3.5版本。

回答

5

,Facebook並不支持OpenID。聲稱的標識符是OpenID術語。 Facebook使用OAuth 2.0,所以你混合了OpenID和OAuth。

Facebook每次都發送一個不同的訪問令牌,這對於OAuth協議來說是正常的。您必須使用訪問令牌來查詢Facebook上的每次訪問都一致的用戶ID。

+1

好的,我沒有意識到我使用了錯誤的術語。我將Facebook添加到了DNOA項目模板中,並重用了現有的東西,因此它們的表現都一樣。因此,我將使用返回的UserId值作爲FacebookGraph對象的一部分,然後構建像http://facebook.com/oauth?userid=123456789這樣的用戶標識字符串。謝謝。 –