Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/31.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何对ASP.NET WebAPI确认电子邮件的URL中的字符进行编码?_C#_Asp.net_Asp.net Web Api - Fatal编程技术网

C# 如何对ASP.NET WebAPI确认电子邮件的URL中的字符进行编码?

C# 如何对ASP.NET WebAPI确认电子邮件的URL中的字符进行编码?,c#,asp.net,asp.net-web-api,C#,Asp.net,Asp.net Web Api,我有一个ASP.NET WebAPI应用程序,需要为确认电子邮件创建链接。下面是它的代码: callbackUrl1 = "http://localhost:2757/index.html" + "?load=email" + "&userId=" + user.Id + "&code=" + code; await UserManager.SendEmailAsync(

我有一个ASP.NET WebAPI应用程序,需要为确认电子邮件创建链接。下面是它的代码:

 callbackUrl1 = "http://localhost:2757/index.html" +
                "?load=email" +
                "&userId=" + user.Id +
                "&code=" + code;
 await UserManager.SendEmailAsync(
    user.Id, 
    "Confirm your account", 
    "Please click this link: <a href=\"" + callbackUrl1 + "\">link</a>");
然而,我没有使用MVC,当我尝试添加它时,我遇到了一些不相关的问题


有人能告诉我如何对链接中的数据进行编码(如果这是正确的话)?

使用方法。请注意,您仍然需要对电子邮件的HTML的URL进行HTML编码

使用System.Uri对URL进行编码

使用系统;
公共课程
{
公共静态void Main()
{
var callbackUrl1=new System.Uri(“http://localhost:2757/index.html" +
“?加载=电子邮件”+
“&userId=“+”12345&^”+
“&code=“+”myCo de”);
var aHref=string.Format(“,callbackUrl1.AbsoluteUri”);
控制台写入线(aHref);
}
}

将其创建为一个Systyem.URI。关于
userId=HttpUtility.UrlEncode(user.Id)
?您可以通过创建一个URI并使用AbsoluteUri属性来完成整个回调URL1。当然可以,但是您必须创建一个对象并将其放回字符串中。对我来说,这似乎是一个优先考虑的问题。公平地说,静态会让你更快到达那里,Uri会更容易。我想说,查询字符串中的参数越多,Uri方法对我来说就越可取。除非你设计的是110%的性能。我真的同意。问题是2作为许多参数的位置:)对于2,可能在我的书中的HttpUtility.UrlEncode上。希望我今天过得愉快:)我建议代码“onclick=function(){alert(“pwned”);}data dummy=”使用这个值(包括单引号)。当然,在这种特殊情况下,代码可能是由ASP.NET标识生成的,但一般来说,在这种情况下,您需要进行HTML编码。这取决于用户标识和代码来自何处。如果来自web表单上的输入,则需要采取必要的措施防止注入。根据您的需求,有各种各样的方法来实现这一点。如果在这种情况下需要的话,这是很有趣的。现在很明显,ASP.NET Identity不可能生成实际注入的代码,但它可能会生成一个单引号并破坏某些东西。
 var callbackUrl = Url.Action("ConfirmEmail", 
     "Account", 
     new { userId = user.Id, code = code }, 
     protocol: Request.Url.Scheme);
using System;

public class Program
{
    public static void Main()
    {

        var callbackUrl1 = new System.Uri("http://localhost:2757/index.html" +
                "?load=email" +
                "&userId=" + "12345££&^" +
                "&code=" + "myCo   de");


        var aHref = string.Format("<a href=\'{0}'>link</a>", callbackUrl1.AbsoluteUri);


        Console.WriteLine(aHref);
    }
}