React .NET-客户端返回带有双Qoute标记和斜杠的用户字符串
我正在使用一个项目,现在有2个端点,管理员可以使用户成为管理员或以管理员的身份删除用户。
此操作是使用用户ID进行的。 但是,当我输入按钮制作/删除管理员时,用户ID不会按预期返回,这给了我500个错误。
这是我得到的ID,
"\"65efe6a5-1384-49f7-8519-8d6789d26baf\""
这是我需要的ID,
"65efe6a5-1384-49f7-8519-8d6789d26baf"
可能会导致这一点,并如何解决?
后端
[HttpPost("assignAdminClaim")]
//[Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme, Policy = "IsAdmin")]
public async Task<ActionResult> AssignAdminClaim([FromBody] string userId)
{
var user = await userManager.FindByIdAsync(userId);
await userManager.AddClaimAsync(user, new Claim("role", "admin"));
return NoContent();
}
前端
const assignAdminClaim = async (id: string) => {
await adminClaim(`${UrlUsers}/assignAdminClaim`, id)
}
const adminClaim = async (url: string, id: string) => {
await axios.post(url, JSON.stringify(id), {
headers: {
"Content-Type": "application/json"
}
});
Swal.fire({
title: "Success",
text: "Successfully updated",
icon: "success",
})
}
所有帮助将不胜感激!
谢谢
I am working with a project and I have 2 endpoints now where an admin can make a user an admin or remove the user as an admin.
This operation is made with the users ID.
But when I enter the button to make/remove an admin, the user ID doesnt return as expected and it gives me a 500 error instead.
This is the Id I get
"\"65efe6a5-1384-49f7-8519-8d6789d26baf\""
This is the Id I need
"65efe6a5-1384-49f7-8519-8d6789d26baf"
What might be causing this and how could it be fixed?
backend
[HttpPost("assignAdminClaim")]
//[Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme, Policy = "IsAdmin")]
public async Task<ActionResult> AssignAdminClaim([FromBody] string userId)
{
var user = await userManager.FindByIdAsync(userId);
await userManager.AddClaimAsync(user, new Claim("role", "admin"));
return NoContent();
}
Frontend
const assignAdminClaim = async (id: string) => {
await adminClaim(`${UrlUsers}/assignAdminClaim`, id)
}
const adminClaim = async (url: string, id: string) => {
await axios.post(url, JSON.stringify(id), {
headers: {
"Content-Type": "application/json"
}
});
Swal.fire({
title: "Success",
text: "Successfully updated",
icon: "success",
})
}
All help would be much appreciated!
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您确认
json.stringify()
是原因,因此我在浏览器控制台中尝试了此代码,并确定json.stringify()
json.stringify()< /code>包装带有额外双引号的字符串,检查附件的图像。
As you confirmed that
JSON.stringify()
is the cause, therefore I tried this code in the browser console and figured out thatJSON.stringify()
wraps strings with extra double quotes, check the attached image.感谢 mahmoud nasr 的评论,我设法解决了问题。我不知道为什么,但是它似乎是
json.stringify()
是问题的原因。如果有人对为什么有更多的了解,请告诉我!Thanks to Mahmoud Nasr's comment, I managed to solve the issue. I dont know why, but it appears to be the
JSON.stringify()
that was the cause of the issue. If any one would have more knowledge about why, please let me know!