自定义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下面使用“ like”,在其中添加了一个名为的自定义属性CustomerCount

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

当我尝试访问该属性以在OnAuthenticated方法中对其进行设置时,将SmartLaneAuthentication无法访问它。用户登录后如何访问和设置此属性?

史考特

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

使用ServiceStack返回自定义身份验证响应

来自分类Dev

ServiceStack自定义用户身份验证

来自分类Dev

ServiceStack-自定义身份验证响应

来自分类Dev

Laravel自定义身份验证

来自分类Dev

Laravel自定义身份验证

来自分类Dev

JMeter自定义身份验证

来自分类Dev

Django身份验证自定义

来自分类Dev

在ServiceStack中注册自定义凭据身份验证提供程序

来自分类Dev

ArgumentNullException与ServiceStack中的自定义身份验证提供程序

来自分类Dev

如何将我的自定义ServiceStack身份验证提供程序与Redis一起使用?

来自分类Dev

具有数据库存储的Api密钥的ServiceStack自定义凭据身份验证

来自分类Dev

跳过自定义错误页面的身份验证

来自分类Dev

星号SIP自定义身份验证

来自分类Dev

使用自定义身份验证修改请求参数

来自分类Dev

Firebase自定义身份验证传递令牌

来自分类Dev

.NET MVC中Elmah的自定义身份验证

来自分类Dev

Web API 2自定义身份验证

来自分类Dev

自定义Spring身份验证中的角色访问

来自分类Dev

Laravel自定义身份验证过滤器

来自分类Dev

Meteor.js中的自定义身份验证

来自分类Dev

未调用自定义身份验证提供程序

来自分类Dev

自定义devise api令牌身份验证

来自分类Dev

邮递员自定义身份验证

来自分类Dev

来自自定义表的Spring Security身份验证

来自分类Dev

Swagger自定义身份验证标头

来自分类Dev

SSRS 2016的自定义身份验证(安全扩展)

来自分类Dev

OWIN身份验证和自定义响应

来自分类Dev

在身份验证上发送自定义参数

来自分类Dev

Wildfly自定义身份验证方法