I have implemented a custom CredentialsAuthProvider
for my authentication and used it with the default in memory session storage.
Now I tried to change the session storage to Redis and added this to my Configure()
method in the AppHost
:
container.Register<IRedisClientsManager>(c =>
new PooledRedisClientManager("localhost:6379"));
container.Register<ICacheClient>(c => (ICacheClient)c
.Resolve<IRedisClientsManager>()
.GetCacheClient()).ReusedWithin(Funq.ReuseScope.None);
Now when I authenticate, I can see that a key with urn:iauthsession:...
is added to my Redis server. But all routes with the [Authenticate]
attribute give a 401 Unauthorized
error.
The CustomCredentialsAuthProvider
is implemented like this:
public class CustomCredentialsAuthProvider : CredentialsAuthProvider
{
public override bool TryAuthenticate(IServiceBase authService, string userName, string password)
{
if (userName != string.Empty && password != string.Empty)
{
// Database call ...
var session = (CustomSession)authService.GetSession();
session.ClientId = login.ClientId;
// Fill session...
authService.SaveSession(session, SessionExpiry);
return true;
}
return false;
}
}
ServiceStack Version: 3.9.71
EDIT :
I tried to override the CredentialsAuthProvider
IsAuthorized
method but without success.
But I'm inheriting my session object from AuthUserSession
, which also has a IsAuthorized
method. When I return true from this method the Redis session does work with the Authenticate
Attribute.
public class CustomSession : AuthUserSession
{
public int ClientId { get; set; }
...
public override bool IsAuthorized(string provider)
{
return true;
}
}
I couldn't figure out a way to get the
[Authenticate]
Attribute to work with Redis storage.I had to write a custom
[SessionAuth]
AttributeIn my AppHost Configure() method I just register the SessionFeature and the IRedisClientsManager/ICacheClient:
The CustomSession class inherits from AuthUserSession:
And I have a normal service route on /login/auth for the authentication part and a /login/logout route to remove the session:
}
I'm still interested in a solution that works with the normal
[Authenticate]
Attribute.