我正在探索使用 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));
}
}