locked
Custom Application 401 Error RRS feed

  • Question

  •  

    Hi,

    I'm really struggling to understand how CRM Authentification works. I've been working on this for days and failed miserably.

    I created an new web application and put it in the ISV folder of my crm website. The application is using the same Application Pool as the crm application. I'm using IFD. I did not change anything in the Web.Config.

    I try to run this piece of code :

     

        Dim orgname As String
        Dim crmurl As String
        Dim deployurl As String
        Dim offline As Boolean
    
        'Determine Offline State from Host Name
        If (Request.Url.Host.ToString() = "127.0.0.1") Then
          offline = True
          Dim regkey As RegistryKey = Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\MSCRMClient")
          orgname = regkey.GetValue("ClientAuthOrganizationName").ToString()
          Dim portnumber As String = regkey.GetValue("CassiniPort").ToString()
    
          Dim baseurl As String = "http://localhost:" & portnumber & "/mscrmservices/2007/"
          crmurl = baseurl & "crmservice.asmx"
          deployurl = baseurl & "crmdeploymentservice.asmx"
        Else
          offline = False
    
          'Retrieve the URLs from the Registry
          Dim regkey As RegistryKey = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\MSCRM")
          Dim ServerUrl As String = regkey.GetValue("ServerUrl").ToString()
    
          crmurl = ServerUrl & "/2007/crmservice.asmx"
          deployurl = ServerUrl & "/2007/crmdeploymentservice.asmx"
    
          'Retrieve the Query String from the current URL
          If (Request.QueryString("orgname") Is Nothing) Then
            orgname = String.Empty
          Else
            'Query String
            Dim orgquerystring As String = Request.QueryString("orgname").ToString()
            If (String.IsNullOrEmpty(orgquerystring)) Then
              orgname = String.Empty
            Else
              orgname = orgquerystring
            End If
    
            If (String.IsNullOrEmpty(orgname)) Then
              'Windows Auth URL
              If (Request.Url.Segments(2).TrimEnd("/").ToLower() = "isv") Then
                orgname = Request.Url.Segments(1).TrimEnd("/").ToLower()
              End If
    
              'IFD URL
              If (String.IsNullOrEmpty(orgname)) Then
                Dim url As String = Request.Url.ToString().ToLower()
                Dim start As Integer = url.IndexOf("://") + 3
                orgname = url.Substring(start, url.IndexOf(".") - start)
              End If
            End If
          End If
        End If
    
        Using (New CrmImpersonator())
          Dim token As CrmAuthenticationToken
    
          If (offline = True) Then
            token = New CrmAuthenticationToken
          Else
            token = CrmAuthenticationToken.ExtractCrmAuthenticationToken(Context, orgname)
          End If
    
          token.OrganizationName = orgname
    
          If (HttpContext.Current.User.Identity.AuthenticationType = "CrmPostAuthentication") Then 'IFD
            token.AuthenticationType = 2
          Else
            token.AuthenticationType = 0
          End If
    
          'Create the Service
          Dim service As CrmService = New CrmService()
          service.Credentials = System.Net.CredentialCache.DefaultCredentials
          service.CrmAuthenticationTokenValue = token
          service.Url = crmurl
    
          Dim whoAmI As WhoAmIResponse = service.Execute(New WhoAmIRequest())
          If Not whoAmI Is Nothing Then
            Me.TextBox1.Text = String.Format("I am {0} from the {1} business unit.", whoAmI.UserId, whoAmI.BusinessUnitId)
          End If
    End Using
    

     

    I'm getting this error :

    Exception information: 
      Exception type: WebException 
      Exception message: The request failed with HTTP status 401: Unauthorized. 
     
    Request information: 
      Request URL: http://XXXXXX:5555/ISV/IVT/ManageCampaignResponses.aspx?orgname=Activa&userlcid=1033&orglcid=1033&type=4400&typename=campaign&id=3b0E6130FA-EAB4-DF11-AEA2-A35FA8C91EC43d 
      Request path: /ISV/IVT/ManageCampaignResponses.aspx 
      User host address: 212.198.125.97 
      User: {9b8b3ef3-fe8d-df11-a308-99dc69b356c5} 
      Is authenticated: True 
      Authentication Type: CrmPostAuthentication 
      Thread account name: NT AUTHORITY\NETWORK SERVICE 
     
    Thread information: 
      Thread ID: 4 
      Thread account name: NT AUTHORITY\NETWORK SERVICE 
      Is impersonating: False 
      Stack trace:  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)
      at Microsoft.Crm.SdkTypeProxy.CrmService.Execute(Request Request)
      at DynamicsCRMActiva.ManageCampaignResponses.LoadData() in C:\Projects\DynamicsCRMActiva\DynamicsCRMActiva\ManageCampaignResponses.aspx.vb:line 91
      at DynamicsCRMActiva.ManageCampaignResponses.WebForm1_Load(Object sender, EventArgs e) in C:\Projects\DynamicsCRMActiva\DynamicsCRMActiva\ManageCampaignResponses.aspx.vb:line 361
      at System.Web.UI.Control.OnLoad(EventArgs e)
      at System.Web.UI.Control.LoadRecursive()
      at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
    

    I also tried creating a simple web project doing exactly what they say on this site : http://www.xrmlinq.com/kb/how-to-publish-link-customaspx-dynamics-crm/ and I get a 401 error with that example too. When I'm on the server, if I try the URL http://localhost:5555 it works, when I try the URL http://localhost:5555/mscrmservices/2007/CrmServiceWsdl.aspx is also works. 

    I however did manage to use the crmService as instructed on this website : http://msdn.microsoft.com/en-us/library/cc151010.aspx.

     

    I just don't understand what it is that I'm doing wrong...

    Tuesday, September 14, 2010 2:54 PM

Answers

  • Nevermind, I got this http://www.xrmlinq.com/kb/how-to-publish-link-customaspx-dynamics-crm/  to work. Phew... But that's about it.
    Tuesday, September 14, 2010 3:46 PM
  • The one thing I would definitely drop is the whole business with "CrmPostAuthentication".

    Remember, code is executed on the CRM server itself and therefore authentication you need to use to get the service will always be AD, i.e. token.AuthenticationType = 0, regardless on what authentication is used by CRM site when your code is executed. The only thing that depends on IFD vs AD is the way orgname is extracted.

    Apart from that I'd double check impersonation settings.

    Cheers



    George Doubinski, MVP http://crm.georged.id.au
    Wednesday, September 15, 2010 3:19 AM
    Moderator

All replies

  • Nevermind, I got this http://www.xrmlinq.com/kb/how-to-publish-link-customaspx-dynamics-crm/  to work. Phew... But that's about it.
    Tuesday, September 14, 2010 3:46 PM
  • The one thing I would definitely drop is the whole business with "CrmPostAuthentication".

    Remember, code is executed on the CRM server itself and therefore authentication you need to use to get the service will always be AD, i.e. token.AuthenticationType = 0, regardless on what authentication is used by CRM site when your code is executed. The only thing that depends on IFD vs AD is the way orgname is extracted.

    Apart from that I'd double check impersonation settings.

    Cheers



    George Doubinski, MVP http://crm.georged.id.au
    Wednesday, September 15, 2010 3:19 AM
    Moderator