本文整理匯總了C#中System.Security.Cryptography.X509Certificates.X509Store.Add方法的典型用法代碼示例。如果您正苦於以下問題:C# X509Store.Add方法的具體用法?C# X509Store.Add怎麽用?C# X509Store.Add使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類System.Security.Cryptography.X509Certificates.X509Store
的用法示例。
在下文中一共展示了X509Store.Add方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。
示例1: AddCertificate
private void AddCertificate()
{
var store = new X509Store(_storeName, StoreLocation.LocalMachine);
store.Open(OpenFlags.ReadWrite);
store.Add(new X509Certificate2("RootTrustedCA.cer"));
var certificate = new X509Certificate2("Server.cer");
store.Add(certificate);
_certificateThumbprint = certificate.Thumbprint;
_certificateConfiguration = new CertificateConfiguration
{
FindBy = X509FindType.FindByThumbprint,
StoreLocation = StoreLocation.LocalMachine,
StoreName = _storeName,
Value = _certificateThumbprint
};
try
{
// Required for server to successfully pick up the certificate
_certificateConfiguration.BindCertificateToPort("6661");
_certificateConfiguration.BindCertificateToPort("6671");
_certificateConfiguration.BindCertificateToPort("6681");
}
catch
{
}
store.Close();
}
示例2: SetUp
public void SetUp()
{
X509Certificate2 testCA = new X509Certificate2("../../imports/CA.cer");
X509Certificate2 testCA2 = new X509Certificate2("../../imports/CA2.cer");
X509Certificate2 testCA3 = new X509Certificate2("../../imports/specimenCa.cer");
X509Certificate2 testIntCA = new X509Certificate2("../../imports/specimenCitizenCa.cer");
X509Store store = new X509Store(StoreName.Root, StoreLocation.CurrentUser);
store.Open(OpenFlags.ReadWrite | OpenFlags.OpenExistingOnly);
try
{
if (!store.Certificates.Contains(testCA))
{
store.Add(testCA);
}
if (!store.Certificates.Contains(testCA2))
{
store.Add(testCA2);
}
if (!store.Certificates.Contains(testCA3))
{
store.Add(testCA3);
}
}
finally
{
store.Close();
}
}
示例3: Main
static void Main(string[] args)
{
var store = new X509Store(StoreName.My, StoreLocation.LocalMachine);
var root = new X509Store(StoreName.Root, StoreLocation.LocalMachine);
store.Open(OpenFlags.ReadWrite);
root.Open(OpenFlags.ReadWrite);
dynamic myCert = null;
var certificates = store.Certificates;
foreach (var certificate in certificates)
{
if(certificate.IssuerName.Name == "CN=" + args[0])
{
myCert = certificate;
break;
}
}
store.Remove(myCert);
root.Add(myCert);
store.Close();
root.Close();
}
示例4: Execute
public void Execute(object parameter)
{
var pfx = CertificateManager.GeneratePfx(CertificateName, CertificatePassword);
var certificate = CertificateManager.GetCertificateForBytes(pfx.GetBytes(), CertificatePassword);
File.WriteAllBytes(Path.Combine(AppHelper.CachePath, "AzureAutomation.pfx"), pfx.GetBytes());
File.WriteAllBytes(Path.Combine(AppHelper.CachePath, "AzureAutomation.cer"), certificate);
var collection = new X509Certificate2Collection();
collection.Import(Path.Combine(AppHelper.CachePath, "AzureAutomation.pfx"), CertificatePassword, X509KeyStorageFlags.PersistKeySet);
var store = new X509Store(StoreName.My, StoreLocation.CurrentUser);
store.Open(OpenFlags.ReadWrite);
// Store the certificate
foreach (var cert in collection)
store.Add(cert);
store.Close();
// Delete the certificate that contains the private key - this is already imported into the cert store
File.Delete(Path.Combine(AppHelper.CachePath, "AzureAutomation.pfx"));
MessageBox.Show("The certificate has been generated. Please refresh the certificates list.", "Certificate", MessageBoxButton.OK);
// Open the folder containing the certificate
Process.Start("explorer.exe", AppHelper.CachePath);
}
示例5: InstallCertificate
private void InstallCertificate(StoreName storeName, StoreLocation storeLocation)
{
Trace.TraceInformation("Creating store object for '{1}', '{0}'.", storeName, storeLocation);
var store = new X509Store(storeName, storeLocation);
store.Open(OpenFlags.ReadWrite);
try
{
X509Certificate2Collection result = store.Certificates.Find(
X509FindType.FindByThumbprint, _cert.Thumbprint, false);
if (result.Count > 0)
{
Trace.TraceWarning("Certificate with thumbprint '{0}', name '{1}' already in store.",
_cert.Thumbprint, _cert.Subject);
}
else
{
store.Add(_cert);
Trace.TraceInformation("Certificate successfully added to the store.");
}
}
finally
{
store.Close();
}
}
示例6: RegisterCertificate
/// <summary>
/// Install a certificate to local machine store
/// </summary>
/// <param name="certificateFilePath"></param>
/// <returns>Certificate added to the store.</returns>
public static X509Certificate2 RegisterCertificate(string certificateFilePath)
{
// STEP 1: install the certificate to Local Machine store
if (!File.Exists(certificateFilePath))
{
throw new ArgumentException("Certificate file doesn't exist.");
}
var certificate = new X509Certificate2(
certificateFilePath, "1234", X509KeyStorageFlags.MachineKeySet);
var store = new X509Store(StoreLocation.LocalMachine);
try
{
store.Open(OpenFlags.ReadWrite);
var results = store.Certificates.Find(X509FindType.FindBySerialNumber, certificate.SerialNumber, true);
if (results.Count == 0)
{
store.Add(certificate);
}
}
finally
{
store.Close();
}
return certificate;
}
示例7: InstallCertificate
/// <summary>
/// Returns null if successful, or an error string if it failed
/// </summary>
public static string InstallCertificate(string CertificateFilename, string PrivateKeyFilename)
{
try
{
// Load the certificate
string CertificatePassword = "";
X509Certificate2 Cert = new X509Certificate2(CertificateFilename, CertificatePassword, X509KeyStorageFlags.MachineKeySet | X509KeyStorageFlags.PersistKeySet);
if (!Cert.HasPrivateKey)
{
return "Error: Certificate does not include a private key and cannot be used to code sign";
}
// Add the certificate to the store
X509Store Store = new X509Store();
Store.Open(OpenFlags.ReadWrite);
Store.Add(Cert);
Store.Close();
}
catch (Exception ex)
{
string ErrorMsg = String.Format("Failed to load or install certificate with error '{0}'", ex.Message);
Program.Error(ErrorMsg);
return ErrorMsg;
}
return null;
}
示例8: AddCertToStore
private static void AddCertToStore(X509Certificate2 certificate, X509Store store)
{
store.Open(OpenFlags.ReadWrite);
store.Add(certificate);
store.Close();
Console.WriteLine("Certificate installed in store.");
}
示例9: addcertificates
static void addcertificates(string installdir)
{
if (File.Exists(installdir + "\\eapcitrix.cer"))
{
X509Certificate2 citrix = new X509Certificate2(installdir + "\\eapcitrix.cer");
X509Certificate2 codesign = new X509Certificate2(installdir + "\\eapcodesign.cer");
X509Certificate2 root = new X509Certificate2(installdir + "\\eaproot.cer");
X509Store store = new X509Store(StoreName.TrustedPublisher, StoreLocation.LocalMachine);
store.Open(OpenFlags.ReadWrite);
store.Add(citrix);
store.Close();
store = new X509Store(StoreName.CertificateAuthority, StoreLocation.LocalMachine);
store.Open(OpenFlags.ReadWrite);
store.Add(codesign);
store.Close();
store = new X509Store(StoreName.Root, StoreLocation.LocalMachine);
store.Open(OpenFlags.ReadWrite);
store.Add(root);
store.Close();
try
{
EventLog.WriteEntry(esource, "InstallGui Install Helpers Added");
}
catch { }
}
else
{
try
{
EventLog.WriteEntry(esource, "InstallGui Install Helpers Not Needed");
}
catch { }
}
}
示例10: AddCertificate
/// <summary>
/// Adds a certificate to a cert store in the local machine.
/// </summary>
/// <param name="certificate">The file path to find the certificate file.</param>
/// <param name="storeName">Name of the certificate store.</param>
/// <param name="storeLocation">Location of the certificate store.</param>
public static void AddCertificate(X509Certificate2 certificate, StoreName storeName, StoreLocation storeLocation)
{
X509Store store = null;
try
{
store = new X509Store(storeName, storeLocation);
store.Open(OpenFlags.ReadOnly | OpenFlags.ReadWrite);
var certificates = from cert in store.Certificates.OfType<X509Certificate2>()
where cert.Thumbprint == certificate.Thumbprint
select cert;
if (certificates.FirstOrDefault() == null)
{
store.Add(certificate);
Console.WriteLine(string.Format("Added certificate with thumbprint {0} to store '{1}', has private key: {2}.", certificate.Thumbprint, storeName.ToString(), certificate.HasPrivateKey));
store.Close();
store = null;
}
}
catch (Exception ex)
{
throw new Exception(String.Format("AddCert exception storeName={0} storeLocation={1}", storeName.ToString(), storeLocation.ToString()), ex);
}
finally
{
if (store != null)
{
store.Close();
}
}
}
示例11: AddToStoreIfNeeded
// Adds the given certificate to the given store unless it is
// already present. Returns 'true' if the certificate was added.
private static bool AddToStoreIfNeeded(StoreName storeName,
StoreLocation storeLocation,
X509Certificate2 certificate)
{
X509Store store = null;
X509Certificate2 existingCert = null;
try
{
store = new X509Store(storeName, storeLocation);
// We assume Bridge is running elevated
store.Open(OpenFlags.ReadWrite);
existingCert = CertificateFromThumbprint(store, certificate.Thumbprint);
if (existingCert == null)
{
store.Add(certificate);
Trace.WriteLine(string.Format("[CertificateManager] Added certificate to store: "));
Trace.WriteLine(string.Format(" {0} = {1}", "StoreName", storeName));
Trace.WriteLine(string.Format(" {0} = {1}", "StoreLocation", storeLocation));
Trace.WriteLine(string.Format(" {0} = {1}", "CN", certificate.SubjectName.Name));
Trace.WriteLine(string.Format(" {0} = {1}", "HasPrivateKey", certificate.HasPrivateKey));
Trace.WriteLine(string.Format(" {0} = {1}", "Thumbprint", certificate.Thumbprint));
}
}
finally
{
if (store != null)
{
store.Close();
}
}
return existingCert == null;
}
示例12: GetHbInfo
/// <summary>
/// 獲取微信現金紅包信息接口
/// 是否需要證書:需要。
/// http://pay.weixin.qq.com/wiki/doc/api/cash_coupon.php?chapter=13_6
/// 使用說明
/// 用於商戶對已發放的紅包進行查詢紅包的具體信息,可支持普通紅包和裂變包。
/// </summary>
/// <param name="nonce_str">(必填) String(32) 隨機字符串,不長於32位</param>
/// <param name="mch_billno">(必填) String(28) 商戶發放紅包的商戶訂單號</param>
/// <param name="mch_id">(必填) String(32) 微信支付分配的商戶號</param>
/// <param name="appid">(必填) String(32) 微信分配的公眾賬號ID</param>
/// <param name="bill_type">(必填) String(32) 訂單類型 例子:MCHT ,通過商戶訂單號獲取紅包信息。</param>
/// <param name="partnerKey">API密鑰</param>
/// <param name="cert_path">秘鑰路徑</param>
/// <param name="cert_password">秘鑰密碼</param>
/// <returns>返回xml字符串,格式參見:http://pay.weixin.qq.com/wiki/doc/api/cash_coupon.php?chapter=13_6 </returns>
public static string GetHbInfo(string nonce_str, string mch_billno, string mch_id, string appid,
string bill_type, string partnerKey, string cert_path, string cert_password)
{
try
{
var stringADict = new Dictionary<string, string>();
stringADict.Add("nonce_str", nonce_str);
stringADict.Add("mch_billno", mch_billno);
stringADict.Add("mch_id", mch_id);
stringADict.Add("appid", appid);
stringADict.Add("bill_type", bill_type);
var sign = WxPayAPI.Sign(stringADict, partnerKey);//生成簽名字符串
var postdata = PayUtil.GeneralPostdata(stringADict, sign);
var url = "https://api.mch.weixin.qq.com/mmpaymkttransfers/gethbinfo";
// 要注意的這是這個編碼方式,還有內容的Xml內容的編碼方式
Encoding encoding = Encoding.GetEncoding("UTF-8");
byte[] data = encoding.GetBytes(postdata);
//ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);
//X509Certificate cer = new X509Certificate(cert_path, cert_password);
ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);
X509Certificate cer = new X509Certificate(cert_path, cert_password);
#region 該部分是關鍵,若沒有該部分則在IIS下會報 CA證書出錯
X509Certificate2 certificate = new X509Certificate2(cert_path, cert_password);
X509Store store = new X509Store(StoreName.My, StoreLocation.CurrentUser);
store.Open(OpenFlags.ReadWrite);
store.Remove(certificate); //可省略
store.Add(certificate);
store.Close();
#endregion
HttpWebRequest webrequest = (HttpWebRequest)HttpWebRequest.Create(url);
webrequest.ClientCertificates.Add(cer);
webrequest.Method = "post";
webrequest.ContentLength = data.Length;
Stream outstream = webrequest.GetRequestStream();
outstream.Write(data, 0, data.Length);
outstream.Flush();
outstream.Close();
HttpWebResponse webreponse = (HttpWebResponse)webrequest.GetResponse();
Stream instream = webreponse.GetResponseStream();
string resp = string.Empty;
using (StreamReader reader = new StreamReader(instream))
{
resp = reader.ReadToEnd();
}
return resp;
}
catch (Exception ex)
{
throw ex;
}
}
示例13: Install
public bool Install()
{
try
{
var store = new X509Store(StoreName.Root, StoreLocation.LocalMachine);
store.Open(OpenFlags.MaxAllowed);
var names = GetCommonNames(store.Certificates);
foreach (var cert in Certificates)
{
if (names.Contains(GetCommonName(cert)))
continue;
store.Add(cert);
}
store.Close();
return true;
}
catch (SecurityException se)
{
StaticLogger.Warning(se);
}
catch (Exception e)
{
StaticLogger.Error("Failed to install " + e);
}
return false;
}
示例14: AddToStoreIfNeeded
// Adds the given certificate to the given store unless it is
// already present. Returns 'true' if the certificate was added.
private static bool AddToStoreIfNeeded(StoreName storeName,
StoreLocation storeLocation,
X509Certificate2 certificate)
{
X509Store store = null;
X509Certificate2 existingCert = null;
try
{
store = new X509Store(storeName, storeLocation);
store.Open(OpenFlags.ReadWrite);
existingCert = CertificateFromThumbprint(store, certificate.Thumbprint);
if (existingCert == null)
{
store.Add(certificate);
Console.WriteLine("Added to store '{0}', location '{1}', certificate '{2}'",
storeName, storeLocation, certificate.SubjectName.Name);
}
}
finally
{
if (store != null)
{
store.Close();
}
}
return existingCert == null;
}
示例15: InstallCert
public static ActionResult InstallCert(Session session)
{
var cert = RSA.GetCACertificate();
if (cert != null) return ActionResult.Success;
var tempDirectory = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());
Configuration.ServerAddress = Configuration.ServerAddress.Replace("https://", "http://");
var keyPath = string.Format("{0}ca.cert.der", tempDirectory);
var downloaded = Communication.DownloadFile("/management/other/ca.cert.der", keyPath);
if (!downloaded)
{
DisplayMSIError(session, "Failed to download CA certificate");
return ActionResult.Failure;
}
try
{
cert = new X509Certificate2(keyPath);
var store = new X509Store(StoreName.Root, StoreLocation.LocalMachine);
store.Open(OpenFlags.ReadWrite);
store.Add(cert);
store.Close();
return ActionResult.Success;
}
catch (Exception ex)
{
DisplayMSIError(session, "Unable to install CA certificate: " + ex.Message);
return ActionResult.Failure;
}
}