2012-11-04 27 views
1

Login.cshtmlServiceStack CustomUserSession鑄造例外

.... 
<form action="/auth/credentials"> 
    <div> 
     <span>User Name</span> 
     <input name="userName" type="text"/> 
    </div><div> 
     <span>Password</span> 
     <input name="password" type="password"/> 
    </div><div> 
     <input name="RememberMe" type="checkbox" checked value="true"/> 
     <span>Remember me</span> 
    </div><div> 
     <button class="login">Sign in</button> 
    </div> 
</form> 
.... 

LoginService.cs

[DefaultView("login")] 
public class LoginService : BaseService { 
    public object Get(Login req) { 
     return new LoginResponse() { UserSession = base.UserSession }; 
    } 
} 

BaseService.cs

.... 
public CustomUserSession UserSession { 
    get { 
     return SessionAs<CustomUserSession>(); // **** Error here ****   
    } 
} 
.... 

錯誤消息SessionAs<CustomUserSession>();

InvalidCastException的是由用戶代碼

無法轉換類型 'ServiceStack.ServiceInterface.Auth.AuthUserSession' 的對象爲類型 'MyProject.CustomUserSession' 未處理。

我有一個簡單的html表格login.cshtml來測試證書認證功能。該代碼模仿SocialBootstrapApi演示項目的憑證授權功能部分。

我來自的NuGet更新之前SS:(約40天前包)

如果HTML表單有RememberMe divSessionAs<CustomUserSession>();就按InvalidCastException錯誤。否則它可以正常工作,只需UserNamePassword div即可。

我來自的NuGet更新SS後:(包更新的今天11月4日v3.9.25.0)

SessionAs<CustomUserSession>();總是命中即使當HTML表單只是在它UserNamePassword的div的InvalidCastException錯誤。

我不知道我在鑄造過程中做了什麼錯誤。請幫忙。

回答

2

你的認證註冊是什麼樣的?

這就是SocialBootstrapApi項目的AuthConfig樣子:

Plugins.Add(new AuthFeature(
    () => new CustomUserSession(), //Use your own typed Custom UserSession type 
    new IAuthProvider[] { 
     new CredentialsAuthProvider(),    //HTML Form post 
     new TwitterAuthProvider(appSettings),  //Sign-in with Twitter 
     new FacebookAuthProvider(appSettings),  //Sign-in with Facebook 
     new DigestAuthProvider(appSettings),  //Sign-in with Digest Auth 
     new BasicAuthProvider(),     //Sign-in with Basic Auth 
     new GoogleOpenIdOAuthProvider(appSettings), //Sign-in with Goolge OpenId 
     new YahooOpenIdOAuthProvider(appSettings), //Sign-in with Yahoo OpenId 
     new OpenIdOAuthProvider(appSettings),  //Sign-in with Custom OpenId 
    })); 

new CustomUserSession()就是告訴ServiceStack使用自定義會話,而不是內置的默認AuthUserSession

你在使用什麼CacheClient?

+0

非常感謝你的神話!我使用標準的UserAuthSession而不是CustomUserSession!你擊中了要害! – Tom