locked
The request failed with HTTP status 401: Unauthorized. RRS feed

  • Question


  • Hi there,

    I have set up a CRMService in code :

    CrmService service = new CrmService();

     

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

    however, am getting the above error on this line :

    retrieveResponse = (RetrieveResponse)service.Execute(retrieveRequest);

    I am running  everything CRM related on my local machine, and am logged on as Administrator.

    I have changed the Logon from NT AUTHORITY\NetworkService to Local System Account, in the CRM Async Processing Service, but to no avail.

    Any help much appreciated.

    Thanks

    Joe
    Wednesday, January 20, 2010 10:16 AM

Answers

  • Hi,

    What about the token and organization info? What about the service URL? The proper code to initiate web services communication with CRM may look like the following (exceprt from SDK: http://msdn.microsoft.com/en-us/library/cc151175.aspx

    public static CrmService GetCrmService(string crmServerUrl, string organizationName)
        {
            // Set up the Authentication Token.
            CrmSdk.CrmAuthenticationToken token = new CrmSdk.CrmAuthenticationToken();
            token.OrganizationName = organizationName;
    
            CrmService service = new CrmService();
    
            if (crmServerUrl != null &&
                crmServerUrl.Length > 0)
            {
                UriBuilder builder = new UriBuilder(crmServerUrl);
                builder.Path = "//MSCRMServices//2007//CrmService.asmx";
                service.Url = builder.Uri.ToString();
            }
    
            service.Credentials = System.Net.CredentialCache.DefaultCredentials;
            service.CrmAuthenticationTokenValue = token;
    
            return service;
        }
    I hope that helps,
    Kuba
    -- Kuba Skałbania, Netwise
    • Marked as answer by Joe.com Wednesday, January 20, 2010 11:13 AM
    Wednesday, January 20, 2010 10:24 AM

All replies

  • If it is a asp.net web app, it should have windows authentication enabled along with impersantion & anonymous disabled.
    Also it will be better if you add your asp.net application in the isvconfig folder in the crm web virtual diretory (In IIS).

    In this CRM Async service has nothing to do with which account it is running.

    However, If it is workflow, the you can create the crmservice from the context object itself.
    Wednesday, January 20, 2010 10:23 AM
  • Hi,

    What about the token and organization info? What about the service URL? The proper code to initiate web services communication with CRM may look like the following (exceprt from SDK: http://msdn.microsoft.com/en-us/library/cc151175.aspx

    public static CrmService GetCrmService(string crmServerUrl, string organizationName)
        {
            // Set up the Authentication Token.
            CrmSdk.CrmAuthenticationToken token = new CrmSdk.CrmAuthenticationToken();
            token.OrganizationName = organizationName;
    
            CrmService service = new CrmService();
    
            if (crmServerUrl != null &&
                crmServerUrl.Length > 0)
            {
                UriBuilder builder = new UriBuilder(crmServerUrl);
                builder.Path = "//MSCRMServices//2007//CrmService.asmx";
                service.Url = builder.Uri.ToString();
            }
    
            service.Credentials = System.Net.CredentialCache.DefaultCredentials;
            service.CrmAuthenticationTokenValue = token;
    
            return service;
        }
    I hope that helps,
    Kuba
    -- Kuba Skałbania, Netwise
    • Marked as answer by Joe.com Wednesday, January 20, 2010 11:13 AM
    Wednesday, January 20, 2010 10:24 AM
  • Thanks Muhammad, Kuba

    I omitted the service URL - Thanks.

    Muhammad - yes its in a Custom workflow, but I dropped the code into an asp.net project as I found it easier to debug.

    Joe

    Wednesday, January 20, 2010 11:12 AM
  • I'm glad I could help! Any time!

    Kuba
    -- Kuba Skałbania, Netwise
    Wednesday, January 20, 2010 11:43 AM