gpt4 book ai didi

c# - MVC 中的 URL 编码 token

转载 作者:太空宇宙 更新时间:2023-11-03 22:36:32 25 4
gpt4 key购买 nike

核心 2 MVC。我想生成“确认电子邮件链接”。我像这样生成一个 token :

var token = await m_userManager.GenerateEmailConfirmationTokenAsync(user);

向用户发送电子邮件以激活他/她的帐户。然而,通过的 token 已从输入中删除了“+”:

    // Original
CfDJ8ON8hfMagqZNkicGHBXln5hi6kj4TCMMaxS5SaaU4BzEgFlKev1mT2gsij7gh2AkG8G+00t4n/PGwX6oQCvnpHk+wIGc1y8ycNbnzGpwW9Q8fwRlZDGQcIMtSCmX7LXwRS0iHSXgDF1O/QcDNGKbIMMZdcfMKYwnnzmUpmNxvZtG0JCYu5o754Y83VEtdbKASlzQz4aFxOUulvHRBQc3xRi2r0N8yveg26FO+RJ9khsqxKGRu4JDDVNWkpguXeVJvA==

//Notice '+' is missin
CfDJ8ON8hfMagqZNkicGHBXln5hi6kj4TCMMaxS5SaaU4BzEgFlKev1mT2gsij7gh2AkG8G 00t4n/PGwX6oQCvnpHk wIGc1y8ycNbnzGpwW9Q8fwRlZDGQcIMtSCmX7LXwRS0iHSXgDF1O/QcDNGKbIMMZdcfMKYwnnzmUpmNxvZtG0JCYu5o754Y83VEtdbKASlzQz4aFxOUulvHRBQc3xRi2r0N8yveg26FO RJ9khsqxKGRu4JDDVNWkpguXeVJvA==

Controller :

[HttpGet]
public async Task<IActionResult> Activate(Guid? id, string token)
{
...

这里的解决方案是什么?我应该简单地将“空格”替换为 + 号吗?

最佳答案

您可以对 token 进行编码,然后在收到 token 时进行解码。通过这种方式,您可以避免 url 编码。

例子:

public static void Main()
{
var token = "CfDJ8ON8hfMagqZNkicGHBXln5hi6kj4TCMMaxS5SaaU4BzEgFlKev1mT2gsij7gh2AkG8G+00t4n/PGwX6oQCvnpHk+wIGc1y8ycNbnzGpwW9Q8fwRlZDGQcIMtSCmX7LXwRS0iHSXgDF1O/QcDNGKbIMMZdcfMKYwnnzmUpmNxvZtG0JCYu5o754Y83VEtdbKASlzQz4aFxOUulvHRBQc3xRi2r0N8yveg26FO+RJ9khsqxKGRu4JDDVNWkpguXeVJvA==";
var newToken = HttpUtility.UrlEncode(token);
Console.WriteLine(newToken);
//this will print newToken without +
var originalToken = HttpUtility.UrlDecode(newToken);
//this will print original token, with +
Console.WriteLine(originalToken);
}

关于c# - MVC 中的 URL 编码 token ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54830717/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com