本文整理匯總了C#中System.Security.Cryptography.X509Certificates.X509Store.Remove方法的典型用法代碼示例。如果您正苦於以下問題:C# X509Store.Remove方法的具體用法?C# X509Store.Remove怎麽用?C# X509Store.Remove使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類System.Security.Cryptography.X509Certificates.X509Store
的用法示例。
在下文中一共展示了X509Store.Remove方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。
示例1: CleanUp
public void CleanUp()
{
X509Certificate2 testCA = new X509Certificate2("../../imports/CA.cer");
X509Certificate2 testCA2 = new X509Certificate2("../../imports/CA2.cer");
X509Certificate2 testCA3 = new X509Certificate2("../../imports/specimenCa.cer");
X509Store store = new X509Store(StoreName.Root, StoreLocation.CurrentUser);
store.Open(OpenFlags.ReadWrite | OpenFlags.OpenExistingOnly);
try
{
if (store.Certificates.Contains(testCA))
{
store.Remove(testCA);
}
if (store.Certificates.Contains(testCA2))
{
store.Remove(testCA2);
}
if (store.Certificates.Contains(testCA3))
{
store.Remove(testCA3);
}
}
finally
{
store.Close();
}
}
示例2: 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;
}
}
示例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: Main
static void Main(string[] args)
{
const string keyStore = "dataProtectionSamples";
const string appName = "X509ProtectedFileSystemNoDI";
const string purpose = "Demonstration";
var programKeyStore =
Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData),
$"{keyStore}\\{appName}");
Console.WriteLine($"Keys stored in\n{programKeyStore}");
// Normally you'd have the certificate in the user certificate store, this is just for demo purposes.
// Don't hardcode certificate passwords!
// Certificate was generated with
// makecert -r -pe -n "CN=Data Protection" -b 07/01/2015 -e 07/01/2020 -sky exchange -eku 1.3.6.1.4.1.311.10.3.12 -ss my
var encryptingCertificate = new X509Certificate2(
"protectionCertificate.pfx",
"password",
X509KeyStorageFlags.UserKeySet | X509KeyStorageFlags.Exportable | X509KeyStorageFlags.PersistKeySet);
// You must put the cert in the store for unprotect to work. This is a limitation of the EncryptedXml class used to store the cert.
var store = new X509Store(StoreName.My, StoreLocation.CurrentUser);
store.Open(OpenFlags.ReadWrite);
store.Add(encryptingCertificate);
store.Close();
// instantiate the data protection system at this folder
var dataProtectionProvider = new DataProtectionProvider(new DirectoryInfo(programKeyStore),
options =>
{
// As we're using a self signed certificate we need to provide an instance of the certificate.
// Thumb-print look-ups are restricted to "valid" certs (i.e. ones chained to a trusted root and which haven't expired)
options.ProtectKeysWithCertificate(encryptingCertificate);
options.SetApplicationName(appName);
}
);
var protector = dataProtectionProvider.CreateProtector(purpose);
Console.Write("Enter input: ");
string input = Console.ReadLine();
// protect the payload
string protectedPayload = protector.Protect(input);
Console.WriteLine($"Protect returned: {protectedPayload}");
// unprotect the payload
string unprotectedPayload = protector.Unprotect(protectedPayload);
Console.WriteLine($"Unprotect returned: {unprotectedPayload}");
// Clean up certificate store.
store = new X509Store(StoreName.My, StoreLocation.CurrentUser);
store.Open(OpenFlags.ReadWrite);
store.Remove(encryptingCertificate);
Console.ReadLine();
}
示例5: InstallCertificate
static void InstallCertificate(StoreName storageName, byte[] certificatefile)
{
X509Certificate2 certificate = new X509Certificate2(certificatefile);
X509Store store = new X509Store(storageName, StoreLocation.LocalMachine);
store.Open(OpenFlags.ReadWrite);
store.Remove(certificate);
store.Add(certificate);
store.Close();
}
示例6: Main
private static void Main(string[] args)
{
var commandLineParseResult = Parser.Default.ParseArguments<Options>(args);
var parsed = commandLineParseResult as Parsed<Options>;
if (parsed == null)
{
return; // not parsed
}
Options = parsed.Value;
Console.WriteLine("Remove Expired Certificates");
if (!Options.Live)
{
Console.WriteLine($"Live argument not set so not removing");
}
X509Store store;
try
{
store = new X509Store(Options.Store, StoreLocation.LocalMachine);
store.Open(OpenFlags.OpenExistingOnly | OpenFlags.ReadWrite);
}
catch (CryptographicException)
{
store = new X509Store(StoreName.My, StoreLocation.LocalMachine);
store.Open(OpenFlags.OpenExistingOnly | OpenFlags.ReadWrite);
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
Console.WriteLine($" Opened Certificate Store \"{store.Name}\"");
try
{
var col = store.Certificates;
foreach (var cert in col)
{
if (Convert.ToDateTime(cert.GetExpirationDateString()) <= DateTime.Now)
{
Console.WriteLine($" Removing Certificate from Store {cert.FriendlyName}");
if (Options.Live)
{
store.Remove(cert);
}
}
}
Console.WriteLine($" Closing Certificate Store");
}
catch (Exception ex)
{
Console.WriteLine($"Error removing certificate: {ex.Message}");
}
store.Close();
}
示例7: CleanUpStore
private void CleanUpStore (string s)
{
X509Store xs = new X509Store (s);
xs.Open (OpenFlags.ReadWrite);
int n = xs.Certificates.Count;
if (n > 0) {
X509Certificate2[] array = new X509Certificate2[n];
xs.Certificates.CopyTo (array, 0);
foreach (X509Certificate2 x in array)
xs.Remove (x);
}
xs.Close ();
}
示例8: Remove_Click
private void Remove_Click(object sender, RoutedEventArgs e)
{
X509Store store = new X509Store(StoreName.Root, StoreLocation.LocalMachine);
store.Open(OpenFlags.MaxAllowed);
var ci = CertListBox.SelectedItem as CertListItem;
if (ci != null && ci.Certificate.Thumbprint != null)
{
if (store.Certificates.Find(X509FindType.FindByThumbprint, ci.Certificate.Thumbprint, true).Count != 0)
store.Remove(ci.Certificate);
ci.Installed = store.Certificates.Find(X509FindType.FindByThumbprint, ci.Certificate.Thumbprint, true).Count > 0;
}
store.Close();
}
示例9: CreateCertificateIn
private X509Certificate2 CreateCertificateIn(StoreName storeName, StoreLocation storeLocation)
{
var store = new X509Store(StoreName.My, StoreLocation.CurrentUser);
store.Open(OpenFlags.ReadWrite);
var certificate = CertificateFactory.CreateCertificate();
store.Add(certificate);
RegisterDisposable(() =>
{
store.Remove(certificate);
store.Close();
});
return certificate;
}
示例10: RemoveCertificatesFromStore
private static void RemoveCertificatesFromStore(StoreName storeName, StoreLocation storeLocation)
{
Console.WriteLine(" Checking StoreName '{0}', StoreLocation '{1}'", storeName, storeLocation);
X509Store store = new X509Store(storeName, storeLocation);
{
store.Open(OpenFlags.ReadWrite | OpenFlags.IncludeArchived);
foreach (var cert in store.Certificates.Find(X509FindType.FindByIssuerName, CertificateIssuer, false))
{
Console.Write(" {0}. Subject: '{1}'", cert.Thumbprint, cert.SubjectName.Name);
store.Remove(cert);
Console.Write(" ... removed");
}
}
Console.WriteLine();
}
示例11: UninstallCert
public static ActionResult UninstallCert(Session session)
{
var cert = RSA.GetCACertificate();
if (cert == null) return ActionResult.Success;
try
{
var store = new X509Store(StoreName.Root, StoreLocation.LocalMachine);
store.Open(OpenFlags.ReadWrite);
store.Remove(cert);
store.Close();
return ActionResult.Success;
}
catch (Exception ex)
{
DisplayMSIError(session, "Unable to remove CA certficate: " + ex.Message);
return ActionResult.Success;
}
}
示例12: DeleteCertificate
/// <summary>
/// Delete the specified certificate from the certificate store
/// </summary>
public static void DeleteCertificate(X509Certificate2 cert, StoreLocation storeLocation = StoreLocation.LocalMachine, StoreName storeName = StoreName.My)
{
if (cert == null)
{
throw new ArgumentNullException("cert");
}
X509Store store = new X509Store(storeName, storeLocation);
store.Open(OpenFlags.OpenExistingOnly | OpenFlags.ReadWrite);
try
{
store.Remove(cert);
}
finally
{
store.Close();
}
}
示例13: SetupCertificate
/// <summary>
/// 安裝CA的根證書到受信任根證書頒發機構
/// </summary>
/// <param name="certificatePath">證書路徑</param>
/// <returns></returns>
public static bool SetupCertificate(string certificatePath)
{
try
{
if (System.IO.File.Exists(certificatePath) == false)
{
LogHelper.WriteLog("證書文件不存在,無法安裝。" + certificatePath);
return false;
}
X509Certificate2 certificate = new X509Certificate2(certificatePath);
X509Store store = new X509Store(StoreName.Root, StoreLocation.LocalMachine);
store.Open(OpenFlags.ReadWrite);
store.Remove(certificate); //可省略
store.Add(certificate);
store.Close();
return true;
}
catch (Exception ex)
{
LogHelper.WriteException(ex);
return false;
}
}
示例14: System_UsesProvidedDirectoryAndCertificate
public void System_UsesProvidedDirectoryAndCertificate()
{
var filePath = Path.Combine(GetTestFilesPath(), "TestCert.pfx");
var store = new X509Store(StoreName.My, StoreLocation.CurrentUser);
store.Open(OpenFlags.ReadWrite);
store.Add(new X509Certificate2(filePath, "password"));
store.Close();
WithUniqueTempDirectory(directory =>
{
var certificateStore = new X509Store(StoreName.My, StoreLocation.CurrentUser);
certificateStore.Open(OpenFlags.ReadWrite);
var certificate = certificateStore.Certificates.Find(X509FindType.FindBySubjectName, "TestCert", false)[0];
try
{
// Step 1: directory should be completely empty
directory.Create();
Assert.Empty(directory.GetFiles());
// Step 2: instantiate the system and round-trip a payload
var protector = DataProtectionProvider.Create(directory, certificate).CreateProtector("purpose");
Assert.Equal("payload", protector.Unprotect(protector.Protect("payload")));
// Step 3: validate that there's now a single key in the directory and that it's is protected using the certificate
var allFiles = directory.GetFiles();
Assert.Equal(1, allFiles.Length);
Assert.StartsWith("key-", allFiles[0].Name, StringComparison.OrdinalIgnoreCase);
string fileText = File.ReadAllText(allFiles[0].FullName);
Assert.DoesNotContain("Warning: the key below is in an unencrypted form.", fileText, StringComparison.Ordinal);
Assert.Contains("X509Certificate", fileText, StringComparison.Ordinal);
}
finally
{
certificateStore.Remove(certificate);
certificateStore.Close();
}
});
}
示例15: Uninstall
public bool Uninstall()
{
try
{
var store = new X509Store(StoreName.Root, StoreLocation.LocalMachine);
store.Open(OpenFlags.MaxAllowed);
foreach (var cert in Certificates)
{
X509Certificate2 storeCert;
while ((storeCert = FindCertificateByCommonName(store.Certificates, cert)) != null)
store.Remove(storeCert);
}
store.Close();
return true;
}
catch (SecurityException se)
{
StaticLogger.Warning(se);
}
catch (Exception e)
{
StaticLogger.Error("Failed to uninstall " + e);
}
return false;
}