如何使用ServiceStack中的证书对客户端进行身份验证?

时间:2014-07-15 01:58:28

标签: c# servicestack client-certificates mutual-authentication

我正在探索使用ServiceStack作为WCF的替代方案。我的一个要求是服务器和客户端必须使用证书进行相互身份验证。客户端是一项服务,因此我无法使用涉及用户输入的任何类型的身份验证。此外,客户端需要能够使用单声道在Linux上运行,因此Windows身份验证已经完成。

我使用netsh.exe将服务器证书绑定到服务器端口,验证客户端获取服务器证书并使用wireshark加密数据。但是我不能为我的生活弄清楚如何配置服务器以要求客户端证书。

有些人建议使用请求过滤器来验证客户端证书,但这似乎非常低效,因为每个请求都会检查客户端证书。性能是非常重要的。创建自定义IAuthProvider似乎很有希望,但所有文档和示例都面向涉及用户交互的身份验证类型,而不是证书。

https://github.com/ServiceStack/ServiceStack/wiki/Authentication-and-authorization

是否可以使用证书通过自托管ServiceStack服务相互验证客户端和服务器?

这是我的测试服务供参考。

public class Host : AppHostHttpListenerBase
{
    public Host()
        : base("Self-hosted thing", typeof(PutValueService).Assembly)
    {
        //TODO - add custom IAuthProvider to validate the client certificate?
        this.RequestFilters.Add(ValidateRequest);

        //add protobuf plugin
        //https://github.com/ServiceStack/ServiceStack/wiki/Protobuf-format
        Plugins.Add(new ProtoBufFormat());

        //register protobuf
        base.ContentTypeFilters.Register(ContentType.ProtoBuf,
                (reqCtx, res, stream) => ProtoBuf.Serializer.NonGeneric.Serialize(stream, res),
                ProtoBuf.Serializer.NonGeneric.Deserialize);
    }

    public override void Configure(Funq.Container container)
    {}

    void ValidateRequest(IHttpRequest request, IHttpResponse response, object dto)
    {
        //TODO - get client certificate?
    }
}

[DataContract]
[Route("/putvalue", "POST")]
//dto
public class PutValueMessage : IReturnVoid
{
    [DataMember(Order=1)]
    public string StreamID { get; set; }

    [DataMember(Order=2)]
    public byte[] Data { get; set; }
}

//service
public class PutValueService : Service
{
    public void Any(PutValueMessage request)
    {
        //Comment out for performance testing

        Console.WriteLine(DateTime.Now);
        Console.WriteLine(request.StreamID);
        Console.WriteLine(Encoding.UTF8.GetString(request.Data));
    }
}

1 个答案:

答案 0 :(得分:10)

  

有些人建议使用请求过滤器来验证客户端证书,但这似乎非常低效,因为每个请求都会检查客户端证书。性能是非常重要的。

REST是无状态的,因此如果您不愿意在每个请求上检查客户端证书,则需要提供备用身份验证令牌以显示已经提供的有效身份。

因此,您可以避免在后续请求中检查证书,如果在验证客户端证书后,您向客户端提供可以验证的会话ID cookie。

  

但是,我不能为我的生活找出如何配置服务器以要求客户端证书。

客户端证书仅在原始http请求对象上可用,这意味着您必须转换请求对象才能访问此值。下面的代码用于将请求转发给自托管应用程序使用的ListenerRequest

服务器进程:

请求过滤器将检查:

  • 首先是有效的会话cookie,如果有效会允许请求而不进一步处理,因此不需要在后续请求中验证客户端证书。

  • 如果未找到有效会话,则过滤器将尝试检查客户端证书的请求。如果存在则尝试根据某些条件匹配它,并在接受时,为客户端创建会话,并返回cookie。

  • 如果客户端证书不匹配则抛出授权例外。

GlobalRequestFilters.Add((req, res, requestDto) => {

    // Check for the session cookie
    const string cookieName = "auth";
    var sessionCookie = req.GetCookieValue(cookieName);
    if(sessionCookie != null)
    {
        // Try authenticate using the session cookie
        var cache = req.GetCacheClient();
        var session = cache.Get<MySession>(sessionCookie);
        if(session != null && session.Expires > DateTime.Now)
        {
            // Session is valid permit the request
            return;
        }
    }

    // Fallback to checking the client certificate
    var originalRequest = req.OriginalRequest as ListenerRequest;
    if(originalRequest != null)
    {
        // Get the certificate from the request
        var certificate = originalRequest.HttpRequest.GetClientCertificate();

        /*
         * Check the certificate is valid
         * (Replace with your own checks here)
         * You can do this by checking a database of known certificate serial numbers or the public key etc.
         * 
         * If you need database access you can resolve it from the container
         * var db = HostContext.TryResolve<IDbConnection>();
         */

        bool isValid = certificate != null && certificate.SerialNumber == "XXXXXXXXXXXXXXXX";

        // Handle valid certificates
        if(isValid)
        {
            // Create a session for the user
            var sessionId = SessionExtensions.CreateRandomBase64Id();
            var expiration = DateTime.Now.AddHours(1);

            var session = new MySession {
                Id = sessionId,
                Name = certificate.SubjectName,
                ClientCertificateSerialNumber = certificate.SerialNumber,
                Expires = expiration
            };

            // Add the session to the cache
            var cache = req.GetCacheClient();
            cache.Add<MySession>(sessionId, session);

            // Set the session cookie
            res.SetCookie(cookieName, sessionId, expiration);

            // Permit the request
            return;
        }
    }

    // No valid session cookie or client certificate
    throw new HttpError(System.Net.HttpStatusCode.Unauthorized, "401", "A valid client certificate or session is required");
});

这使用了一个名为MySession的自定义会话类,您可以根据需要将其替换为您自己的会话对象。

public class MySession
{
    public string Id { get; set; }
    public DateTime Expires { get; set; }
    public string Name { get; set; }
    public string ClientCertificateSerialNumber { get; set; }
}

客户流程:

客户端需要将其客户端证书设置为随请求一起发送。

var client = new JsonServiceClient("https://servername:port/");
client.RequestFilter += (httpReq) => {
    var certificate = ... // Load the client certificate
    httpReq.ClientCertificates.Add( certificate );
};

一旦您向服务器发出第一个请求,您的客户端将收到会话ID cookie,您可以选择删除客户端证书,直到会话失效。

我希望有所帮助。