React .NET-客户端返回带有双Qoute标记和斜杠的用户字符串

发布于 2025-02-08 23:57:01 字数 1298 浏览 3 评论 0原文

我正在使用一个项目,现在有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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

╰つ倒转 2025-02-15 23:57:02

您确认json.stringify()是原因,因此我在浏览器控制台中尝试了此代码,并确定json.stringify()json.stringify()< /code>包装带有额外双引号的字符串,检查附件的图像。

enter image description here

As you confirmed that JSON.stringify() is the cause, therefore I tried this code in the browser console and figured out that JSON.stringify() wraps strings with extra double quotes, check the attached image.

懵少女 2025-02-15 23:57:02

感谢 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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文