ServiceStack自定义用户身份验证 [英] ServiceStack Custom User Authentication

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

问题描述

有没有人有一个如何使用INT REFID在本<提议一个实际的例子href=\"http://stackoverflow.com/questions/11117469/how-can-i-extend-servicestack-authentication/37667766#37667766\">question?

Does anyone have an actual example of how to use int RefId as proposed in this question?

我想获得认证会,但我需要用我自己的用户表嫁给了USERAUTH数据。问题是我不知道如何将附加参数传递给/注册的方法。我想我在寻找像OnAddUser,这将允许我一些额外的参数扔进组合的事件。

I am trying to get authentication going but I need to marry up the userAuth data with my own user table. The problem is I have no idea how to pass an additional parameter to the "/register" method. I guess I'm looking for an event like "OnAddUser" which will allow me to throw some additional parameters into the mix.

我设法迅速得到了用户注册工作pretty,这是超级简单。也许问题是,这是太容易了?我可以看到它的工作,但我无法弄清楚如何将它与数据库之间得到的。

I managed to get the user registration working pretty quickly, it was super easy. Maybe the problem is that it was too easy? I can see it work but I can't figure out how to get between it and the database.

无论是字典方法或REFID方法可能会工作对我来说,它只是没有明显的对我有多要么使用。

Either the dictionary approach or the RefId approach will probably work for me, it's just no obvious to me how use either.

是否可以覆盖共创用户?我发现这个code:

Is it possible to override the create user altogether? I found this code:

MyServices.cs

MyServices.cs

它看起来像它做的创建用户代替/注册,但是有一些建议,你不能覆盖ServiceStack DTO的其他一些文章,你必须使用默认的表。

which looks like it's doing the create user in place of "/register" but there are some other articles that suggest that you can't override the ServiceStack DTOs, you have to use the default tables.

推荐答案

您可以通过使用的<一个副本,包括你自己的注册服务href=\"https://github.com/ServiceStack/ServiceStack/blob/master/src/ServiceStack/Auth/RegisterService.cs\"相对=nofollow> RegisterService 源$ C ​​$ c和修改,以满足您的需求,例如,使用自定义的注册DTO ,提供你想要的附加属性。

You could include your own Register Service by using a copy of the RegisterService source code and modify it to suit your needs, e.g. Use a custom Register DTO with the additional properties you want.

但你可以很容易地将它添加到查询字符串,你可以你的服务内部访问与传不改变现有注册DTO额外params:一个?

But you can easily pass additional params without changing the existing Register DTO by adding it to the ?querystring which you can access inside your Services with:

var myParam = base.Request.QueryString["myParam"];

否则在注册过程中添加您的定制逻辑或认证是挖掘到现有的 Session或验证活动的。

TechStacks在其<一个一个的这个例子href=\"https://github.com/ServiceStackApps/TechStacks/blob/master/src/TechStacks/TechStacks.ServiceInterface/CustomAuthUserSession.cs\"相对=nofollow> CustomAuthUserSession :

TechStacks has an example of this in its CustomAuthUserSession:

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

    public string GithubProfileUrl { get; set; }
    public string TwitterProfileUrl { get; set; }

    public override void OnAuthenticated(IServiceBase authService, 
        IAuthSession session, 
        IAuthTokens tokens, 
        Dictionary<string, string> authInfo)
    {
        base.OnAuthenticated(authService, session, tokens, authInfo);
        var appSettings = authService.TryResolve<IAppSettings>();
        var userAuthRepo = authService.TryResolve<IAuthRepository>();
        var userAuth = userAuthRepo.GetUserAuth(session, tokens);
        var dbConnectionFactory = authService.TryResolve<IDbConnectionFactory>();
        foreach (var authTokens in session.ProviderOAuthAccess)
        {
            if (authTokens.Provider.ToLower() == "github")
            {
                GithubProfileUrl = session.GetProfileUrl();
            }
            if (authTokens.Provider.ToLower() == "twitter")
            {
                TwitterProfileUrl = session.GetProfileUrl();
                if (appSettings.GetList("TwitterAdmins").Contains(session.UserName) 
                    && !session.HasRole(RoleNames.Admin))
                {
                    userAuthRepo.AssignRoles(userAuth, roles:new[]{RoleNames.Admin});
                }
            }

            DefaultProfileUrl = GithubProfileUrl ?? TwitterProfileUrl;
            using (var db = dbConnectionFactory.OpenDbConnection())
            {
                var userAuthInstance = db.Single<CustomUserAuth>(x => 
                    x.Id == this.UserAuthId.ToInt());
                if (userAuthInstance != null)
                {
                    userAuthInstance.DefaultProfileUrl = this.DefaultProfileUrl;
                    db.Save(userAuthInstance);
                }
            }
        }
    }
}

这取时,他们通过GitHub上或Twitter登录用户的个人资料网址。将指定的管理角色给用户在 TwitterAdmins AppSetting ,这是分配管理权限已知Twitter用户的一种方式。最后,检索资料网址添加到 CustomUserAuth POCO表并保存。

Which fetches the Profile Url of the User when they login via GitHub or Twitter. Will assign the Admin role to users in the TwitterAdmins AppSetting, which is a way to assign admin rights to known Twitter users. Finally the retrieved Profile Url is added to the CustomUserAuth POCO Table and saved.

TechStacks告诉ServiceStack由<一个使用自己的CustomUserAuth表而不是href=\"https://github.com/ServiceStackApps/TechStacks/blob/a040f7e173230081a6f48e27a6740e19517d7a45/src/TechStacks/TechStacks/AppHost.cs#L74\"相对=nofollow>注册一个通用OrmLiteAuthRepository :

TechStacks tells ServiceStack to use its own CustomUserAuth table instead by registering a generic OrmLiteAuthRepository:

var authRepo = new OrmLiteAuthRepository<CustomUserAuth, UserAuthDetails>(dbFactory);
container.Register<IUserAuthRepository>(authRepo);
authRepo.InitSchema();

如果它现在将保存用户信息的 CustomUserAuth 而不是默认的 USERAUTH 表。

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

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