Active Directory, C#, Certificates, Programming

Programmatically Install A Root CA Certificate So Users Don’t Have To (C#)

So here’s the backstory: I received the task of improving certificate enrollment so that users can

1) Be verified via username, password, captcha & verification pin

2) Auto Enroll without an external approval

3) Simplify the process

As some of you already know, ADCS via web enrollment is…how can we say…dated. So I wrote an application that sits in front of ADCS to first verify the user. Once they are through then the web enrollment is configured to let them run the wizard through to installing their cert. The issue that came to me is that most end-users will not take the time to ensure the root CA makes it to the trusted store (thus giving the classic CA cert is not installed message). So I received the order from on-high to “do it for them”. At first I struggled, attempting to understand how this could be done. I spoke with Microsoft and as I already was aware they indicated that having the user choose the trusted root store for the CA is by design. So what to do…ah I know, let’s just script it out.

So this is as simple as it gets. Download the cert, store it on the local drive and use the built-in certmgr.exe to perform the root CA to trusted store installation. Here it is (this code is just one a basic console app):


using System.Security.Cryptography.X509Certificates;

WebClient webClient = new WebClient();
webClient.DownloadFile("", @"C:\Temp\certnew.cer");

X509Store store = new X509Store(StoreName.Root,
X509Certificate2Collection collection = new X509Certificate2Collection();
X509Certificate2 cert = new X509Certificate2(@"C:\Temp\certnew.cer");
byte[] encodedCert = cert.GetRawCertData();
Console.WriteLine("We are now installing the CA certificate into the Trusted Root Certificate store ...");
Console.WriteLine("Done! The CA certificate was successfully. Press any key to close.");

This finishes the root CA portion so they can fly through the rest of web enrollment. Hope this helps.

C#, Oracle, Programming, SQL

Execute An Oracle Stored Procedure With Parameters (C#)

So you want to execute an Oracle stored procedure with parameters, huh? For this example I have an Oracle stored procedure called MEMBER_TYPE_UPDATE that will update what type of membership I have based on the numeric value. This sort of snippet can be used in a web application directly or called by some form of web service.

Here are some example values:

0 = Not a member

1 = Member

2 = Member with first tier privileges

3 = Member with highest level privileges

The update occurs based on their username and based on that username will attempt to update the numeric value. So below we will be calling a method (passing the two parameters to update with). I will then gather the connection string to Oracle and begin to execute my Oracle stored procedure (while giving it the values passed into the method to be used in the stored procedure).

Here’s my snippet:

using System;
using System.Data;
using System.Web.Services;
using Oracle.DataAccess.Client;
using System.Configuration;

public string SetUserMembership(string membershipNetworkUserName, int membershipStatusValue)
string errorString = string.Empty;
OracleCommand cmd = null;

string connectionString = string.Empty;
if (ConfigurationManager.AppSettings["location"].Contains("PROD"))
connectionString = ConfigurationManager.ConnectionStrings["ConnectionStringPROD"].ConnectionString;
connectionString = ConfigurationManager.ConnectionStrings["ConnectionStringDEV"].ConnectionString;

cmd = new OracleCommand();
cmd.Connection = new OracleConnection(connectionString);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("in_employeeUserName", OracleDbType.Varchar2).Value = membershipNetworkUserName;
cmd.Parameters.Add("in_status_id", OracleDbType.Decimal).Value = membershipStatusValue;
cmd.Parameters.Add("O_RETURN_STATUS", OracleDbType.Varchar2, 4000).Direction = ParameterDirection.Output;

string returnString = cmd.Parameters["O_RETURN_STATUS"].Value.ToString();
if (!returnString.Contains("SUCCESS"))
// obviously there was an issue and we want to display this somewhere
errorString = returnString;

For a specific call I would call it with SetUserMembership(“username”, “2”);

Ideally you would have variables there. Hope this helps people to see the parameters in use.

C#, Email, Programming, Web

Programmatically Send An Email Message With An Html Body (C#)

I have used the following snippet many times in the past. I would recommend validating your string values if the values are dynamic. I would also recommend placing your email attempt in a try/catch so you will not break your web application if there is some kind of issue on the email server itself. Anyway, hope this helps.

using System.Net.Mail;

// server settings
SmtpClient client = new SmtpClient();
client.Port = 25;
client.Host = "";

// declasre mail message
MailMessage mm = new MailMessage();

// from
mm.From = new MailAddress("");

// to
string toemail = "";
toemail = "";
mm.To.Add(new MailAddress(toemail));

// cc
string toemailcc = "";
toemailcc = "";
mm.CC.Add(new MailAddress(toemailcc));

// subject
mm.Subject = "The All Important Subject";

// body
string message = "";
message = "This is the all important email body message. <br /><br />";
message = message + "Sincerely yours, <br /><br />";
mm.Body = message;
mm.IsBodyHtml = true;

// send the message