【发布时间】:2022-09-27 19:14:44
【问题描述】:
我是一名工科学生,基于 xamarin 应用程序完成我的最终学位项目,包括客户端 (xamarin) 和 API (.net) 之间的连接。我正在尝试将 json 对象中包含的一些加密数据(以 base64 编码)作为请求发送。在 API 方面,它接受 json 请求,执行一些完全同态的加密功能,并在新的加密信息中返回响应。
问题是当我试图在 API 中接收作为名为 \"Post.cs\" 的自创建类的响应时,其中包括下一个属性;
public class Post
{
public ulong ? userId { get; set; }
public int ? id { get; set; }
public string? title { get; set; }
public string? body { get; set; }
public string? userIdEncrypted { get; set; }
public string? userIdEncryptedReturned { get; set; }
public string? parmsStr { get; set; }
public Post(ulong? userId, int? id, string? title, string? body, string? userIdEncrypted, string? userIdEncryptedReturned, string? parmsStr)
{
this.userId = userId;
this.id = id;
this.title = title;
this.body = body;
this.userIdEncrypted = userIdEncrypted;
this.userIdEncryptedReturned = userIdEncryptedReturned;
this.parmsStr = parmsStr;
}
因此,我的 API 接受请求并对其进行反序列化,以创建一个 \"Post\" 并用它做一些事情。 我正在尝试复制 HttpPost,如下所示:
[Route(\"api/[controller]\")]
[ApiController]
public class PostController
{
#region CONSTRUCTOR
public PostController()
{
}
#endregion
//POST ://api/Post
[Route(\"api/[controller]\")]
[HttpPost]
public Post ReceivePost([FromBody] Post post)
{
...
var _post = new Post(post.userId, post.id, post.title, post.body, post.userIdEncrypted
post.userIdEncryptedReturned, post.parmsStr);
... FHE functions...
return _post;
}
}
因此,当我在 xamarin 上从客户端发布 \"Post\" 时,我正在发送一个 Post 作为已经提到的结构,其中 userIdEncrypted 和 parmsStr 包含一个 base64 编码的字符串。当它到达 API 服务器时,会出现以下问题:
Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware[1]
An unhandled exception has occurred while executing the request.
System.NotSupportedException: Deserialization of types without a parameterless constructor, a singular parameterized constructor, or a parameterized constructor annotated with \'JsonConstructorAttribute\' is not supported. Type \'System.IO.Stream\'. Path: $ | LineNumber: 0 | BytePositionInLine: 1.
---> System.NotSupportedException: Deserialization of types without a parameterless constructor, a singular parameterized constructor, or a parameterized constructor annotated with \'JsonConstructorAttribute\' is not supported. Type \'System.IO.Stream\'.
--- End of inner exception stack trace ---
XAMARI 应用程序的客户端这是我从客户端发布的 json 字符串:
PostModel postAux = new PostModel()
{
userId = 2,
id = 1,
title = \"Title for post 1\",
body = \"Body for post 1\",
};
/******************************************************
* Encrypt data of the post (userId)
******************************************************/
PostModel newPost = initializeFHE(postAux);
//Here is where I fill the encrypted data (base64 string) included in the Post object
/******************************************************
* POST encrypted data to the server in csharp
******************************************************/
Uri requestUri = new Uri(\"http://myHost:3000/api/Post\");
var client = new HttpClient();
client.DefaultRequestHeaders
.Accept
.Add(new MediaTypeWithQualityHeaderValue(\"application/json\")); // ACCEPT header
var json = JsonConvert.SerializeObject(newPost);
var contentJson = new StringContent(json.ToString(), Encoding.UTF8, \"application/json\");
//Console.WriteLine(contentJson);
var response = await client.PostAsync(requestUri, contentJson);
...
其中 PostModel 指的是这个自建模型:
public class PostModel : BaseViewModel
{
public ulong userId { get; set; }
public int id { get; set; }
public string title { get; set; }
public string body { get; set; }
public string userIdEncrypted { get; set; }
public string userIdEncryptedReturned { get; set; }
public string parmsStr { get; set; }
}
我知道我在 .Net 和 c# 上的编程经验不足,因此欢迎任何帮助和解释。
问候, 劳尔。
标签: c# asp.net .net xamarin seal