飞创

WebApi 允许跨域

                    
public static void ConfigureCors(this IServiceCollection services)
{
    services.AddCors(options =>
    {
        options.AddPolicy("AnyPolicy", builder => builder.AllowAnyHeader().AllowAnyOrigin().AllowAnyMethod());
    });
}

//Program.cs
app.UseCors("AnyPolicy");
                    
                

C# 发送邮件

                    
public bool SendMail(MailAddress MessageFrom, string MessageTo, string MessageSubject, string MessageBody)
{
    MailMessage message = new MailMessage();
    message.To.Add(MessageTo);
    message.From = MessageFrom;
    message.Subject = MessageSubject;
    message.SubjectEncoding = System.Text.Encoding.UTF8;
    message.Body = MessageBody;
    message.BodyEncoding = System.Text.Encoding.UTF8;
    message.IsBodyHtml = true;
    message.Priority = MailPriority.High;
    SmtpClient sc = new SmtpClient();
    sc.EnableSsl = true;
    sc.Host = "smtp.qq.com";
    sc.Port = 587;
    sc.Credentials = new System.Net.NetworkCredential("xxxxxxxxxx@qq.com", "komkzngdfdxzebjg");

    try
    {
        ServicePointManager.ServerCertificateValidationCallback =
        delegate (Object obj, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors) 
        { 
            return true; 
        };
        sc.Send(message);
    }
    catch (Exception e)
    {
        Response.Write(e.Message);
        return false;
    }
    return true;
}
    
    
//调用方法
MailAddress MessageFrom = new MailAddress("xxxxxxxxxx@qq.com");
string MessageTo = "xxxxxxxxxx@126.com";
string MessageSubject = "密码找回";
string MessageBody = "您的密码是:123";

SendMail(MessageFrom, MessageTo, MessageSubject, MessageBody);
                    
                

C# RSA加解密

                    
static string keysDirectory = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Keys");
static string publicKeyPath = Path.Combine(keysDirectory, "public.xml");
static string privateKeyPath = Path.Combine(keysDirectory, "private.xml");

public static string Encrypt(string data)
{
    RSAParameters publicKey = LoadKeyFromFile(publicKeyPath, false);
    return EncryptData(data, publicKey);
}

public static string Decrypt(string data)
{
    RSAParameters privateKey = LoadKeyFromFile(privateKeyPath, true);
    return DecryptData(data, privateKey);
}

static void GenerateAndSaveKeyPair()
{
    using (RSA rsa = RSA.Create())
    {
        rsa.KeySize = 4096;
        RSAParameters publicKey = rsa.ExportParameters(false);
        RSAParameters privateKey = rsa.ExportParameters(true);
        SaveKeyToFile(publicKey, publicKeyPath);
        SaveKeyToFile(privateKey, privateKeyPath);
    }
}

static void SaveKeyToFile(RSAParameters key, string fileName)
{
    if (!Directory.Exists(keysDirectory))
    {
        Directory.CreateDirectory(keysDirectory);
    }
    using (StreamWriter sw = new StreamWriter(fileName))
    {
        sw.WriteLine($"<RSAKeyValue>");
        SaveKeyPart(sw, "Modulus", key.Modulus);
        SaveKeyPart(sw, "Exponent", key.Exponent);
        SaveKeyPart(sw, "P", key.P);
        SaveKeyPart(sw, "Q", key.Q);
        SaveKeyPart(sw, "DP", key.DP);
        SaveKeyPart(sw, "DQ", key.DQ);
        SaveKeyPart(sw, "InverseQ", key.InverseQ);
        SaveKeyPart(sw, "D", key.D);
        sw.WriteLine($"</RSAKeyValue>");
    }
}

static void SaveKeyPart(StreamWriter sw, string elementName, byte[]? data)
{
    if (data != null && data.Length > 0)
    {
        sw.WriteLine($"<{elementName}>{Convert.ToBase64String(data)}</{elementName}>");
    }
}

static RSAParameters LoadKeyFromFile(string fileName, bool isPrivate)
{
    if (!File.Exists(fileName))
    {
        GenerateAndSaveKeyPair();
    }
    RSAParameters key = new RSAParameters();
    string xmlString = File.ReadAllText(fileName);
    using (RSA rsa = RSA.Create())
    {
        rsa.FromXmlString(xmlString);
        key.Modulus = rsa.ExportParameters(isPrivate).Modulus;
        key.Exponent = rsa.ExportParameters(isPrivate).Exponent;
        key.P = rsa.ExportParameters(isPrivate).P;
        key.Q = rsa.ExportParameters(isPrivate).Q;
        key.DP = rsa.ExportParameters(isPrivate).DP;
        key.DQ = rsa.ExportParameters(isPrivate).DQ;
        key.InverseQ = rsa.ExportParameters(isPrivate).InverseQ;
        key.D = rsa.ExportParameters(isPrivate).D;
    }
    return key;
}

//加密数据
static string EncryptData(string data, RSAParameters publicKey)
{
    using (RSA rsa = RSA.Create())
    {
        rsa.ImportParameters(publicKey);
        byte[] encryptedBytes = rsa.Encrypt(Encoding.UTF8.GetBytes(data), RSAEncryptionPadding.OaepSHA512);
        return Convert.ToBase64String(encryptedBytes);
    }
}

//解密数据
static string DecryptData(string encryptedData, RSAParameters privateKey)
{
    using (RSA rsa = RSA.Create())
    {
        rsa.ImportParameters(privateKey);
        byte[] decryptedBytes = rsa.Decrypt(Convert.FromBase64String(encryptedData), RSAEncryptionPadding.OaepSHA512);
        return Encoding.UTF8.GetString(decryptedBytes);
    }
}
                    
                

C# BCrypt加解密

                    
//加密数据
public static string Encrypt(string password)
{
    string result = BCrypt.Net.BCrypt.HashPassword(password);
    return RSAHelper.Encrypt(result);
}

//解密数据
public static bool Verify(string password, string encryptedPassword)
{
    string hashedPassword = RSAHelper.Decrypt(encryptedPassword);
    return BCrypt.Net.BCrypt.Verify(password, hashedPassword);
}