开发者

Encryption/Decryption through DelegatingChannel (MessageHandler)

开发者 https://www.devze.com 2023-03-23 12:03 出处:网络
I am trying to use DelegatingChannel (MessageHandler) to decrypt the incoming message and encrypt the outgoing message. Code looks like this, but with some placeholder i couldn\'t figure out how to ac

I am trying to use DelegatingChannel (MessageHandler) to decrypt the incoming message and encrypt the outgoing message. Code looks like this, but with some placeholder i couldn't figure out how to achieve.

    protected override Task<HttpResponseMessage> SendAsync(
    HttpRequestMessage request, CancellationToken cancellationToken)
{
    var inEncryptedMessage = request.Content.ReadAsOrDefault<EncryptedMessage>();

    Message inMessage = inEncryptedMessage.Decrypt();

    var newContent = new StringContent(inMessage.Data, Encoding.UTF8, "text/json");

    newContent.Headers.ContentType = request.Content.Headers.ContentType;

    request.Content = newContent;            

    return base.SendAsync(request, cancellationToken).ContinueWith(
        task =>
        {
            HttpResponseMessage response = task.Result;

            // need to serialize the data in response.Content as json
            var outMessage = new Message {
                Data = ... // but don't know how
            };

            var outEncryptedMessage = outMessage.Encrypt();

            response.Content = new ObjectContent(outEncryptedMessage);

            return response;
        });
}



public class Message
{
    public string Data { 开发者_如何学Cget; set; }
}

public class EncryptedMessage
{
    public byte[] Key { get; set; }
    public byte[] Message { get; set; }
}

I want to pass JSON string in the request to the Operation, and therein deserialise the JSON into object. But failed... But object was created, but all the properties are null.

So which stoped me to think about response aspect. But i still find the difficulties in read object from response.Content.

Please give me some suggestion if you have the answer.

Thanks a lot, M


If I'm not wrong you could try to implement a DecryptOperationHandler : HttpOperationHandler<HttpRequestMessage>, <HttpRequestMessage>

This should decrypt your incoming message before passing it to the resource itself

Encryption could be done using a EncryptOperationHandler : HttpOperationHandler<HttpResponseMessage>, <HttpResponseMessage> which gets executed after your resource method has been executed

0

精彩评论

暂无评论...
验证码 换一张
取 消