views:

3620

answers:

7

Hi everyone,

I made an application that access CRM's web service. The problem is, when I deployed the dll into Sharepoint server, it returned error 401 unauthorized. Apparently the System.Net.CredentialCache.DefaultCredentials didn't work (my suspicion). Here's the code.

CrmSdk.CrmAuthenticationToken token = new CrmSdk.CrmAuthenticationToken();
token.AuthenticationType = AuthenticationType.AD;
token.OrganizationName = ORGANIZATION_NAME;

CrmService service = new CrmService();
service.Url = "http://crmserver:5555/mscrmservices/2007/crmservice.asmx";
service.CrmAuthenticationTokenValue = token;
service.PreAuthenticate = true;
service.Credentials = System.Net.CredentialCache.DefaultCredentials;

It goes vice-versa.

When I made application that access Sharepoint's webservice (coding the plugin) and deployed it to CRM server. It couldn't access the Sharepoint's web service. Unauthorized error. Here is the code:

Lists listService = new Lists();
listService.PreAuthenticate = true;
listService.Credentials = System.Net.CredentialCache.DefaultCredentials;
listService.Url = "http://sharepointserver/webname/_vti_bin/Lists.asmx";

My CRM server and Sharepoint server are in the same domain.

For both code, if I changed the credentials part into something like this then deploy it on server, it can run.

service.Credentials = new NetworkCredential("username", "password", "domain");

Still, I don't want to do this because it reveals user's password in the code. May anyone help me?

The IIS in both server doesn't allow Anonymous Access and it uses Integrated Windows Authentication.

Thank you

+1  A: 

Could be that you need to be running Kerberos for authentication, but cannot be sure and it is a pain to setup just to check.

Nat
Hm..I have checked before and the server uses Kerberos authentication.any other option or opinion? :Dthanks anyway :D
cyrene
+1  A: 

Have you verified that the default credentials are the same as those when you explicitly state them? It could be that the default credentails are those of another account that you wouldn't expect.

EDIT #1: Per the remarks for the DefaultCredentials property on MSDN:

DefaultCredentials represents the system credentials for the current security context in which the application is running. For a client-side application, these are usually the Windows credentials (user name, password, and domain) of the user running the application. For ASP.NET applications, the default credentials are the user credentials of the logged-in user, or the user being impersonated.

You'll also want to ensure that the user accessing the CRM page (making the call to the SharePoint web service) can access the web service with their credentials and vice versa. If they can then it would seem more likely that some kind of impersonation is happening.

Edit #2: Assuming that you have access to both the CRM and SharePoint server you might take a peak into both the application and system logs. One or both should likely indicate a failed login and indicate which account attempted to access the resource (in this case the web services).

Richard C. McGuire
hm...I didn't state the value of default credential. I just used it, and isn't it supposed to find by itself the matching credential needed then send it to the web service?how to verify default credential? as we can't see the username and password in it, can we?thank you :)
cyrene
added an update, you can try checking the logs, they might show the failed login and point you to the account that is attempting to access the service(s).
Richard C. McGuire
thanks for your update :) I peeked into CRM's logs, and even though I've turned off anonymous access on IIS, the account attempting to access the service is one of the CRM's user. I wonder why the defaultcredentials in Sharepoint turned out to be that user. I'll look for more informations. thanks :)
cyrene
A: 

From my local computer, I can access the CRM web services or Sharepoint web services. I guess I'm authorized because the DefaultCredentials sent my credentials that its password is saved in the "Stored Username and Password" (Control Panel > User Accounts > tab Advanced > Manage Passwords) This way, I don't have to type:

service.Credentials = new NetworkCredential("username", "password", "domain");

and my DefaultCredentials from my local comp is authorized to access the web services.

I tried to implement this on the Sharepoint server that access CRM web services. and..tadaa..it won't work. hahaha..

can we inject credentials to DefaultCredentials in server?

the last thing I want to do is to hardcode the useraccount (like the code above)

cyrene
You cannot inject credentials to the DefaultCredentials property as it is read only; the link to the property on MSDN in my post shows the declaration of the property.
Richard C. McGuire
A: 

service.Credentials = System.Net.CredentialsCache.DefaultNetworkCredentials;

Try that.

Corey Sunwold
I've tried and it didn't work. I've read msdn and can't distinguish the difference between DefaultCredentials and DefaultNetworkCredentials.but, thanks :)
cyrene
A: 

Not familiar with Sharepoint, but can't you just store the connection information in a configuration and use built in tools for securing your web.config? Thats what I do.

http://aspnet.4guysfromrolla.com/articles/021506-1.aspx

benjynito
A: 

to be able use defaultcredentials, the user in active directory must be defined both in SharePoint and CRM and have enough privileges to do what you are doing with code.

And try to use sdk (crm have helper classes) instead of service definitions.

Orkun Balkancı
A: 

Hi,

By using DefaultCredentials means the ASP.NET worker process or IIS worker process will take the credential of the user who run the IIS Application Pool.

so if your Dynamics CRM Application Pool is run under a user account Custom-CRM-Domain\JohnDoe, that means it will take the privileges under user account Custom-CRM-Domain\JohnDoe.

Please check the user account who run the application pool of the CRM\Sharepoint Application IIS Web application.

These are the steps to check the Application Pool:

  1. Open the website -> Right Click -> Choose Properties
  2. Select the Home Directory tab
  3. Notice the Application Pool name at the dropdownlist below
  4. Now, go to the Application Pools folder
  5. Try to find the Application Pool name which has been listed in the step 3 -> Right Click and choose Properties
  6. Select the "Identity" tab and you will find the user account who run the application pool

Hope this helps.

hadi teo