2013-09-26 32 views
1

我有一個API寫在ServiceStack,我試圖建立在客戶端的身份驗證。目前這個API只能被Android客戶端(Xamarin/C#)訪問。該API本身運行在Apache/mod_mono上的Debian服務器上Servicestack用戶會話不工作

在讀完Github後,我仍然不能100%確定如何將它們放在一起......一旦客戶端提供了有效的證書(用於測試,基本HTTP認證)用戶獲得一個會話,並且在來自同一會話的後續請求中不再檢查憑證。

APPHOST類:

{ 
public class AppHost 
    : AppHostBase 
{  
    public AppHost() //Tell ServiceStack the name and where to find your web services 
     : base("Service Call Service", typeof(ServiceCallsService).Assembly) { } 

    public override void Configure(Funq.Container container) 
    { 
     //Set JSON web services to return idiomatic JSON camelCase properties 
     ServiceStack.Text.JsConfig.EmitCamelCaseNames = true; 

     // Session storage 
     container.Register<ICacheClient>(new MemoryCacheClient()); 

     // auth feature and session feature 
     Plugins.Add(new AuthFeature(
      () => new AuthUserSession(), 
      new[] { new userAuth() } 
     ) { HtmlRedirect = null }); 
    } 

    public class userAuth : BasicAuthProvider 
    { 
     public override bool TryAuthenticate(IServiceBase authService, string userName, string password) 
     { 
      peruseAuth peruseAuthHandler= new peruseAuth(); 
      errorLogging MyErrorHandler = new errorLogging() ; 
      if (peruseAuthHandler.ValidateUser(authService, userName, password)) 
      { 
       try 
       { 
        var session = (AuthUserSession)authService.GetSession(false); 
        session.UserAuthId = userName; 
        session.IsAuthenticated = true; 
        return true; 
       } 
       catch(Exception ex) 
       { 
        MyErrorHandler.LogError(ex, this) ; 
        return false ; 
       } 
      } 
      else 
      { 
       Console.Write("False"); 
       return false; 
      } 
     } 
    } 

的JsonServiceClient:(就在 「登錄」 事件)

 btnLogin.Click += (sender, e) => 
      { 
       // Set credentials from EditText elements in Main.axml 
       client.SetCredentials(txtUser.Text, txtPass.Text); 

       // Force the JsonServiceClient to always use the auth header 
       client.AlwaysSendBasicAuthHeader = true; 

      }; 

我已經做了一些記錄,並且它似乎每當客戶端執行一個操作時,他們的用戶名/密碼都將被檢查對數據庫。這裏有什麼不對嗎,還是這是預期的結果?

回答

1

對於Basic Auth其中憑證在每個預期的請求上發送。

爲了使ServiceClient保留已經被認證應設置RememberMe標誌,當您驗證會話cookie,例如使用CredentialsAuthProvider

var client = new JsonServiceClient(BaseUrl); 
var authResponse = client.Send(new Authenticate { 
    provider = "credentials", 
    UserName = "user", 
    Password = "[email protected]", 
    RememberMe = true, 
}); 

Behind the scenes這個附加的用戶會話的ss-pid餅乾(永久會話cookie),客戶端在該ServiceClient實例的後續請求中保留並重新發送。