Following is the C code to import a certificate into the Windows trusted root certificate store using CryptoAPI:
PCCERT_CONTEXT pCertCtx = NULL;
if (CryptQueryObject (
CERT_QUERY_OBJECT_FILE,
L”D:\\selva\\b64.cer”,
CERT_QUERY_CONTENT_FLAG_ALL,
CERT_QUERY_FORMAT_FLAG_ALL,
0,
NULL,
NULL,
NULL,
NULL,
NULL,
(const void **)&pCertCtx) != 0)
{
HCERTSTORE hCertStore = CertOpenStore (
CERT_STORE_PROV_SYSTEM,
0,
0,
CERT_STORE_OPEN_EXISTING_FLAG |
CERT_SYSTEM_STORE_LOCAL_MACHINE,
L”ROOT”);
if (hCertStore != NULL)
{
if (CertAddCertificateContextToStore (
hCertStore,
pCertCtx,
CERT_STORE_ADD_ALWAYS,
NULL))
{
cout << “Added certificate to store.” << endl;
}
if (CertCloseStore (hCertStore, 0))
{
cout << “Cert. store handle closed.” << endl;
}
}
if (pCertCtx)
{
CertFreeCertificateContext [...]
Entries Tagged as ‘Windows’
June 9, 2008
CryptoAPI: How to import a certificate
March 14, 2008
Boxing/Unboxing in .NET
This post has been long overdue; I should have posted it two months back. Anyway, a couple of my friends attended training in .NET concepts sponsored by my company. They came back from the training and discussed with me the stuff they had learnt. One of the fellows mentioned that they had been taught that [...]
December 10, 2007
Some more COM+ configuration issues
As the release date of our project was nearing, I had become a bit busier in the past few weeks. That would explain the absence of any activity in this blog. The long coding and debugging sessions were fun. We faced plenty of issues during deployment in our test environment. Had loads of fun solving [...]
September 21, 2007
How to find the NetBIOS name of a domain
Following is the C# code needed to find the NetBIOS name of a domain from Active Directory:
using System;
using System.DirectoryServices;
void PrintNetBIOSName()
{
string netBIOSName = null;
DirectoryEntry rootDSE = new DirectoryEntry(
”LDAP://<server address>/rootDSE”,
”<username>”, “<password>”);
string domain = (string)rootDSE.Properties[
"defaultNamingContext"][0];
if (!String.IsNullOrEmpty(domain))
{
DirectoryEntry parts = new DirectoryEntry(
”LDAP://<server address>/CN=Partitions
,CN=Configuration,” + domain,
”<username>”, “<password>”);
foreach (DirectoryEntry part in parts.Children)
{
if ((string)part.Properties[
"nCName"][0] == domain)
{
netBIOSName = (string)part.Properties[
"NetBIOSName"][0];
break;
}
}
Console.WriteLine(netBIOSName);
}
}
REFERENCES:
http://home.hot.rr.com/graye/CodeSnippets.htm
Binding to Active Directory Domain Services [...]
September 13, 2007
Exposing events from custom controls in .NET
Recently, one of my very good friends asked me to help him with a problem. I noticed that he was using a custom control. To solve the problem, I needed to expose an event from the control. This was something I had never bothered about before. I stumbled upon this wonderful article in my quest [...]
July 13, 2007
COM+ configuration problems
I had been writing a COM+ application using Enterprise Services. I ran into a couple of configuration problems while deploying it. The COM+ server was deployed in a machine running Windows 2003 server.
Hurdle #1: First, the client refused to connect to the server and it kept throwing the following exception:
System.UnauthorizedAccessException: Access is denied. (Exception from [...]
June 22, 2007
NAnt: Signing satellite assemblies
I use NAnt to build my code. But, a few days ago, my code started throwing System.Resources.MissingManifestResourceException. I found that the code worked fine when I built it from VS.NET.
I was using resources for localization in my project. And it turned out that the generated satellite assembly wasn’t being signed when I was building my [...]
May 2, 2007
NUnit: A tutorial
Purpose of this article:
Till a few months back, I used to be one of those guys who think that testing is the worst thing a software developer could do under the sun. But then, I got introduced to NUnit recently and my perception about testing changed. In this article, I wish to sum up [...]