5
votes

I have a self hosted service stack app in which I'm using Facebook Oath and Redis - the Facebook and redis side of things seem to be working ie. when I visit

abc.com/auth/facebook 

The custom user session gets populated in OnAuthenticated method. The Redis cache has the data persisted correctly..so far so good

The problem Im having is understanding how to retrieve this CustomUserSession in a subsequent request. To begin with the oauth redirect page "/About-Us" is where I want to retrieve the session value however it is always null

[DataContract]
public class CustomUserSession : AuthUserSession
{      
    [DataMember]
    public string CustomId { get; set; }

    public override void OnAuthenticated(IServiceBase authService, IAuthSession session, IAuthTokens tokens, Dictionary<string, string> authInfo)
    {
        // receiving session id here and can retrieve from redis cache
    }
    public override bool IsAuthorized(string provider)
    {
        // when using the [Authenticate] attribute - this Id is always
        // a fresh value and so doesn't exist in cache and cannot be auth'd
        string sessionKey = SessionFeature.GetSessionKey(this.Id);

        cacheClient = ServiceStackHost.Instance.TryResolve<ICacheClient>();
        CustomUserSession session = cacheClient.Get<CustomUserSession>(sessionKey);

        if (session == null)
        {
            return false;
        }
        return session.IsAuthenticated;
    }
}


[DefaultView("AboutUs")]
public class AboutUsService : AppServiceBase
{
    public object Get(AboutUsRequest request)
    {
        var sess = base.UserSession;
        return new AboutUsResponse
        {
            //custom Id is always null?? 
            Name = sess.CustomId
        };
    }
}
public abstract class AppServiceBase : Service
{
    protected CustomUserSession UserSession
    {
        get
        {
            return base.SessionAs<CustomUserSession>();
        }
    }
}

How I register the cache & session etc.

        AppConfig = new AppConfig(appSettings);
        container.Register(AppConfig);
        container.Register<IRedisClientsManager>(c => new PooledRedisClientManager("10.1.1.10:6379"));
        container.Register(c =>
              c.Resolve<IRedisClientsManager>().GetCacheClient());
        ConfigureAuth(container, appSettings);

the contents of ConfigureAuth()

        var authFeature = new AuthFeature(
            () => new CustomUserSession(),
            new IAuthProvider[]
            {
                new FacebookAuthProvider(appSettings), // override of BasicAuthProvider
            }
            ) {HtmlRedirect = null, IncludeAssignRoleServices = false};

        Plugins.Add(authFeature);

I feel I'm missing something obvious here.... thanks in advance

1

1 Answers

0
votes

To register to use a CustomUserSession for your typed Sessions, it needs to be specified when you register the AuthFeature, e.g:

Plugins.Add(new AuthFeature(() => new CustomUserSession(), ...));