.NET MVC从JSON Uint8Array反序列化字节数组 [英] .NET MVC deserialize byte array from JSON Uint8Array
问题描述
我正在使用js-scrypt( https://github.com/tonyg/js-scrypt )在我的客户端Web应用程序上,在将密码发布到我的服务器端.NET MVC应用程序之前对哈希密码进行哈希和盐密封再次进行哈希和盐析。此JavaScript库将字节数组实现为JavaScript Uint8Arrays。如何让我的MVC控制器将我的JSON Uint8Array反序列化为byte []?
I'm using js-scrypt (https://github.com/tonyg/js-scrypt) on my client-side web application to hash and salt passwords before posting them to my server-side .NET MVC application to be hashed and salted again. This JavaScript library implements byte arrays as JavaScript Uint8Arrays. How do I get my MVC Controller to deserialize my JSON Uint8Array to a byte[]?
JavaScript示例:(AJAX.Post是我写的库,myUint8Array正确序列化)
JavaScript Example: (AJAX.Post is a library I wrote, myUint8Array serializes properly)
AJAX.Post('www.example.com/SendByteArray', { myByteArray: myUint8Array }, Callback);
C#示例:(在我的默认控制器中)
C# Example: (In my default controller)
[HttpPost]
public async Task<JsonResult> SendByteArray(byte[] myByteArray) {
}
在此示例中myByteArray永远是空的。我尝试了几种不同的方法,基于转换为字符串,然后返回到byte [],但我无法获得正确的值。如果我能够以某种方式直接将代码实现到.NET的JSON解串器中以便上面的代码完全按原样工作,那将是非常优选的,因为我有一些其他项目,如果我可以直接传递字节数组,我可以做一些很酷的事情服务器端和客户端应用程序。
In this example myByteArray is always null. I've tried a couple different approaches based on converting to strings and then back to a byte[] but I haven't been able to get the correct value. It would be greatly preferred if I could somehow implement the code into .NET's JSON deserializer directly so that the code above works exactly as is, because I have a few other projects where I could do some cool things if I could pass byte arrays directly between the server-side and client-side applications.
推荐答案
现在唯一可以使用的方法是base64编码Uint8Array,在C#中将其捕获为字符串,然后将该字符串转换为byte []。
For now the only method I could get to work was to base64 encode the Uint8Array, capture it as a string in C#, and then convert that string to a byte[].
JavaScript:
JavaScript:
AJAX.Post('www.example.com/SendByteArray', { strByteArray: btoa(String.fromCharCode.apply(null, myUint8Array)) }, Callback);
C#:
[HttpPost]
public async Task<JsonResult> SendByteArray(string strByteArray) {
byte[] myByteArray = Convert.FromBase64String(strByteArray);
}
这篇关于.NET MVC从JSON Uint8Array反序列化字节数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!