如何扩展 ServiceStack 身份验证 [英] How can I extend ServiceStack Authentication

查看:27
本文介绍了如何扩展 ServiceStack 身份验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将 ServiceStack 身份验证与 ServiceStack.OrmLite (SqlServer) 结合使用.我有不同的用户类型,如学生、教师、校长......所有用户类型都将与其他数据库表相关.实施它的最佳做法是什么?

A) 在 UserAuth 表中添加必要的字段并制作 Black Magic.(创建 UserAuth 后手动添加必要的额外信息到表中)

B) Student:UserAuth(UserAuth 中的所有字段都将被克隆)

C) Student.UserAuthId, UserAuth.Meta[UserType.Student,StudentId] 相互引用

D) 扩展 XService、XFeature 或 ??

PS:如何将 Owner 和 Type 字段添加到 ServiceStack UserAuth 表中.

解决方案

有几种策略可以将额外的元数据附加到 UserAuthUserAuthDetails 表,

如果你想扩展 UserAuth 的架构 使用您自己的自定义 POCO,您需要对 OrmLiteAuthRepository 类进行子类化,包括您的自定义 POCO,例如,请参阅 OrmLiteAuthRepository:

公共类 OrmLiteAuthRepository: OrmLiteAuthRepository, IUserAuthRepository{公共 OrmLiteAuthRepository(IDbConnectionFactory dbFactory):基地(dbFactory){}}

使用您自己键入的自定义会话扩展 UserAuthSession

同时扩展和提供类型化的、自定义的AuthUserSession 是推荐的方法,因为 ServiceStack 的 身份验证功能,因为用户会话刚刚在 缓存提供者(即不在 RDBMS 中),其无模式持久性特征,轻松支持扩展类型.

向元字典字段添加额外的元数据

对于次要扩展,您可以使用每个表上的 Meta 字符串字典字段,这些字段是专门为支持自定义保留的元数据而添加的.它们还包括有用的GetSet 方法,它们也支持blob 复杂类型:

userAuth.Set(new Address { ... });var address = userAuth.Get<地址>();

将参考数据与 RefId 和 RefIdStr 字段关联

UserAuthUserAuthDetails 表还包括一个 int?RefIdstring RefIdStr 字段,您可以使用它们来引用外部数据,例如针对每个用户身份验证记录或用户 OAuth 注册的自定义表.

将 UserAuth 信息提取到您自己的自定义表中

另一种选择是执行 SocialBootstrapApi 示例演示 正在执行的操作,并将 UserAuth 信息提取到您的通过覆盖您自己的自定义 UserSession 中的 OnAuthenticated 钩子来拥有自定义表,每次用户成功进行身份验证时都会调用该钩子.

这是 SocialBootstrapApi 示例 将会话数据复制到自定义用户 POCO 并将其保存在不同的表中.

public class CustomUserSession : AuthUserSession{公共字符串 CustomId { 获取;放;}公共覆盖无效 OnAuthenticated(IServiceBase authService, IAuthSession session,IAuthTokens 令牌,Dictionary身份信息){base.OnAuthenticated(authService, session, tokens, authInfo);//将此会话中的所有匹配字段填充到您自己的自定义用户表中var user = session.TranslateTo();user.Id = int.Parse(session.UserAuthId);user.GravatarImageUrl64 = !session.Email.IsNullOrEmpty()?CreateGravatarUrl(session.Email, 64): 空值;//从IOC解析DbFactory并持久化用户信息使用 (var db = authService.TryResolve<IDbConnectionFactory>().Open()){db.Save(用户);}}}

I'm using ServiceStack Authentication with ServiceStack.OrmLite (SqlServer). I have different User types like Student, Teacher, Principal.. All user types will have relation with other Db Tables. What is the best practise to implement it?

A) Add necessary fields to UserAuth table and make Black Magic.(After creation of UserAuth add necessary extra informations to table Manually)

B) Student:UserAuth (All fields in UserAuth will be cloned)

C) Student.UserAuthId, UserAuth.Meta[UserType.Student,StudentId] mutual-reference

D) Extend XService, XFeature or ??

PS: How about to add Owner and Type fields to ServiceStack UserAuth table.

解决方案

There are a couple of strategies to append additional metadata to the UserAuth and UserAuthDetails tables,

If you want to extend the schema of UserAuth with your own custom POCO's you need to subclass OrmLiteAuthRepository<T,T> class including your custom POCO's, e.g see the source for OrmLiteAuthRepository:

public class OrmLiteAuthRepository 
    : OrmLiteAuthRepository<UserAuth, UserAuthDetails>, IUserAuthRepository
{
    public OrmLiteAuthRepository(IDbConnectionFactory dbFactory) 
        : base(dbFactory) { }
}

Extend UserAuthSession with your own typed Custom Session

At the same time extending and providing a typed, custom AuthUserSession is the recommended approach as it's supported by ServiceStack's Authentication Feature since the Users Session just gets blobbed in a Caching Provider (i.e. not in an RDBMS) where its schema-less persistance characteristics, easily supports extended types.

Adding additional metadata to the Meta dictionary fields

For minor extensions you can use the Meta string dictionaries fields on each table which were added specifically to support custom-held metadata. They also include useful Get<T> and Set<T> methods which also support blobbing complex types:

userAuth.Set(new Address { ... });
var address = userAuth.Get<Address>();

Linking referential data with RefId and RefIdStr fields

The UserAuth and UserAuthDetails tables also include an int? RefId and a string RefIdStr fields which you can use to reference external data like your own custom tables against each User Auth record or User OAuth registration.

Extracting UserAuth info into your own custom tables

Another option is to do what the SocialBootstrapApi example demo is doing and extract the UserAuth info into your own custom tables by overriding the OnAuthenticated hook in your own custom UserSession which get's called each time a user successfully authenticates.

Here's the SocialBootstrapApi example of copying the session data into a custom user POCO and saving it in a different table.

public class CustomUserSession : AuthUserSession
{
    public string CustomId { get; set; }

    public override void OnAuthenticated(IServiceBase authService, IAuthSession session, 
        IAuthTokens tokens, Dictionary<string, string> authInfo)
    {
        base.OnAuthenticated(authService, session, tokens, authInfo);

        //Populate all matching fields from this session to your own custom User table
        var user = session.TranslateTo<User>();
        user.Id = int.Parse(session.UserAuthId);
        user.GravatarImageUrl64 = !session.Email.IsNullOrEmpty()
            ? CreateGravatarUrl(session.Email, 64)
            : null;

        //Resolve the DbFactory from the IOC and persist the user info
        using (var db = authService.TryResolve<IDbConnectionFactory>().Open())
        {
            db.Save(user);
        }
    }
}

这篇关于如何扩展 ServiceStack 身份验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆