2013-01-10 25 views
3

我有REST服務要求,其中一些呼叫需要認證,另一些則不需要。絕對沒有使用狀態,因爲呼叫都是相互獨立的。我已經把一些東西放在一起了,這似乎是正常的,但這是不使用會話的正確方法嗎?這是在ServiceStack上進行無狀態身份驗證的正確方法嗎?

這個問題是有關我的WCF問題,這是回答here

首先我登記的認證方法:

 Plugins.Add(new AuthFeature(() => new AuthUserSession(), 
            new IAuthProvider[] { 
             new CustomCredentialsAuthProvider(), //HTML Form post of UserName/Password credentials 
            } 
         )); 

我然後屬性相應的呼叫(或服務或DTO)與所述身份驗證屬性:

[Authenticate] 
public HelloResponse Post(Hello request) 
{ 
    return new HelloResponse { Result = "Hello, " + request.Name + " with POST & Auth"}; 
} 

我從BasicAuthProvider類不繼承認證:

public class CustomCredentialsAuthProvider : BasicAuthProvider 
{ 
    public override bool TryAuthenticate(IServiceBase authService, string userName, string password) 
    { 
     return userName == "dylan" && password == "abc123"; 
    } 

    public override void OnAuthenticated(IServiceBase authService, IAuthSession session, IOAuthTokens tokens, Dictionary<string, string> authInfo) 
    { 
     session.IsAuthenticated = true; 

     //Important: You need to save the session! 
     authService.SaveSession(session, new TimeSpan(0,0,10)); 
    } 
} 

正如你所看到的,我確實保存了會話,但在10秒後超時。這是我肯定可以做得更好的部分。它似乎很好地工作。

有沒有更好的方式來做我想要完成的事情? 由於這些服務的會話性質,還有什麼方法可以刪除Auth,AssignRoles和UnassignRoles方法嗎?

回答

3

如果你想使用ServiceStack's AuthenticationSession支持,你可以只添加一個響應濾波器執行服務後清除用戶會話,例如保持:

執行每個請求後
this.ResponseFilters.Add((req, res, dto) => req.RemoveSession()); 

基本上,它會清除會話,所以沒有他們的身份驗證記錄存在。

否則,您可以完全跳過使用ServiceStack的身份驗證,只需通過FilterAttributesRequestFitlers(這基本上是SS Auth在底層所做的)提供您自己的身份。

相關問題