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

  • Question

  • Hi,
    I'm new to Dynamics CRM web service client develop, my customer has a sandbox for developing, I can login with user name and password in IE, but when I use the asp.net program, it will show "The request failed with HTTP status 401: Unauthorized.".

    I have debuged the asp.net program (written by other guys), it's works when create CrmService object, but when call addLead function, it will throw an exception that says "The request failed with HTTP status 401: Unauthorized." and the program is end.

    How can I access the server in asp.net program? Is there any settings need do in the Dynamics system? (I cannot access the sandbox).

    Thanks.

    Daniel

    I need more sleeping~_~
    Tuesday, August 4, 2009 6:24 AM

Answers

  • Hi,

    So you must have added web reference to CrmService.

    Check if you are passing correct values for !!
     getCrmServiceBy(url,username,password,domain
    );
    You could also try this as well

    <identity impersonate="true"
    userName="domain\user"
    password="password" />


    Regards,
    Nishant Rana
    http://nishantrana.wordpress.com
    Tuesday, August 4, 2009 7:50 AM

All replies

  • Hi,

    In your asp.net program, try by setting impersonation in your web.config .

    <identity impersonate="true" />

    Regards,
    Nishant Rana

    http://nishantrana.wordpress.com
    Tuesday, August 4, 2009 6:30 AM
  • HI, Nishant,

    Thanks for your replay, my asp.net program still not works, the configure file updated:
    <?xml version="1.0" encoding="utf-8"?>

    <configuration>
      
        <configSections>
            <sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
                <section name="WebService2.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
            </sectionGroup>
        </configSections>
        <appSettings/>
        <connectionStrings/>
      
        <system.web>
            <!-- 
                Set compilation debug="true" to insert debugging 
                symbols into the compiled page. Because this 
                affects performance, set this value to true only 
                during development.
            -->
            <compilation debug="true" />
            <!--
                The <authentication> section enables configuration 
                of the security authentication mode used by 
                ASP.NET to identify an incoming user. 
            -->
            <authentication mode="Windows" />
            <!--
                The <customErrors> section enables configuration 
                of what to do if/when an unhandled error occurs 
                during the execution of a request. Specifically, 
                it enables developers to configure html error pages 
                to be displayed in place of a error stack trace.

            <customErrors mode="RemoteOnly" defaultRedirect="GenericErrorPage.htm">
                <error statusCode="403" redirect="NoAccess.htm" />
                <error statusCode="404" redirect="FileNotFound.htm" />
            </customErrors>
            -->
          <identity impersonate="true"/>
        </system.web>
        <applicationSettings>
            <WebService2.Properties.Settings>
                <setting name="WebService2_WebReference_MetadataService" serializeAs="String">
                    <value>My Dynamics sandbox metadata services address.</value>
                </setting>
                <setting name="WebService2_WebReference1_CrmService" serializeAs="String">
                    <value>My Dynamics sandbox web services address.</value>
                </setting>
            </WebService2.Properties.Settings>
        </applicationSettings>
    </configuration>


    I need more sleeping~_~
    Tuesday, August 4, 2009 6:41 AM
  • Hi.

    Could you paste your codebehind code here?
    Truth is opened the prepared mind My blog - http://a33ik.blogspot.com
    Tuesday, August 4, 2009 6:43 AM
    Moderator
  • CrmService parameterCrm = getCrmServiceBy(url,username,password,domain);
    
    if (leadid != null)
    { 
      try
      {
          updateLead(
                  parameterCrm, 
                  firestname, 
                  lastname, 
                  companyname,
                  saluation, 
                  jobtitle,
                  businessphone, 
                  homephone,
                  mobilephone,
                  ifax, 
                  pager, 
                  emailaddress,
                  description,
                  street1name, 
                  street1stateorprovince,
                  street2name, 
                  address2postalcode,
                  city,
                  sic,
                  leadid
                  );
    
          Response.Write(leadid.ToString()); 
    
      } 
      catch (System.Exception ex)
      {
          string teststr = ex.Message; 
          Guid leadId = addLead(
                                  parameterCrm, 
                                  firestname, 
                                  lastname, 
                                  companyname,
                                  saluation, 
                                  jobtitle,
                                  businessphone, 
                                  homephone, 
                                  mobilephone,
                                  ifax, 
                                  pager, 
                                  emailaddress,
                                  description,
                                  street1name, 
                                  street1stateorprovince,
                                  street2name, 
                                  address2postalcode,
                                  city,
                                  sic
                                  );
    
          Response.Write(leadId.ToString());
    
      }
    }
    else
    {
       Guid leadId = addLead(
                              parameterCrm, 
                              firestname, 
                              lastname, 
                              companyname,
                              saluation, 
                              jobtitle,
                              businessphone, 
                              homephone, 
                              mobilephone,
                              ifax, 
                              pager, 
                              emailaddress,
                              description,
                              street1name, 
                              street1stateorprovince,
                              street2name, 
                              address2postalcode,
                              city,
                              sic
                              );
    
       Response.Write(leadId.ToString()); 
    }
    
    }
    catch (Exception ex)
    {
    Response.Write(ex.Message); 
    }
    The get CrmServiceBy function:
    private static CrmService getCrmServiceBy(string url ,string username,string password,string domain) 
    {
    	CrmService myService = new CrmService();
    	if (domain != null)
    	{
      	myService.Credentials = new NetworkCredential(username, password, domain);
    	}else {
      	myService.Credentials = new NetworkCredential(username, password, "PrivUserDomain"); 
    	}
    
    	myService.Url = url;
    	return myService;
    }


    I need more sleeping~_~
    Tuesday, August 4, 2009 6:54 AM
  • Hi.

    It looks nice, but where UpdateLead and AddLead methods?

    Truth is opened the prepared mind My blog - http://a33ik.blogspot.com
    Tuesday, August 4, 2009 6:56 AM
    Moderator
  • Sorry.
    The addLead function:
           public static Guid addLead(CrmService crmServ, 
                string firestname ,string lastname ,string companyname ,
                string saluation ,string jobtitle,
                string businessphone,string homephone,string mobilephone,
                string ifax,string pager, string emailaddress,
                string description,
                string street1name,string street1stateorprovince,
                string street2name, string address2postalcode,
                string city,
                string sic)
            {   //need receive parameters from access url .
                
                lead newlead = new lead();
    
                //define attibute lead class 
               
    
                //General item fill with parameter;  
                newlead.firstname = firestname;         newlead.salutation = saluation;
                newlead.lastname = lastname;            newlead.jobtitle = jobtitle; 
                newlead.companyname = companyname;  
    
               
               
                //contact information;
                newlead.telephone1 = businessphone;         newlead.fax = ifax;
                newlead.telephone2 = homephone;             newlead.pager = pager;
                newlead.mobilephone = mobilephone;          newlead.emailaddress1 = emailaddress;
    
                newlead.description = description;
    
                //Details of lead items 
               newlead.address1_line1 = street1name;       newlead.address1_stateorprovince = street1stateorprovince;
               newlead.address1_line2 = street2name; newlead.address1_postalcode = address2postalcode;
                
                newlead.address1_city = newlead.address2_city = city ;
    
                newlead.sic = sic;
    
                
                Guid leadId = crmServ.Create(newlead);
    
               
                return leadId; 
    
            }
    My program happens when call the addLead function now (have not test the update lead function).

    I need more sleeping~_~
    Tuesday, August 4, 2009 7:00 AM
  • Hi.

    Cover all your code with

    using (new CrmImpersonator())
    {
    //...
    }
    Truth is opened the prepared mind My blog - http://a33ik.blogspot.com
    Tuesday, August 4, 2009 7:04 AM
    Moderator
  • All my code?
    Sorry that I'm unfamiliar with .net, you means like this:
    using (new CrmImpersonator())
    {
    namespace WebService2
    {
         public partial class WebForm1 : System.Web.UI.Page
         {
    //...
    }
      }
    }
    Is this right?


    I need more sleeping~_~
    Tuesday, August 4, 2009 7:08 AM
  • Hi. Not like this but:


    using (new CrmImpersonator())

    {
    CrmService parameterCrm = getCrmServiceBy(url,username,password,domain);

    if (leadid != null )
    {
    try
    {

    //...

    }
    catch (Exception ex)
    {
    Response.Write(ex.Message);
    }
    }

    Truth is opened the prepared mind My blog - http://a33ik.blogspot.com
    Tuesday, August 4, 2009 7:09 AM
    Moderator
  • Ok, thanks, I will try. Thank you very much. :-)
    I need more sleeping~_~
    Tuesday, August 4, 2009 7:10 AM
  • Hi, 
    I have tryed, but MS Studio says that there are some errors in the line:
    using (new CrmImpersonator())

    Error 11 The type or namespace name 'CrmImpersonator' could not be found (are you missing a using directive or an assembly reference?) D:\www\marketbright\DEV\trunk_old\msDynamicsAdapter\WebService2\CreateLead.aspx.cs 23 24 WebService2
    Is there any packages need imported?

    I need more sleeping~_~
    Tuesday, August 4, 2009 7:15 AM
  • Hi.

    Have you referenced Microsoft.CRM.Sdk.dll and Microsoft.Crm.sdktypeproxy.dll assemlies?
    Truth is opened the prepared mind My blog - http://a33ik.blogspot.com
    Tuesday, August 4, 2009 7:16 AM
    Moderator
  • No, but my asp.net code is develop under Dynamics CRM 3.0, is it still need these two dll?

    Thanks.
    Daniel

    I need more sleeping~_~
    Tuesday, August 4, 2009 7:31 AM
  • Hi.

    I thought you develop page for CRM 4.0. I haven't develop pages for 3.0. I'm sorry but I can't help you....
    Truth is opened the prepared mind My blog - http://a33ik.blogspot.com
    Tuesday, August 4, 2009 7:33 AM
    Moderator
  • I'm sorry that have provide the version of the CRM first, thanks all the same.
    Daniel

    I need more sleeping~_~
    Tuesday, August 4, 2009 7:35 AM
  • Hi,

    So you must have added web reference to CrmService.

    Check if you are passing correct values for !!
     getCrmServiceBy(url,username,password,domain
    );
    You could also try this as well

    <identity impersonate="true"
    userName="domain\user"
    password="password" />


    Regards,
    Nishant Rana
    http://nishantrana.wordpress.com
    Tuesday, August 4, 2009 7:50 AM
  • Thanks a lot, this one solved my issue.
    Monday, December 21, 2015 7:17 AM