Login.cshtml
....
<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 ****
}
}
....
Error Message at SessionAs<CustomUserSession>();
InvalidCastException was unhandled by user code
Unable to cast object of type ‘ServiceStack.ServiceInterface.Auth.AuthUserSession’
to type ‘MyProject.CustomUserSession’.
I have a simple html form in login.cshtml to test out the credentials auth feature. The code is mimicking the credential auth feature part of SocialBootstrapApi demo project.
Before I updated SS from NuGet: (packages from about 40 days ago)
If the html form has RememberMe div, SessionAs<CustomUserSession>(); would hit the InvalidCastException error. Otherwise it works fine with just UserName and Password divs in it.
After I updated SS from NuGet: (packages updated today 4th Nov v3.9.25.0)
SessionAs<CustomUserSession>(); always hits the InvalidCastException error even when the html form just have UserName and Password divs in it.
I’m not sure what I have done wrong in the casting process. Please help.
What does your Auth Registration look like?
This is what the AuthConfig of the SocialBootstrapApi project looks like:
The
new CustomUserSession()is what tells ServiceStack to use a Custom Session and not the built-in defaultAuthUserSession.And what CacheClient are you using?