Reputation: 79
My requirement is to Sync my database record to Dynamic 365 and also to authenticate the user.
I am trying with this code but it is not helping me out, Please help me to connect and resolve it
string api = "https://ujwl.api.crm8.dynamics.com/api/data/v9.0";
AuthenticationParameters ap = AuthenticationParameters.CreateFromResourceUrlAsync(new Uri(api)).Result;
var creds = new ClientCredential("xxx", "xxx");
AuthenticationContext authContext = new AuthenticationContext(ap.Authority);
var token = authContext.AcquireTokenAsync(ap.Resource, creds).Result.AccessToken;
using (HttpClient httpClient = new HttpClient())
{
httpClient.Timeout = new TimeSpan(0, 2, 0);
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);
HttpResponseMessage response = await httpClient.GetAsync("https://ujwl.api.crm8.dynamics.com/api/data/v9.0/accounts?$top=2");
}
I am not able to authenticate user nor getting the data from the server. Is this approach correct or I need to go with the different approach.
Upvotes: 2
Views: 406
Reputation: 79
Here is the solution of above question which I got it by digging into Microsoft Docs
string resource = "https://ujwl.crm8.dynamics.com/";
string clientId = "aaaa1111-xxxx-yyyy-zzzz-2222bbbbc692";
string redirectUrl = "https://localhost:44345/";
AuthenticationContext authContext = new AuthenticationContext("https://login.windows.net/common", false);
AuthenticationResult result = authContext.AcquireToken(resource, clientId, new Uri(redirectUrl));
HttpResponseMessage response = null;
using (HttpClient httpClient = new HttpClient())
{
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;
httpClient.Timeout = new TimeSpan(0, 2, 0);
httpClient.BaseAddress = new Uri("https://ujwl.crm8.dynamics.com/");
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", result.AccessToken);
response = await httpClient.GetAsync("https://ujwl.api.crm8.dynamics.com/api/data/v9.0/contacts");
}
Upvotes: 1
Reputation: 3541
I have only done it on-premise, so I could be wrong... But looks to me that you could/should use the MS c# API. The namespaces are Microsoft.Xrm.Sdk
and can be downloaded at he MS page.
Please note that this following code is partial, for on-premise, probably online would have a different connectionstring/parameters.
private IOrganizationService _orgService = null;
public CrmHelper(string serverHost, int port, string orgName, bool usarSSL, string dominio, string usuario, string password, bool claims)
{
/* Nomenclatura usando CLAIMS
* ServiceUri=https://[server:puerto]/[NombreOrganizacion];
* AuthType=IFD;
* Domain=[Dominio];
* UserName=[Dominio]\[usuario];
* Password=[Password];
* LoginPrompt=Never;
* Organization=[NombreOrganizacion] */
/* Nomenclatura sin usar CLAIMS:
* Url=http://[direccionIP]/[NombreOrganizacion];
* Domain=[Dominio];
* Username=[usuario];
* Password=[Password];
* authtype=AD
*/
string formato = claims ? @"ServiceUri={0}://{1}{2}/{3};AuthType=IFD;Domain={4};UserName={4}\{5};Password={6};LoginPrompt=Never;Organization={3}" : @"Url={0}://{1}{2}/{3}; Domain={4}; Username={5}; Password={6}; authtype=AD";
string connectionString = string.Format(formato, (usarSSL) ? "https" : "http", serverHost, (port == 80) ? "" : ":" + port.ToString(), orgName, dominio, usuario, password);
CrmServiceClient conn = new Xrm.Tooling.Connector.CrmServiceClient(connectionString);
// Cast the proxy client to the IOrganizationService interface.
_orgService = (IOrganizationService)conn.OrganizationWebProxyClient != null ? (IOrganizationService)conn.OrganizationWebProxyClient : (IOrganizationService)conn.OrganizationServiceProxy;
if (_orgService == null || !conn.IsReady)
{
throw new ArgumentException("No se ha podido conectar al CRM con los parametros facilitados");
}
}
Upvotes: 1