Random Musings

June 16, 2009

Error 401 while using NetworkCredential

Filed under: Dynamics CRM — haditeo @ 10:02 am

While running these code snippet to invoke Dynamics CRM Web Service using NetworkCredential, i have encountered the famous HTTP Error 401.2 – Unauthorized Access is Denied

CrmAuthenticationToken token = new CrmAuthenticationToken();
token.AuthenticationType = 0;
token.OrganizationName = "123";

CrmService service = new CrmService();
service.Url = "http://SERVER123/mscrmservices/2007/crmservice.asmx";
service.CrmAuthenticationTokenValue = token;

NetworkCredential nc = new NetworkCredential();
nc.Domain = "STAFF";
nc.UserName = "Administrator";
nc.Password = "whatisthepassword";

service.Credentials = nc;


Notice the current domain name. It’s specified as “STAFF.COM”, but inside the code above, i have specified it as “STAFF”. After i changed it to “STAFF.COM”, it’s working well at the 1st HTTP request. In my assumption, i don’t need to include Full Qualified Domain Name (FQDN), but eventually the system must accept a FQDN


Here are the things that i have tried before i found the current solution:

  • Downloaded Authentication Diagnostic to help pointing whether the current user (STAFF\Administrator) has proper rights and privileges to the current server.

    Found out that the current Dynamics CRM application pool is run with NetworkService user account, it needs to be changed to run under a valid user account.

  • Setspn command is also used to create SPN for the current user (STAFF\Administrator) to be authenticated properly in the current user
  • Tried to use DefaultCredentials and DefaultNetworkCredentials, turned out that it’s working well.

    service.Credentials = CredentialCache.DefaultCredentials;
    service.Credentials = CredentialCache.DefaultNetworkCredentials;

    At this point, i am very confuse, why it’s working on DefaultNetworkCredentials , and found out the domain name that i have specified is incorrect.


Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

Create a free website or blog at WordPress.com.

%d bloggers like this: