.NET MVC 从 JSON Uint8Array 反序列化字节数组 [英] .NET MVC deserialize byte array from JSON Uint8Array

查看:24
本文介绍了.NET MVC 从 JSON Uint8Array 反序列化字节数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 js-scrypt (https://github.com/tonyg/js-scrypt) 在我的客户端 Web 应用程序上散列和加盐密码,然后将它们发布到我的服务器端 .NET MVC 应用程序以再次散列和加盐.这个 JavaScript 库将字节数组实现为 JavaScript Uint8Arrays.如何让我的 MVC 控制器将我的 JSON Uint8Array 反序列化为字节 []?

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.

推荐答案

目前我能开始工作的唯一方法是对 Uint8Array 进行 base64 编码,在 C# 中将其捕获为字符串,然后将该字符串转换为字节[].

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆