2013-04-11 59 views
5

我一直在使用ServiceStack,它是Auth提供者。特別是「FacebookAuthProvider」。 我的問題在於,該服務是從iOS應用程序調用的。這個應用程序已經有一個有效的訪問令牌,我只是想將這個值傳遞給servicestack Facebook身份驗證。ServiceStack和FacebookAuthProvider

我已經看到了servicestack github頁面上的測試,但它對我仍然沒有意義。

是否有可能將此訪問令牌傳遞給服務堆棧,因此,鑑定會跳過我請求權限的部分,因爲我們已經在應用程序上執行了該操作?

或者我認爲這是錯誤的方式?

+0

你有沒有找到一種方法來做到這一點? – fractal 2013-08-02 08:07:37

回答

3

取而代之的是使用內建facebook auth提供程序,我創建了自己的CustomFacebookAuthProvider。 原因是內置版本需要瀏覽器將用戶重定向到Facebook進行身份驗證,而我並不需要它。我已經有一個訪問令牌。

所以基於官方版本FacebookAuthProvider.cs我創建了我自己的。

using System; 
using System.Collections.Generic; 
using System.Net; 
using Elmah; 
using Mondohunter.Backend.BusinessLogic.Interfaces; 
using ServiceStack.Common.Extensions; 
using ServiceStack.Common.Web; 
using ServiceStack.Configuration; 
using ServiceStack.ServiceInterface; 
using ServiceStack.ServiceInterface.Auth; 
using ServiceStack.Text; 
using ServiceStack.WebHost.Endpoints; 

namespace Mondohunter.Interfaces 
{ 
    public class CustomFacebookAuthProvider : OAuthProvider 
    { 
     public const string Name = "facebook"; 
     public static string Realm = "https://graph.facebook.com/"; 
     public static string PreAuthUrl = "https://www.facebook.com/dialog/oauth"; 

     public string AppId { get; set; } 
     public string AppSecret { get; set; } 
     public string[] Permissions { get; set; } 

     public CustomFacebookAuthProvider(IResourceManager appSettings) 
      : base(appSettings, Realm, Name, "AppId", "AppSecret") 
     { 
      this.AppId = appSettings.GetString("oauth.facebook.AppId"); 
      this.AppSecret = appSettings.GetString("oauth.facebook.AppSecret"); 
     } 

     public override object Authenticate(IServiceBase authService, IAuthSession session, Auth request) 
     { 
      var tokens = Init(authService, ref session, request); 

      try 
      { 
       if (request.oauth_token.IsNullOrEmpty()) 
        throw new Exception(); 

       tokens.AccessToken = request.oauth_token; 
       session.IsAuthenticated = true; 

       var json = AuthHttpGateway.DownloadFacebookUserInfo(request.oauth_token); 
       var authInfo = JsonSerializer.DeserializeFromString<Dictionary<string, string>>(json); 

       //Here i need to update/set userauth id to the email 
       //UpdateUserAuthId(session, authInfo["email"]); 

       authService.SaveSession(session, SessionExpiry); 
       OnAuthenticated(authService, session, tokens, authInfo); 

       //return json/xml/... response; 
      } 
      catch (WebException ex) 
      { 
       //return json/xml/... response; 
      } 
      catch (Exception ex) 
      { 
       //return json/xml/... response; 
      } 
     } 

     protected override void LoadUserAuthInfo(AuthUserSession userSession, IOAuthTokens tokens, Dictionary<string, string> authInfo) 
     { 
      if (authInfo.ContainsKey("id")) 
       tokens.UserId = authInfo.GetValueOrDefault("id"); 
      if (authInfo.ContainsKey("name")) 
       tokens.DisplayName = authInfo.GetValueOrDefault("name"); 
      if (authInfo.ContainsKey("first_name")) 
       tokens.FirstName = authInfo.GetValueOrDefault("first_name"); 
      if (authInfo.ContainsKey("last_name")) 
       tokens.LastName = authInfo.GetValueOrDefault("last_name"); 
      if (authInfo.ContainsKey("email")) 
       tokens.Email = authInfo.GetValueOrDefault("email"); 
      if (authInfo.ContainsKey("gender")) 
       tokens.Gender = authInfo.GetValueOrDefault("gender"); 
      if (authInfo.ContainsKey("timezone")) 
       tokens.TimeZone = authInfo.GetValueOrDefault("timezone"); 

      LoadUserOAuthProvider(userSession, tokens); 
     } 

     public override void LoadUserOAuthProvider(IAuthSession authSession, IOAuthTokens tokens) 
     { 
      var userSession = authSession as CustomUserSession; 
      if (userSession == null) return; 

      userSession.Email = tokens.Email ?? userSession.PrimaryEmail ?? userSession.Email; 
     } 
    } 
} 

我希望它是有道理的。

+0

艾倫,你是如何返回json的,你能舉個例子嗎?另外,如何配置URL以訪問auth提供程序,例如,您擁有「/ auth/facebook」的內置程序。 – pug 2013-10-03 11:14:32