自定义ServiceStack身份验证

时间:2014-05-09 09:59:38

标签: servicestack

我已阅读文档并成功实施了如下所示的自定义身份验证层:

public class SmartLaneAuthentication : CredentialsAuthProvider
{
    private readonly SmartDBEntities _dbEntities;

    public SmartLaneAuthentication(SmartDBEntities dbEntities)
    {
        _dbEntities = dbEntities;
    }

    public override bool TryAuthenticate(IServiceBase authService, string userName, string password)
    {
        var user = _dbEntities.Users.FirstOrDefault(x => !((bool)x.ActiveDirectoryAccount) && x.UserName == userName);
        if (user == null) return false;

        // Do my encryption, code taken out for simplicity

        return password == user.Password;
    }

    public override void OnAuthenticated(IServiceBase authService, IAuthSession session, IOAuthTokens tokens, Dictionary<string, string> authInfo)
    {
        // user should never be null as it's already been authenticated
        var user = _dbEntities.Users.First(x => x.UserName == session.UserAuthName);
        var customerCount = _dbEntities.Customers.Count();
        session.UserName = user.UserName;
        session.DisplayName = user.DisplayName;
        session.CustomerCount = customerCount; // this isn't accessible?

        authService.SaveSession(session, SessionExpiry);
    }
}

然后我在AppHost中注册它:

Plugins.Add(new AuthFeature(() => new SmartLaneUserSession(), 
    new IAuthProvider[]
    {
        new SmartLaneAuthentication(connection)
    })
{
    HtmlRedirect = null
});

Plugins.Add(new SessionFeature()); 

请注意我使用下面的SmartLaneUserSession,其中添加了名为CustomerCount的自定义属性:

public class SmartLaneUserSession : AuthUserSession
{
    public int CustomerCount { get; set; }
}

当我尝试访问此属性以在OnAuthenticated类的SmartLaneAuthentication方法中设置它时,它无法访问。当用户登录时,我如何访问和设置此属性?

1 个答案:

答案 0 :(得分:4)

OnAuthenticated方法中,您需要将session(类型为IAuthSession)强制转换为会话对象类型,例如:

...
var customerCount = _dbEntities.Customers.Count();
var smartLaneUserSession = session as SmartLaneUserSession;
if(smartLaneUserSession != null)
{
    smartLaneUserSession.UserName = user.UserName;
    smartLaneUserSession.DisplayName = user.DisplayName;
    smartLaneUserSession.CustomerCount = customerCount; // Now accessible

    // Save the smartLaneUserSession object
    authService.SaveSession(smartLaneUserSession, SessionExpiry);
}

在您的服务中,您可以使用SessionAs<T>方法访问会话。所以在你的情况下你可以使用:

public class MyService : Service
{
    public int Get(TestRequest request)
    {
        var session = SessionAs<SmartLaneUserSession>();
        return session.CustomerCount;
    }
}