0

我已經從Azure移動服務遷移到應用服務,但我很難找出如何最好地實施擴展Facebook身份驗證。Azure應用服務Facebook

在我以前的實現中,我從FacebookLoginProvider繼承並從聲明中提取了令牌。然後,我將CustomFacebookLoginProvider添加到我的登錄提供程序。然後,我使用該令牌獲取有關該用戶的更多信息(他們的出生日期,朋友和性別)。有了這些信息,我創建了一個用戶對象並將其保存到我的數據庫中。

有沒有人有任何建議如何最好地重新在App服務中,因爲我找不到任何文檔。

回答

0

從移動服務切換到移動應用程序時,我必須做出的唯一更改是在開發人員門戶中更改回調URL的末尾以使用/.auth/login/facebook/callback而不是/ signin-facebook和它的工作方式與以前完全相同。

請注意,這是針對具有.NET後端的Windows應用程序;你沒有指定你正在使用的內容,所以你的里程可能會有所不同。

+0

這種方法確實按預期工作,但我沒有辦法獲得Facebook令牌,以便查詢Facebook有關用戶的更多信息。 –

+0

你在使用什麼平臺/後端? –

+0

iOS,但客戶端應該不重要,因爲這應該發生在後端。 –

1

至於如何建立Facebook的身份驗證,您可以在這裏找到文檔(這聽起來像你已經想通了這麼多):

https://azure.microsoft.com/en-us/documentation/articles/app-service-mobile-how-to-configure-facebook-authentication/

現在,Facebook的身份驗證設置,你可以參考它展示瞭如何獲取用戶信息如下:

https://azure.microsoft.com/en-us/documentation/articles/app-service-mobile-dotnet-backend-how-to-use-server-sdk/#user-info

// Get the credentials for the logged-in user. 
var credentials = 
    await this.User 
    .GetAppServiceIdentityAsync<FacebookCredentials>(this.Request); 

if (credentials.Provider == "Facebook") 
{ 
    // Create a query string with the Facebook access token. 
    var fbRequestUrl = "https://graph.facebook.com/me?access_token=" 
     + credentials.AccessToken; 

    // Create an HttpClient request. 
    using (var client = new System.Net.Http.HttpClient()) 
    { 
     // Request the current user info from Facebook. 
     using (var resp = await client.GetAsync(fbRequestUrl)) 
     { 
      resp.EnsureSuccessStatusCode(); 

      // Do something here with the Facebook user information. 
      var fbInfo = await resp.Content.ReadAsStringAsync(); 
     } 
    } 
} 

請注意,您必須添加使用語句爲System.Security.Principal使GetAppServiceIdentityAsync擴展方法的工作。

有關您可以查詢哪些Facebook用戶屬性的更多信息,請參閱此處的Facebook文檔:https://developers.facebook.com/docs/graph-api/reference/user。請注意,您可能需要指定您希望將哪些用戶屬性作爲附加字段您在調用Facebook圖時查詢字符串參數。

+0

我在這裏有一個博客:https://blogs.msdn.microsoft.com/kaushal/2017/06/08/using-easy-auth-to-query-facebook-information-via-graph-api / –

0

我一直在使用以下方法獲取iOS應用程序中的Facebook訪問令牌。

應用程序服務在請求標頭中包含Facebook訪問標記,請參閱https://azure.microsoft.com/en-in/documentation/articles/app-service-api-authentication/

要訪問訪問令牌,請在Azure門戶中創建自定義API,例如, facebookUserInfo,用下面的代碼:

module.exports = { 
"get": function (request, response, next) { 
    response.send(200, { facebookAccessToken: request.headers['x-ms-token-facebook-access-token'] }); 
}};   

在iOS應用中,使用下面的代碼來查詢自定義API:

let client = self.table!.client 
    if client.currentUser != nil { 
     client.invokeAPI("facebookUserInfo", body: nil, HTTPMethod: "GET", parameters: nil, headers: nil, completion: { (result, response, error) -> Void in 
      if let resultDict = result { 
       if let facebookAccessToken = resultDict["facebookAccessToken"]! { 
        print(facebookAccessToken) 
       } 
      } 
     } 
    }