我正在探索使用ServiceStack作为WCF的替代方案。 我的要求之一是服务器和客户端必须相互使用证书进行身份验证。 客户端是一个服务,因此我不能使用任何涉及用户输入的身份验证类型。 此外,客户端需要能够在Linux上运行,因此Windows身份验证不可用。
我已经使用netsh.exe将我的服务器证书绑定到服务器端口,使用wireshark验证了客户端获取服务器证书并加密数据。 但是,我无法弄清如何配置服务器以要求客户端证书。
一些人建议使用请求过滤器来验证客户端证书,但这似乎非常低效,因为每个请求都会检查客户端证书。 性能是非常重要的。 创建自定义IAuthProvider似乎很有前途,但所有文档和示例都面向某些时候涉及用户交互而不是证书的身份验证类型。
关于此主题请参考以下链接:https://github.com/ServiceStack/ServiceStack/wiki/Authentication-and-authorization 在自托管的ServiceStack服务中是否可以使用证书来相互认证客户端和服务器?
以下是我测试服务的代码:
我已经使用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));
}
}