The request failed with HTTP status 401: Unauthorized. in CRM development RRS feed

  • Question

  • Hi, 
    I am new to CRM,  this is the first piece of code i developed in CRM.
    I got error in the following code:   The request failed with HTTP status 401: Unauthorized. 

                System.Net.NetworkCredential creds = new System.Net.NetworkCredential();
                creds.Password = my_live_email;
                creds.UserName = my_password;
                CrmAuthenticationToken token = new CrmAuthenticationToken();
                token.AuthenticationType = 1;
                token.OrganizationName = organization_name;
                CrmService service = new CrmService();
                service.Url = "https://website/MSCrmServices/2007/CrmService.asmx";
                service.CrmAuthenticationTokenValue = token;
                service.Credentials = creds;

                lead lead = new lead();
                lead.emailaddress1 = lead_email;

                RetrieveDuplicatesRequest request = new RetrieveDuplicatesRequest();
                request.BusinessEntity = lead;
                request.MatchingEntityName = EntityName.lead.ToString();
                request.PagingInfo = new PagingInfo();

                RetrieveDuplicatesResponse response = (RetrieveDuplicatesResponse)service.Execute(request);

    I use a live email and password to access the CRM website. I could login from the browser without any problem.
    I could see xml content of CrmServiceWsdl.aspx

    But when i add the web reference of webservice. it ask me the user name and password. after that, it display list of methods in the window, but give me message "The HTML document does not contain Web service discovery information." and "add reference is grayed out and I can't add it.
    So i download the WSDL file to the local, and add the reference from the local.

    I don't know whether this cause "Unauthorized" error, could anyone tell me how to fix it  if it caused by this?

    If not,  anybody can tell me anything wrong in the code. and how to solve the problem

    Appreciate for your help

    Friday, May 22, 2009 4:21 PM


All replies

  • Check this forum where I got some help. The later part of the thread is where I think you will get your answer.

    Use the javascript from one of the last posts to give you a pop up of your unique org name. Replace organization_name with the name that pops up. Unless you were just using a dummy name above, organization_name is likely not your CRM instance.
    • Marked as answer by Yuhe Tuesday, May 26, 2009 2:30 PM
    Friday, May 22, 2009 5:22 PM
  • This link:  http://technet.microsoft.com/en-us/library/bb955360.aspx says you need to get a ticket from the CrmDiscoveryService.
    • Marked as answer by Yuhe Tuesday, May 26, 2009 2:30 PM
    Friday, May 22, 2009 7:24 PM
  • Thanks a lot for Richard and Marcus's help.

    I modified my local WSDL file and make the location to the server instead of localhost.
    and in the code. the url point to the localhost instead of server. then the authentication works fine.
    Tuesday, May 26, 2009 2:32 PM