SSIS: UNC Share Authentication

One common request that I receive while creating SSIS packages for customers is to export some data to a Shared Folder without mapping a drive letter to the share. Usually, I must use different credentials to connect to this share. Below is a code snippet which will connect to the specified share using supplied credentials. Both the share and authentication information are stored as SSIS User Variables.

string strServer = Dts.Variables[“ServerName”].Value.ToString();
string strShare = Dts.Variables[“ServerShare”].Value.ToString();
string strUsername = Dts.Variables[“ServerUsername”].Value.ToString();
string strPassword = Dts.Variables[“ServerPassword”].Value.ToString();
Process pNetDelete = new Process();
pNetDelete.StartInfo.CreateNoWindow = true;
pNetDelete.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
pNetDelete.StartInfo.UseShellExecute = false;
pNetDelete.StartInfo.FileName = “net”;
pNetDelete.StartInfo.Arguments = string.Format(“use /DELETE {0}\
{1} /Y”,  strServer, strShare);
pNetDelete.Start();
pNetDelete.WaitForExit();
Process pNetShare = new Process();
pNetShare.StartInfo.CreateNoWindow = true;
pNetShare.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
pNetShare.StartInfo.UseShellExecute = false;
pNetShare.StartInfo.RedirectStandardError = true;
pNetShare.StartInfo.RedirectStandardOutput = true;
pNetShare.StartInfo.FileName = “net”;
pNetShare.StartInfo.Arguments = string.Format(“use \\{0}\{1} /u:"{2}" "{3}"”,
        strServer, strShare, strUsername, strPassword);
pNetShare.Start();
pNetShare.WaitForExit();
string strError = pNetShare.StandardError.ReadToEnd();
if (pNetShare.ExitCode != 0)
{
    bool fireAgain = false;
    Dts.TaskResult = (int)ScriptResults.Failure;
    Dts.Events.FireInformation(999, “Networking Authentication”,
    strError.Replace(Environment.NewLine, ” “), string.Empty, 0, ref fireAgain);
}
else
    Dts.TaskResult = (int)ScriptResults.Success;

Note: Using this method you will have to store the passwords as clear text variables. I would recommend implementing an encryption function to use with this approach.

Leave a Reply

Your email address will not be published. Required fields are marked *

*


*