问题描述
我使用ServiceStack认证与ServiceStack.OrmLite(SqlServer的)。我有不同的用户类型,如学生,教师,校长与其他数据库表。所有用户类型都会有关系。
什么是实现它的最佳做法是什么?
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)中添加必要的字段USERAUTH表,使黑色魔术。(创建USERAUTH后添加必要的额外信息表手动)
A) Add necessary fields to UserAuth table and make Black Magic.(After creation of UserAuth add necessary extra informations to table Manually)
B)学生:USERAUTH(在USERAUTH所有字段将被克隆)
B) Student:UserAuth (All fields in UserAuth will be cloned)
C)Student.UserAuthId,UserAuth.Meta [UserType.Student,StudentId]相互参照
C) Student.UserAuthId, UserAuth.Meta[UserType.Student,StudentId] mutual-reference
D)扩展Xservice的,XFeature或??
D) Extend XService, XFeature or ??
PS:怎么样对所有者和类型字段添加到ServiceStack USERAUTH表
PS: How about to add Owner and Type fields to ServiceStack UserAuth table.
推荐答案
有几个策略,以额外的元数据附加到 USERAUTH 和 UserAuthDetails 表,
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) { }
}
扩展UserAuthSession用自己的类型化的自定义会话
在扩展,并提供一个类型化的同时,自定义是推荐的方式,因为它是由的因为用户会话只是获取一个的(即不是在RDBMS),其中它的模式少持久性的特点,很容易支持扩展类型。
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.
对于轻微的扩展,你可以使用元字符串的字典上是专门用来支持自定义元数据持有的每个表的字段。它们还包括有用获取LT; T&GT;
和设置&LT; T&GT;
这也支持污点复杂类型的方法:
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>();
链接与REFID和RefIdStr领域参考数据
的 USERAUTH
和 UserOAuthProvider
表还包括诠释? REFID
和字符串RefIdStr
,你可以用它来引用相同的对每个用户验证记录或用户的OAuth注册自己的自定义表的外部数据的字段。
Linking referential data with RefId and RefIdStr fields
The UserAuth
and UserOAuthProvider
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.
另一个选择是做什么的正在做并提取USERAUTH信息到自己的通过覆盖定制表在 OnAuthenticated 钩自己的自定义UserSession其中获得的每一个用户认证成功时调用。
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.
这里的会话数据复制到一个自定义用户POCO并将其保存在不同的表SocialBootstrapApi例如。
public class CustomUserSession : AuthUserSession
{
public string CustomId { get; set; }
public override void OnAuthenticated(IServiceBase authService, IAuthSession session,
IOAuthTokens 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
authService.TryResolve<IDbConnectionFactory>().Exec(dbCmd => dbCmd.Save(user));
}
}
这篇关于如何延长ServiceStack认证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!