Wednesday, June 11, 2008

Access UNC file share with credentials

Continuing a little further on passing credentials, I came across a need where a job is run by a specific local account on the server but the job needed to also pull files from a UNC shared folder that requires network authentication. There are multiple ways to deal with this like using impersonation in .Net, just for the files part and reverting back to default and so on. However, the job under current consideration is just a batch file and uses XCOPY.

An easier way I figured to work around this is to login to the server using the local account that runs the job. Map the UNC file folder to a drive; when windows prompted for credentials, used a valid network account and made sure to reconnect at logon. Well, there you go! It seems to work for me. Don’t know if there’s a better ways to do it but this is simple enough to let it work for now.

Love coding!

UPDATE 07/21/2008:
Well, found a better way :-) Just use Net command to set the connection credentials. For Example,

net use \\MyServer\D /user:domain\myUserID MyPassword
XCopy ....

It works!

Friday, June 6, 2008

Passing credentials to URL - when reading content from a url using System.Net namespace

This is a simple case where a data feed load application was reading from multiple resources for getting feed data. One of the provider switched to basic authentication and required a username and password being passed to access their resource. This can be done very easily using System.Net.NetworkCredential. Let's jump into couple of methods as example to read data from a url requiring credentials:

// Using System.Net.WebClient

WebClient webClient = new WebClient();

webClient.Credentials = new NetworkCredential("userName", "password");

// I set my proxy with webProxy method and use it if needed

//webClient.Proxy = this.webProxy;

using (Stream stream = webClient.OpenRead(url))

{

using (StreamReader reader = new StreamReader(stream))

{

string response = reader.ReadToEnd();

return response;

}

}

// Using System.Net.HttpWebRequest

HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);

request.Credentials = new NetworkCredential("userName", "password");

// I set my proxy with webProxy method and use it if needed

//request.Proxy = this.webProxy;

using (Stream stream = request.GetResponse().GetResponseStream())

{

using (StreamReader reader = new StreamReader(stream))

{

string response = reader.ReadToEnd();

return response;

}

}
Love coding!

Thursday, June 5, 2008

Visual Studio 2008 & Crystal Reports Redistributable package

Recently, I was looking for the redistributable package for Crystal Reports after Visual Studio 2008 installation. It is tucked under Microsoft SDK folder and we could get it from this location:

C:\Program Files\Microsoft SDKs\Windows\v6.0A\Bootstrapper\Packages\CrystalReports10_5

Love coding!