C# 名称';解密';在当前上下文中不存在?
我有一个类'IdentityConfig.cs和Helper.cs',在IdentityConfig上我调用Helper类来访问Decrypt方法及其参数列表。不知何故,我似乎不明白这一点,我得到了一个错误称为解密不存在或我错过了一些指令。如何解决此问题并调用正确的包。请帮我解决这个问题C# 名称';解密';在当前上下文中不存在?,c#,asp.net-mvc,C#,Asp.net Mvc,我有一个类'IdentityConfig.cs和Helper.cs',在IdentityConfig上我调用Helper类来访问Decrypt方法及其参数列表。不知何故,我似乎不明白这一点,我得到了一个错误称为解密不存在或我错过了一些指令。如何解决此问题并调用正确的包。请帮我解决这个问题 // IdentityConfig.cs using System; using System.Collections.Generic; using System.Linq; using System.Web;
// IdentityConfig.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;
using System.Security.Claims;
using System.Threading.Tasks;
using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.EntityFramework;
using Microsoft.AspNet.Identity.Owin;
using Microsoft.Owin;
using Microsoft.Owin.Security;
using System.Net.Mail;
using eNtsaTrainingRegistration.Models;
using System.Web.Configuration;
using System.Net;
namespace eNtsaTrainingRegistration.App_Start
{
public class EmailService : IIdentityMessageService
{
public Task SendAsync(IdentityMessage message)
{
var mailMessage = new MailMessage();
mailMessage.To.Add(new MailAddress(message.Destination));
mailMessage.From = new MailAddress("Gcobani Mkontwana <ggcobani@gmail.com>");
mailMessage.Subject = message.Subject;
mailMessage.IsBodyHtml = true;
mailMessage.Body = message.Body;
using(var smtp = new SmtpClient())
{
var credential = new NetworkCredential
{
UserName = WebConfigurationManager.AppSettings["UserName"],
Password = Helper.Decrypt(WebConfigurationManager.AppSettings["UserPasswd"])
};
smtp.Credentials = credential;
smtp.Host = WebConfigurationManager.AppSettings["SMTPName"];
smtp.Port = int.Parse(WebConfigurationManager.AppSettings["SMTPPort"]);
smtp.EnableSsl = true;
smtp.Send(mailMessage);
}
return Task.FromResult(0);
}
}
}
// Helper class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text;
using System.IO;
using System.Security.Cryptography;
namespace eNtsaTrainingRegistration.Helper
{
public class Helper
{
private const string PassPhrase = "3pAc0j$_56K?_S7c9gS!";
//Encrypt password.
public static string Encrypt(string strValue)
{
byte[] results;
UTF8Encoding uTF8 = new UTF8Encoding();
MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
byte[] deskey = md5.ComputeHash(uTF8.GetBytes(PassPhrase));
TripleDESCryptoServiceProvider desalg = new TripleDESCryptoServiceProvider();
desalg.Key = deskey;
desalg.Mode = CipherMode.ECB;
desalg.Padding = PaddingMode.PKCS7;
byte[] encrypt_data = uTF8.GetBytes(strValue);
try
{
ICryptoTransform encrytor = desalg.CreateEncryptor();
results = encrytor.TransformFinalBlock(encrypt_data, 0, encrypt_data.Length);
}
finally
{
desalg.Clear();
md5.Clear();
}
return Convert.ToBase64String(results);
}
//Decrypt password.
public static string Decrypt(string strValue)
{
byte[] results;
UTF8Encoding uTF8 = new UTF8Encoding();
MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
byte[] deskey = md5.ComputeHash(uTF8.GetBytes(PassPhrase));
TripleDESCryptoServiceProvider desalg = new TripleDESCryptoServiceProvider();
desalg.Key = deskey;
desalg.Mode = CipherMode.ECB;
desalg.Padding = PaddingMode.PKCS7;
byte[] decrypt_data = Convert.FromBase64String(strValue);
try
{
ICryptoTransform decryptor = desalg.CreateDecryptor();
results = decryptor.TransformFinalBlock(decrypt_data, 0, decrypt_data.Length);
}
finally
{
desalg.Clear();
md5.Clear();
}
return uTF8.GetString(results);
}
// In between space
public static string GetBetween(string strSource, string strStart, string strEnd)
{
int Start, End;
if(strSource.Contains(strStart) && strSource.Contains(strEnd))
{
Start = strSource.IndexOf(strStart, 0) + strStart.Length;
End = strSource.IndexOf(strEnd, Start);
return strSource.Substring(Start, End - Start);
}else
{
return "";
}
}
public static string BytesToString(long byteCount)
{
string[] suf = { "B", "KB", "MB", "GB", "TB", "PB", "EB" };
if (byteCount == 0)
return string.Format("{0} {1}", 0, suf[0]);
long bytes = Math.Abs(byteCount);
int place = Convert.ToInt32(Math.Floor(Math.Log(bytes, 1024)));
double num = Math.Round(bytes / Math.Pow(1024, place), 1);
return string.Format("{0} {1}", (Math.Sign(byteCount) * num).ToString(), suf[place]);
}
}
}
//IdentityConfig.cs
使用制度;
使用System.Collections.Generic;
使用System.Linq;
使用System.Web;
使用System.Data.Entity;
使用System.Security.Claims;
使用System.Threading.Tasks;
使用Microsoft.AspNet.Identity;
使用Microsoft.AspNet.Identity.EntityFramework;
使用Microsoft.AspNet.Identity.Owin;
使用Microsoft.Owin;
使用Microsoft.Owin.Security;
使用System.Net.Mail;
使用eNtsaTrainingRegistration.Models;
使用System.Web.Configuration;
Net系统;
命名空间eNtsaTrainingRegistration.App\u开始
{
公共类电子邮件服务:IIdentityMessageService
{
公共任务SendAsync(IdentityMessage消息)
{
var mailMessage=new mailMessage();
mailMessage.To.Add(新邮件地址(message.Destination));
mailMessage.From=新的邮寄地址(“Gcobani Mkontwana”);
mailMessage.Subject=message.Subject;
mailMessage.IsBodyHtml=true;
mailMessage.Body=message.Body;
使用(var smtp=new SmtpClient())
{
var-credential=新网络凭据
{
用户名=WebConfiguration Manager.AppSettings[“用户名”],
Password=Helper.Decrypt(WebConfiguration Manager.AppSettings[“UserPasswd”])
};
smtp.Credentials=凭证;
smtp.Host=WebConfiguration Manager.AppSettings[“SMTPName”];
smtp.Port=int.Parse(WebConfiguration Manager.AppSettings[“SMTPPort”]);
smtp.EnableSsl=true;
smtp.Send(mailMessage);
}
返回Task.FromResult(0);
}
}
}
//助手类
使用制度;
使用System.Collections.Generic;
使用System.Linq;
使用System.Web;
使用系统文本;
使用System.IO;
使用System.Security.Cryptography;
命名空间eNtsaTrainingRegistration.Helper
{
公营助理员
{
private const string PassPhrase=“3pAc0j$\u 56K?\u S7c9gS!”;
//加密密码。
公共静态字符串加密(字符串标准值)
{
字节[]结果;
UTF8Encoding uTF8=新的UTF8Encoding();
MD5CryptoServiceProvider md5=新的MD5CryptoServiceProvider();
byte[]deskey=md5.ComputeHash(uTF8.GetBytes(密码短语));
TripleDESCryptoServiceProvider desalg=新的TripleDESCryptoServiceProvider();
desalg.Key=deskey;
desalg.Mode=CipherMode.ECB;
desalg.Padding=PaddingMode.PKCS7;
byte[]encrypt_data=uTF8.GetBytes(strValue);
尝试
{
ICryptoTransform encrytor=desalg.CreateEncryptor();
结果=encrytor.TransformFinalBlock(加密\u数据,0,加密\u数据.Length);
}
最后
{
desalg.Clear();
md5.Clear();
}
返回Convert.tobase64字符串(结果);
}
//解密密码。
公共静态字符串解密(字符串strValue)
{
字节[]结果;
UTF8Encoding uTF8=新的UTF8Encoding();
MD5CryptoServiceProvider md5=新的MD5CryptoServiceProvider();
byte[]deskey=md5.ComputeHash(uTF8.GetBytes(密码短语));
TripleDESCryptoServiceProvider desalg=新的TripleDESCryptoServiceProvider();
desalg.Key=deskey;
desalg.Mode=CipherMode.ECB;
desalg.Padding=PaddingMode.PKCS7;
byte[]decrypt_data=Convert.FromBase64String(strValue);
尝试
{
ICryptoTransform decryptor=desalg.CreateDecryptor();
结果=decryptor.TransformFinalBlock(decrypt_data,0,decrypt_data.Length);
}
最后
{
desalg.Clear();
md5.Clear();
}
返回uTF8.GetString(结果);
}
//中间空间
公共静态字符串GetBetween(字符串strSource、字符串strStart、字符串stred)
{
int开始,结束;
if(strSource.Contains(strStart)和&strSource.Contains(strengd))
{
Start=strSource.IndexOf(strStart,0)+strStart.Length;
结束=strSource.IndexOf(强度,开始);
返回strSource.Substring(Start,End-Start);
}否则
{
返回“”;
}
}
公共静态字符串BytesToString(长字节数)
{
字符串[]suf={“B”、“KB”、“MB”、“GB”、“TB”、“PB”、“EB”};
if(字节计数==0)
返回string.Format(“{0}{1}”,0,suf[0]);
长字节=Math.Abs(字节数);
intplace=Convert.ToInt32(Math.Floor(Math.Log(字节,1024));
double num=Math.Round(bytes/Math.Pow(1024,place),1);
返回string.Format(“{0}{1}”,(Math.Sign(byteCount)*num.ToString(),suf[place]);
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用System.Web;
使用系统文本;
使用System.IO;
使用System.Security.Cryptography;
命名空间eNtsaTrainingRegistration
{
公共类助手
{
private const string PassPhrase=“3pAc0j$\u 56K?\u S7c9gS!”;
//加密密码。
公共静态字符串加密(字符串标准值)
{
字节[]结果;
UTF8Encoding uTF8=新的UTF8Encoding();
MD5CryptoServiceProvider md5=新的MD5CryptoServiceProv
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text;
using System.IO;
using System.Security.Cryptography;
namespace eNtsaTrainingRegistration
{
public class Helper_b
{
private const string PassPhrase = "3pAc0j$_56K?_S7c9gS!";
//Encrypt password.
public static string Encrypt(string strValue)
{
byte[] results;
UTF8Encoding uTF8 = new UTF8Encoding();
MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
byte[] deskey = md5.ComputeHash(uTF8.GetBytes(PassPhrase));
TripleDESCryptoServiceProvider desalg = new TripleDESCryptoServiceProvider();
desalg.Key = deskey;
desalg.Mode = CipherMode.ECB;
desalg.Padding = PaddingMode.PKCS7;
byte[] encrypt_data = uTF8.GetBytes(strValue);
try
{
ICryptoTransform encrytor = desalg.CreateEncryptor();
results = encrytor.TransformFinalBlock(encrypt_data, 0, encrypt_data.Length);
}
finally
{
desalg.Clear();
md5.Clear();
}
return Convert.ToBase64String(results);
}
//Decrypt password.
public static string Decrypt(string strValue)
{
byte[] results;
UTF8Encoding uTF8 = new UTF8Encoding();
MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
byte[] deskey = md5.ComputeHash(uTF8.GetBytes(PassPhrase));
TripleDESCryptoServiceProvider desalg = new TripleDESCryptoServiceProvider();
desalg.Key = deskey;
desalg.Mode = CipherMode.ECB;
desalg.Padding = PaddingMode.PKCS7;
byte[] decrypt_data = Convert.FromBase64String(strValue);
try
{
ICryptoTransform decryptor = desalg.CreateDecryptor();
results = decryptor.TransformFinalBlock(decrypt_data, 0, decrypt_data.Length);
}
finally
{
desalg.Clear();
md5.Clear();
}
return uTF8.GetString(results);
}
// In between space
public static string GetBetween(string strSource, string strStart, string strEnd)
{
int Start, End;
if(strSource.Contains(strStart) && strSource.Contains(strEnd))
{
Start = strSource.IndexOf(strStart, 0) + strStart.Length;
End = strSource.IndexOf(strEnd, Start);
return strSource.Substring(Start, End - Start);
}else
{
return "";
}
}
public static string BytesToString(long byteCount)
{
string[] suf = { "B", "KB", "MB", "GB", "TB", "PB", "EB" };
if (byteCount == 0)
return string.Format("{0} {1}", 0, suf[0]);
long bytes = Math.Abs(byteCount);
int place = Convert.ToInt32(Math.Floor(Math.Log(bytes, 1024)));
double num = Math.Round(bytes / Math.Pow(1024, place), 1);
return string.Format("{0} {1}", (Math.Sign(byteCount) * num).ToString(), suf[place]);
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;
using System.Security.Claims;
using System.Threading.Tasks;
using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.EntityFramework;
using Microsoft.AspNet.Identity.Owin;
using Microsoft.Owin;
using Microsoft.Owin.Security;
using eNtsaTrainingRegistration.Models;
using System.Net.Mail;
using System.Net;
using System.Web.Configuration;
namespace eNtsaTrainingRegistration
{
public class EmailService : IIdentityMessageService
{
public Task SendAsync(IdentityMessage message)
{
var mailMessage = new MailMessage();
mailMessage.To.Add(new MailAddress(message.Destination));
mailMessage.From = new MailAddress("Gcobani Mkontwana <ggcobani@gmail.com>");
mailMessage.Subject = message.Subject;
mailMessage.IsBodyHtml = true;
mailMessage.Body = message.Body;
using(var smtp = new SmtpClient())
{
var credential = new NetworkCredential
{
UserName = WebConfigurationManager.AppSettings["UserName"],
Password = Helper_b.Decrypt(WebConfigurationManager.AppSettings["UserPassword"])
};
smtp.Credentials = credential;
smtp.Host = WebConfigurationManager.AppSettings["SMTPName"];
smtp.Port = int.Parse(WebConfigurationManager.AppSettings["SMTPPort"]);
smtp.EnableSsl = true;
smtp.Send(mailMessage);
}
return Task.FromResult(0);
}
}
}