ADFS Login - crmservice
-
Thursday, April 12, 2012 4:01 PM
I'm trying to connect to the following service:
https://crm2011.xyz.com/mscrmservices/2007/crmservice.asmx
Through the browser I can get to this service if I login to CRM ( and ADFS ) first.
In my code I'm getting a 401 when I connect to the service. How would I connect to CRM in Code, similar to what I do in my browser so that I get my ADFS login?
All Replies
-
Thursday, April 12, 2012 4:33 PM
I assume you are using IFD, in which case you need to provide a security token in order to be authenticated.
The following link has sample code for IFD authentication.
Hope this helps.
Daniel Cai | http://danielcai.blogspot.com | @danielwcai
-
Friday, April 13, 2012 9:34 AM
use following code....
using System; using System.Xml; using System.Text; using System.Web.Services.Protocols; // Microsoft Dynamics CRM namespaces. // Note that the Visual Studio project name is IFD_Authentication. using IFD_Authentication.CrmSdk; using IFD_Authentication.CrmSdk.Discovery; public class IFDConnection { // A CrmService reference. public readonly CrmService CrmService = null; // URL of the Web application. public readonly string WebApplicationUrl = String.Empty; // GUID of the user's organization. public readonly Guid OrganizationId = Guid.Empty; /// <summary> /// Authenticate the user using IFD (Internet Facing Deployment). The class /// constructor sets the values of the public variables (CrmService, etc). /// </summary> /// <param name="organization">Name of the user's organization.</param> /// <param name="server">Microsoft Dynamics CRM server URL. /// For example: https://myserver.</param> /// <param name="domain">Name of the domain hosting the user's system /// account.</param> /// <param name="username">User's system account name.</param> /// <param name="password">User's account password.</param> public IFDConnection(string organization, string server, string domain, string username, string password) { //Remove any trailing forward slash from the end of the server URL. server = server.TrimEnd(new char[] { '/' }); // Initialize an instance of the CrmDiscoveryService Web service proxy. CrmDiscoveryService disco = new CrmDiscoveryService(); disco.Url = server + "/MSCRMServices/2007/SPLA/CrmDiscoveryService.asmx"; //Retrieve a list of available organizations. RetrieveOrganizationsRequest orgRequest = new RetrieveOrganizationsRequest(); orgRequest.UserId = domain + "\\" + username; orgRequest.Password = password; RetrieveOrganizationsResponse orgResponse = (RetrieveOrganizationsResponse)disco.Execute(orgRequest); //Find the desired organization. foreach (OrganizationDetail orgdetail in orgResponse.OrganizationDetails) { if (orgdetail.OrganizationName == organization) { //Retrieve the ticket. RetrieveCrmTicketRequest ticketRequest = new RetrieveCrmTicketRequest(); ticketRequest.OrganizationName = organization; ticketRequest.UserId = domain + "\\" + username; ticketRequest.Password = password; RetrieveCrmTicketResponse ticketResponse = (RetrieveCrmTicketResponse)disco.Execute(ticketRequest); //Create the CrmService Web service proxy. CrmAuthenticationToken sdktoken = new CrmAuthenticationToken(); sdktoken.AuthenticationType = 2; sdktoken.OrganizationName = organization; sdktoken.CrmTicket = ticketResponse.CrmTicket; CrmService = new CrmService(); CrmService.CrmAuthenticationTokenValue = sdktoken; CrmService.Url = orgdetail.CrmServiceUrl; WebApplicationUrl = orgdetail.WebApplicationUrl; OrganizationId = orgdetail.OrganizationId; break; } } } }
- Proposed As Answer by Dynamics CRM 31 Monday, April 16, 2012 5:20 AM
-
Wednesday, April 25, 2012 2:43 PM
Sorry for the slow response. The code given is giving me the same problem. (Milan is your solution not the same as the code in Daniel's link?)
RetrieveOrganizationsResponse orgResponse = (RetrieveOrganizationsResponse)disco.Execute(orgRequest);The above line still gives me a 401 error.
When I visit the discovery service through my browser
https://crm2011.xyz.com/MSCRMServices/2007/ad/CrmDiscoveryService.asmx
I'm getting the same error unless I first login to Crm. Once I've gone through my adfs login screen when logged onto CRM. I can visit this service.
-
Wednesday, April 25, 2012 3:08 PMModeratorThe above code is for CRM 4.0, and is trying to use the CRM 4.0 endpoint. Have you tried using the CRM 2011 endpoints (as you refer to ADFS) ?
Microsoft CRM MVP - http://mscrmuk.blogspot.com http://www.excitation.co.uk
- Edited by DavidJennawayMVP, Moderator Wednesday, April 25, 2012 3:09 PM
-
Wednesday, April 25, 2012 3:11 PM
Hi david,
I tried using Crm2011 endPoints through the available SDK ....
In short the SDK needs .Net 4 while I'm making these calls from SharePoint 2010 which does not support .Net 4. The easiest workarounds seemed to be to use the Crm 4.0 services.
-
Wednesday, January 16, 2013 4:03 PMHm..thanks, but what if we are using 2011 crm? How to get service?
-
Wednesday, January 16, 2013 4:29 PM
Hello Pieter,
I made a connection to the legacy web service with the following code. The CRMSDK in the code i a web reference to an arbitrary CRM4 web service (http://<crmserver>:<port>/mscrmservices/2007/ad/crmdiscoveryservice.asmx?WSDL).
In the code below you obviously need to change "USERNAME", "PASSWORD" and "ORGNAME" to the correct names. I have also added the following references;
using Microsoft.Crm.Sdk;
using Microsoft.Crm.SdkTypeProxy;
using Microsoft.Crm.Sdk.Query;
using Microsoft.Crm.Sdk.Metadata;static void Main(string[] args) { CRMSDK.CrmDiscoveryService disco = new CRMSDK.CrmDiscoveryService(); disco.Url = "https://crm2011.xyz.com/MSCRMServices/2007/SPLA/CrmDiscoveryService.asmx"; disco.Credentials = new System.Net.NetworkCredential("USERNAME", "PASSWORD"); CRMSDK.RetrieveOrganizationsRequest orgreq = new CRMSDK.RetrieveOrganizationsRequest(); orgreq.UserId = "USERNAME"; orgreq.Password = "PASSWORD"; CRMSDK.RetrieveOrganizationsResponse resp = (CRMSDK.RetrieveOrganizationsResponse)disco.Execute(orgreq); CRMSDK.OrganizationDetail orginfo = null; foreach (CRMSDK.OrganizationDetail orgdet in resp.OrganizationDetails) { if(orgdet.OrganizationName.Equals("ORGNAME")) orginfo = orgdet; } if (orginfo == null) return; CRMSDK.RetrieveCrmTicketRequest tickreq = new CRMSDK.RetrieveCrmTicketRequest(); tickreq.OrganizationName = orginfo.OrganizationName; tickreq.UserId = "USERNAME"; tickreq.Password = "PASSWORD"; CRMSDK.RetrieveCrmTicketResponse tresp = (CRMSDK.RetrieveCrmTicketResponse)disco.Execute(tickreq); Microsoft.Crm.SdkTypeProxy.CrmService crmService = new CrmService(); CrmAuthenticationToken token = new CrmAuthenticationToken(); token.AuthenticationType = AuthenticationType.Spla; token.CrmTicket = tresp.CrmTicket; token.OrganizationName = tresp.OrganizationDetail.OrganizationName; crmService.Url = tresp.OrganizationDetail.CrmServiceUrl; crmService.CrmAuthenticationTokenValue = token; WhoAmIRequest wreq = new WhoAmIRequest(); WhoAmIResponse wresp = (WhoAmIResponse)crmService.Execute(wreq); Console.WriteLine(wresp.UserId); Console.ReadKey(); }
Rickard Norström Developer CRM-Konsulterna
http://www.crmkonsulterna.se
Swedish Dynamics CRM Forum: http://www.crmforum.se
My Blog: http://rickardnorstrom.blogspot.se