You went through all the motions of authorization endpoints, return URIs, codes and whatsnot and finally got the magic mushroom authorization token. Now what?
Option 1
Use HttpClient to send requests to the CRM endpoints, add authorization token to the header of every request:
HttpClient httpClient = new HttpClient(); httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
Once reply is received, go through the pain of manually parsing the return values.
Option 2
Download CSDL file for OrganizationService, manually add a service reference to your project, generate custom organization context, then add some fairy dust:
var service = new CrmOrgContext(new Uri(ODataUrl)); service.SendingRequest2 += (sender, args) => args.RequestMessage.SetHeader("Authorization", "Bearer " + token); // active accounts var accounts = service.AccountSet.Where( a => a.StateCode.Value == 0);
Better but manual processing will still be required in some cases and we’ll have to refactor all our existing data access code.
Option 3
Spend 10 minutes reading What’s new for developers in CRM 2015, find new namespace Microsoft.Xrm.Sdk.WebServiceClient containing OrganizationWebProxyClient and change your code to:
var orgService = new OrganizationWebProxyClient( new Uri(serviceUrl), false) { HeaderToken = token, SdkClientVersion = "7.0" }; var userId = ((WhoAmIResponse)orgService.Execute( new WhoAmIRequest())).UserId;
These classes support executing message requests through the /web endpoint of the Discovery.svc or Organization.svc when authenticated with OAuth
Now old code works without any changes and authorization is transparent. Which is exactly what we’ve been trying to do all these years.