0

Overview:

  • I have a Outlook desktop add-in (extension) from which user can browse attachment.
  • We call web API(c#) to upload that attachment data into azure blob storage.
  • But I want that data to be encrypted (using AES with key and IV) into Outlook itself (That's working properly) and then pass to web API.

Questions:

  1. I tried passing data as a CryptoStream object. But It's not allowing to read CryptoStream. Is that possible to pass CryptoStream to web api ?
  2. Secondly I tried passing data as a byte[] (Byte Array) object. but that's again encrypted byte array. While HttpPostRequest to API model validation fails. Is that possible to pass encrypted byte array to web api.

HttpPostRequest:

var content = new MultipartFormDataContent();
 content.Add(new StringContent(encryptionKey.ToString()), nameof(ChannelMessageFilePostModel.EncryptionKey).ToCamelCase());
 content.Add(new ByteArrayContent(encryptedBytes), nameof(ChannelMessageFilePostModel.EncryptedBytes).ToCamelCase());
 HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, $"ControllerName/MethodName/{id}");
 request.Content = content;

 return InvokeApi<ChannelMessageFileModel>(request, cancellationToken);

Post method Web API:

public async Task < IActionResult > Post(string Id, FilePostModel model) {
 if (string.IsNullOrEmpty(Id) || model == null) {
  return BadRequest();
 }

 //todo: check that ModelState is working for array
 if (!ModelState.IsValid) {
  return BadRequest(ModelState);
 }
}
}

Here It fails on ModelState for model.encryptedBytes that is type of byte[].

Gopal Zadafiya
  • 274
  • 2
  • 10
  • 1
    Can't you base64_encode that stream/bytes and pass that to the API? Also please limit the questions down to one specific issue you are facing. To answer your questions, #1 - no, #2 - yes and #3 - IMHO it's opinionated and varies depending on setup and configurations. – Trevor Aug 23 '19 at 14:02
  • 2
    Base-64 encode the encrypted bytes. – Kenneth K. Aug 23 '19 at 14:02
  • Option 2 should just work and is the most efficient. Why it fails, totally depends on what you have configured your client to send and your API to receive. If you send it as form-data, you can just send binary data, no need for base64. See https://stackoverflow.com/questions/19954287/how-to-upload-file-to-server-with-http-post-multipart-form-data and https://stackoverflow.com/questions/16416601/c-sharp-httpclient-4-5-multipart-form-data-upload. Show relevant code if you want #2 solved. – CodeCaster Aug 23 '19 at 14:04
  • @CodeCaster, I have added code of post request – Gopal Zadafiya Aug 23 '19 at 14:19
  • @KennethK., I found ways to convert to Base64String only, Should I convert byte[] to base64 - encoded bytes[] ? – Gopal Zadafiya Aug 23 '19 at 14:23
  • So how does that code fail? Looks good to me. – CodeCaster Aug 23 '19 at 14:24
  • So show what your receive... Show the actual model error... Show what actually gets sent, for example using Fiddler... – CodeCaster Aug 23 '19 at 14:45

0 Answers0