2013-06-05 64 views
1

我有一種情況,我正在使用Credentials身份驗證成功,但我有時需要能夠從服務內部僅使用用戶的電子郵件地址創建經身份驗證的會話。我怎樣才能做到這一點?我想我正在尋找類似的意圖FormsAuthentication.SetAuthCookie(),但我還沒有找到它。如何在ServiceStack中創建經過身份驗證的會話?

這是我迄今爲止的想法。假設我已經到這個構建自己,我看這裏面CredentialsAuthProvider.TryAuthenticate

if (authRepo.TryAuthenticate(userName, password, out userAuth)) 
{ 
    session.PopulateWith(userAuth); 
    session.IsAuthenticated = true; 
    session.UserAuthId = userAuth.Id.ToString(CultureInfo.InvariantCulture); 
    session.ProviderOAuthAccess = authRepo.GetUserOAuthProviders(session.UserAuthId) 
     .ConvertAll(x => (IOAuthTokens)x); 

    return true; 
} 

這似乎在暗示,我可以從IUserAuthRepository.GetUserAuthByUserName()做什麼是塊內自己通過UserAuth對象。我不認爲這是足夠的,因爲會議沒有保存在這裏。反向工作時,我發現TryAuthenticate由Authenticate調用,然後繼續呼叫OnAuthenticated,其他事情發生在其中,包括保存會話。我可以解決CredentialsAuthProvider的實例並調用OnAuthenticated myself?

我在正確的軌道上,還是這是完全錯誤的方式去做這件事?

回答

1

有時需要能夠僅使用用戶的電子郵件地址從服務內部創建經過身份驗證的會話。我怎樣才能做到這一點?我想我在尋找的意圖相似FormsAuthentication.SetAuthCookie東西()

我覺得我能想到的最簡單,最FormsAuthentication.SetAuthCookie()方法是在你的服務來修改內AuthUserSession。下面的代碼將獲得會話,將IsAuthenticated設置爲true,設置電子郵件並保存會話。

public class SomeService : Service 
{ 
    public String Any(SomeRequest request) 
    { 
     //not sure you need it, but you could put some code here to verify the email is allowed to authenticate 
     //and if true run the code below 
     var sess = this.SessionAs<AuthUserSession>(); 
     sess.IsAuthenticated = true; 
     sess.Email = "[email protected]"; 
     this.SaveSession(sess); 

     return "success"; 
    } 
} 
+0

技術上可行,但會話不能真正用於任何事情,因爲除了電子郵件地址外,它幾乎完全是空的。我可以從'UserAuth'填充它,但即使如此,它仍然缺少其他類希望存在的其他內容,例如UserAuthId。我想我也可以填充它,但也有一個Cookie丟失,「X-UAId」。這似乎是我在這裏重新編碼,這正是我希望我不需要做的。 – Peeyotch

+0

那麼,如果你真的只想使用電子郵件地址進行「身份驗證」,那麼最好的辦法可能是創建一個自定義身份驗證提供程序(https://github.com/ServiceStack/ServiceStack/wiki/Authentication-and-authorization#自定義的認證和授權)。 – paaschpa

+0

是的,這看起來像我需要去的路線。謝謝。 – Peeyotch

相關問題