Domanda

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.

È stato utile?

Soluzione

What does your Auth Registration look like?

This is what the AuthConfig of the SocialBootstrapApi project looks like:

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
    }));

The new CustomUserSession() is what tells ServiceStack to use a Custom Session and not the built-in default AuthUserSession.

And what CacheClient are you using?

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top