给定一个应用程序,需要为 oAuth 交换 ConsumerKey/ConsumerSecret 等信息
- server running "WebApi" over ServiceStack
- consumer is a desktop app
希望使用已经存在的公钥/私钥来加密消息
是否支持加密/解密消息,希望是透明的?即加密/解密是通过服务器上的过滤器处理的?
或
关于如何执行此操作的任何解决方法?
仅供引用 - 服务器已经需要 SSL 并且请求经过身份验证,但是注册是免费/自动的,因此需要加密
最佳答案
更新
Encrypted Messaging Feature发表于 v4.0.42现在为所有服务提供安全通道,为现在可以通过不安全的 HTTP 轻松发送和接收加密消息的客户提供保护。
ServiceStack 中内置的消息级加密没有已发布的支持目前,虽然这是我们正在考虑在未来添加的内容。下面是您今天可以尝试使用 ServiceStack 进行探索的方法示例(您需要进行更多的研发才能提出良好的可靠设计),并且由于这是一个 future 可能会发生变化的领域,因此它不是我们现在支持的用例,但这里有一些可以与 ServiceStack 一起使用的东西:
class EncryptedRequest
{
//Name of the request type
public string Type { get; set; }
//Serialized Request DTO using something like JSON
public string EncryptedBody { get; set; }
//optional: let server the private key that was used (if multiple)
public string PrivateKeyMd5Hash { get; set; }
}
class EncryptedResponse
{
//Name of the response type
public string Type { get; set; }
//Serialized Response DTO
public string EncryptedBody { get; set; }
//optional
public string PublicKeyMd5Hash { get; set; }
}
这是一个方便的CryptUtils wrapper这使得创建公钥/私钥以及能够使用它们加密/解密文本变得容易。
然后你可以有一个单一的服务,并做这样的事情:
class EncryptedService : Service
{
const string PublicKey = ...;
const string PrivateKey = ...;
EncryptedResponse Any(EncryptedRequest request)
{
var requestType = Type.GetType("{0}.{1}"
.Fmt(typeof(EncryptedRequest).Namespace, request.Type));
var json = CryptUtils.Decrypt(PrivateKey, request.EncryptedBody);
var requestDto = JsonSerializer.DeserializeFromString(json,requestType);
var responseDto = GetAppHost().Config.ServiceController
.Execute(requestDto, base.RequestContext);
return new EncryptedResponse {
Type = responseDto.GetType().Name,
EncryptedBody = CryptUtils.Encrypt(PublicKey, responseDto.ToJson()),
};
}
}
客户端还需要您将在带外交换的公钥/私钥对的副本。
关于c# - 通过 ServiceStack 加密消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16098839/