【问题标题】:what is the proper way to use EFx to authenticate in ServiceStack?在 ServiceStack 中使用 EFx 进行身份验证的正确方法是什么?
【发布时间】:2014-03-30 20:28:05
【问题描述】:

我们最近在与 MVC 4 网页服务器相同的解决方案中为我们的 ReSTful Web 服务切换到 ServiceStack,到目前为止,我们发现它很容易使用。但是,我们的问题是我们想要添加基本身份验证,并且每篇关于该主题的文章和博客都使它看起来比它应该使其他一切变得如此简单的工具更复杂和“棘手”(他们的话)。我们已经在数据库中拥有“基本 xxx”字符串,我们使用实体框架通过 DAL 模式访问该字符串。我们可以将“Authenticate”标头中的值与我们的数据库值进行比较,但这是粗略的方式。我们不想加载另一个库(例如 OrmLite)或创建额外的表。

我的问题是......鉴于我已经说过关于我们的实施,有没有一种简单的方法可以在正确的地点/时间说“这是我们存储的'基本 xxx' 字符串”?或者我们是否必须覆盖 IUserAuthRepository 并在 IAuthProvider 的覆盖中使用它?这似乎很简单,直到您看到网络上可用的其他实现然后您都感到困惑。

提前感谢您的任何回复! 马库斯

【问题讨论】:

    标签: entity-framework authentication servicestack


    【解决方案1】:

    实现你自己的 Basic AuthProvider

    您只需要继承BasicAuthProvider 并用您自己的实现覆盖TryAuthenticate 方法,例如:

    public class MyBasicAuthProvider : BasicAuthProvider
    {
        public override bool TryAuthenticate(IServiceBase authService, 
           string userName, string password)
        {
            return MyIsValidLogin(userName, password);
        }
    }
    

    然后在注册 AuthFeature 时将其提供给 ServiceStack,例如:

    Plugins.Add(new AuthFeature(
        () => new CustomUserSession(), //Use your own typed Custom UserSession type
        new IAuthProvider[] {
          new MyBasicAuthProvider()
        });
    

    这插入到 ServiceStack 的内置身份验证中,让您使用 HTTP BasicAuth 进行身份验证并使用内置的[Authenticate] 属性保护您的服务。

    使用内置的 ServiceClient 发送基本身份验证

    ServiceStack .NET Service Clients 通过设置用户名/密码字段内置了对基本身份验证请求的支持,即:

    var client = new JsonServiceClient { 
        UserName = username,
        Password = password
    };
    

    现在,当遇到未经身份验证的请求时,将使用 BasicAuth 凭据自动重新发送请求。要始终在您可以设置的每个请求上发送 BasicAuth:

    client.AlwaysSendBasicAuthHeader = true;
    

    使用全局请求过滤器手动验证 BasicAuth

    使用 ServiceStack 内置身份验证的替代方法是使用全局请求过滤器,该过滤器手动提取 BasicAuth 用户名和密码,并在请求上设置一个标志以指示请求已通过身份验证,例如:

    this.GlobalRequestFilters.Add((req, res, dto) => {
        var userAndPass = req.GetBasicAuthUserAndPassword();
        if (userAndPass == null)
            return;
    
        var username = userAndPass.Value.Key;
        var password = userAndPass.Value.Value;
    
        if (MyIsValidLogin(username, password)) {
            //Set a flag that will be available for the entire request:
            req.Items["UserAuthenticatedWithBasicAuth"] = username;  
        }
    });
    

    现在ServiceStack's Request pipeline 中的所有服务、属性过滤器和任何其他自定义逻辑都可以检查此标志以查看它是否已设置,例如:

    public class MyServices : Service
    {
        public object Any(Request request)
        {
            var authenticatedUser = Request.Items["UserAuthenticatedWithBasicAuth"];
            if (authenticatedUser != null) {
                //this user was authenticated with BasicAuth
            }
        }
    }
    

    【讨论】:

    • 在等待您的回复时,我尝试了几篇我发现的更有希望的帖子。这看起来像是你做的另一篇我正在考虑的帖子。我非常感谢您的反馈!你为我节省了很多时间!
    猜你喜欢
    • 1970-01-01
    • 2014-10-10
    • 2020-06-27
    • 2019-04-23
    • 2020-02-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-29
    相关资源
    最近更新 更多