简体   繁体   English

使用asp.net在邮件链接中发送表单URL

[英]Sending Form URL in the mail link using asp.net

I have a requirement where I want to send the User the link of the form via Email . 我有一个要求,我想通过电子邮件向用户发送表单的链接。 The link for every user will be different. 每个用户的链接都将不同。 Currently I am just passing hardcode value in the mail. 目前,我只是在邮件中传递硬编码值。

Here is my code. 这是我的代码。

sb.AppendLine("Please click on the link to view the details.");
            sb.AppendLine("</br></br>");
            sb.AppendLine("<a href='" + ConfigurationManager.AppSettings["ClickHereLink"].ToString() + "' >Click here to open request</a>. ");
            sb.AppendLine("</br></br>");
            sb.AppendLine("<b>This is a system generated mail. Please do not reply to the sender.</b>");

And the ClickHereLink is been defined in Web.config like below 并在Web.config定义了ClickHereLink ,如下所示

<add key="ClickHereLink" value="https://www.test.com"/>

How can I send dynamic link for every user based on the details 如何根据详细信息为每个用户发送动态链接

Encryption Method 加密方式

public static class EncryptDecrypt
{
    public static string Encrypt(string clearText)
    {
        string EncryptionKey = "MAKV2SPBNI99212";
        byte[] clearBytes = Encoding.Unicode.GetBytes(clearText);
        using (Aes encryptor = Aes.Create())
        {
            Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(EncryptionKey, new byte[] { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 });
            encryptor.Key = pdb.GetBytes(32);
            encryptor.IV = pdb.GetBytes(16);
            using (MemoryStream ms = new MemoryStream())
            {
                using (CryptoStream cs = new CryptoStream(ms, encryptor.CreateEncryptor(), CryptoStreamMode.Write))
                {
                    cs.Write(clearBytes, 0, clearBytes.Length);
                    cs.Close();
                }
                clearText = Convert.ToBase64String(ms.ToArray());
            }
        }
        return clearText;
    }
    public static string Decrypt(string cipherText)
    {
        string EncryptionKey = "MAKV2SPBNI99212";
        cipherText = cipherText.Replace(" ", "+");
        byte[] cipherBytes = Convert.FromBase64String(cipherText);
        using (Aes encryptor = Aes.Create())
        {
            Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(EncryptionKey, new byte[] { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 });
            encryptor.Key = pdb.GetBytes(32);
            encryptor.IV = pdb.GetBytes(16);
            using (MemoryStream ms = new MemoryStream())
            {
                using (CryptoStream cs = new CryptoStream(ms, encryptor.CreateDecryptor(), CryptoStreamMode.Write))
                {
                    cs.Write(cipherBytes, 0, cipherBytes.Length);
                    cs.Close();
                }
                cipherText = Encoding.Unicode.GetString(ms.ToArray());
            }
        }
        return cipherText;
    }
}

I am assuming that you are using .Net 4.x, and if so Try this, 我假设您使用的是.Net 4.x,如果是,请尝试

if you are not using 4.x then where I have used string interoperlation then you should change that for string.Format("...", ...); 如果您没有使用4.x,那么在我使用过字符串互操作的地方,应该将其更改为string.Format(“ ...”,...);

    void Main()
{
    var urlStr = new StringBuilder();
    var url = "http://www.yoursite.com";
    urlStr.Append($"{url}?{BuildQs(Request.QueryString)}");

}

private string BuildQs(System.Collections.Specialized.NameValueCollection collection)
{
    var sb = new StringBuilder();

    foreach (var key in collection.Keys)
    {
        var value = collection[key.ToString()].ToString();
        sb.Append($"{key}={value}");

    }
    var enc = new EncryptDecrypt();
    return enc.Encrypt(sb.ToString());
}

public class EncryptDecrypt
{
    public string Encrypt(string clearText)
    {
        string EncryptionKey = "MAKV2SPBNI99212";
        byte[] clearBytes = Encoding.Unicode.GetBytes(clearText);
        using (Aes encryptor = Aes.Create())
        {
            Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(EncryptionKey, new byte[] { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 });
            encryptor.Key = pdb.GetBytes(32);
            encryptor.IV = pdb.GetBytes(16);
            using (MemoryStream ms = new MemoryStream())
            {
                using (CryptoStream cs = new CryptoStream(ms, encryptor.CreateEncryptor(), CryptoStreamMode.Write))
                {
                    cs.Write(clearBytes, 0, clearBytes.Length);
                    cs.Close();
                }
                clearText = Convert.ToBase64String(ms.ToArray());
            }
        }
        return clearText;
    }
    public static string Decrypt(string cipherText)
    {
        string EncryptionKey = "MAKV2SPBNI99212";
        cipherText = cipherText.Replace(" ", "+");
        byte[] cipherBytes = Convert.FromBase64String(cipherText);
        using (Aes encryptor = Aes.Create())
        {
            Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(EncryptionKey, new byte[] { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 });
            encryptor.Key = pdb.GetBytes(32);
            encryptor.IV = pdb.GetBytes(16);
            using (MemoryStream ms = new MemoryStream())
            {
                using (CryptoStream cs = new CryptoStream(ms, encryptor.CreateDecryptor(), CryptoStreamMode.Write))
                {
                    cs.Write(cipherBytes, 0, cipherBytes.Length);
                    cs.Close();
                }
                cipherText = Encoding.Unicode.GetString(ms.ToArray());
            }
        }
        return cipherText;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM