none
CRM WebService - 401: Unauthorized Access

    Question

  • I am having an authentication issue with CRM Web service at the point when I want to insert a new record for any existing CRM entity through my Web service.

    The error message I receive is

     System.Net.WebException: The request failed with HTTP status 401: Unauthorized.

       at System.Web.Services.Protocols.SoapHttpClientProtocol.ReadResponse(SoapClientMessage message, WebResponse response, Stream responseStream, Boolean asyncCall)

       at System.Web.Services.Protocols.SoapHttpClientProtocol.Invoke(String methodName, Object[] parameters)

       .....\Service.asmx.cs:line 94

    ”   It even fails for the “WhoAmIRequest”. User I am trying to access has got full admin rights in CRM to update data in our Dev Environment. I am sending the Web service code snippets below.

     

    A) WhoAmI Request

    public string TestAuthentication(){

        string whoAmI = string.Empty;

     

        CrmWService.CrmService service = new CrmWService.CrmService();

               

        // Get the web service url from the config file

        service.Url = ConfigurationManager.AppSettings["CRMService"]; // read this entry from the web.config file.

     

        //service.Credentials = System.Net.CredentialCache.DefaultCredentials;

        service.Credentials = new NetworkCredential(ConfigurationManager.AppSettings["Username"], ConfigurationManager.AppSettings["Password"], ConfigurationManager.AppSettings["Domain"]);

     

        CrmWService.CrmAuthenticationToken token = new CrmWService.CrmAuthenticationToken();

        token.AuthenticationType = 0;

        token.OrganizationName = ConfigurationManager.AppSettings["Organisation"];

        service.CrmAuthenticationTokenValue = token;

     

        CrmWService.WhoAmIRequest request = new CrmWService.WhoAmIRequest();

        CrmWService.WhoAmIResponse response = (CrmWService.WhoAmIResponse)service.Execute(request);

        whoAmI = "<h1>Test</h1>";

        whoAmI = whoAmI + "<b>Organization Id: </b>" + response.OrganizationId.ToString() + "<br /><br />";

        whoAmI = whoAmI + "<b>BusinessUnit Id: </b>" + response.BusinessUnitId.ToString() + "<br /><br />";

        whoAmI = whoAmI + "<b>User Id: </b>" + response.UserId.ToString() + "<br /><br />";

     

        return whoAmI;

    }

    B) Insert Contact

     

    public string InsertContact()

    {

        string Message = string.Empty;

        try

        {

            CrmWService.CrmService service = new CrmWService.CrmService();

            // Get the web service url from the config file

            service.Url = ConfigurationManager.AppSettings["CRMService"]; // read this entry from the web.config file.

     

            //service.Credentials = System.Net.CredentialCache.DefaultCredentials;

            service.Credentials = new System.Net.NetworkCredential(ConfigurationManager.AppSettings["Username"], ConfigurationManager.AppSettings["Password"], ConfigurationManager.AppSettings["Domain"]);

            service.PreAuthenticate = true;

     

            // An Authentication Token is required because CRM requires an OrganizationName

            // to identify the Organization to be used

            CrmWService.CrmAuthenticationToken token = new CrmWService.CrmAuthenticationToken();

            token.AuthenticationType = 0;

            token.OrganizationName = ConfigurationManager.AppSettings["Organisation"];

            service.CrmAuthenticationTokenValue = token;

     

            CrmWService.contact contactToBeInserted = new CrmWService.contact();

                   

            // Create the properties of the contact object.

            contactToBeInserted.firstname = "Jesper";

            contactToBeInserted.lastname = "Aaberg";

            contactToBeInserted.address1_line1 = "23 Market St.";

            contactToBeInserted.address1_city = "Sammamish";

            contactToBeInserted.address1_stateorprovince = "MT";

            contactToBeInserted.address1_postalcode = "99999";

            contactToBeInserted.donotbulkemail = new CrmWService.CrmBoolean();

            contactToBeInserted.donotbulkemail.Value = true;

            contactToBeInserted.emailaddress1 = "test.test@test.com";

     

            // Create the contact in Microsoft CRM.

            Guid contactGuid = service.Create(contactToBeInserted);

        }

        catch (Exception ex)

        {

            //do nothing

            Message = ex.ToString();

        }

        return Message;

    }

    I would certainly appreciate if anyone could look into this and suggest us whether are we missing anything obvious (or) do we need to add anything in CRM server please?

    Friday, April 20, 2012 1:02 PM

All replies

  • Hi,

    are you sure you are using vaild user to connect.

    The request failed with HTTP status 401: Unauthorized


    Mahain : Check My Blog
    Follow me on Twitter
    Make sure to "Vote as Helpful" and "Mark As Answer",if you get answer of your question.

    Friday, April 20, 2012 1:21 PM
    Moderator