
Hello,
We are trying to build a custom portal (web) through which we want to login external customer (having Dynamics CRM) and show contacts from their CRM. We tried to search for solution and we found that "Azure B2B" authentication is a ideal solution for our requirement. Our issue is we are not able to find code which can query data from external customer's CRM. We followed below article.
Any help appreciated.
Thanks in Advance !
Hi,
You can use web api to get the contact from CRM.
To connect to web api you first need Access token. Register your application in azure and grant crm permission.
Follow below article to Register Application in Azure Active DIrectory.
docs.microsoft.com/.../walkthrough-register-app-azure-active-directory
Once this setup is completed, copy Application ID and Create Application use in CRM then grant appropriate permission/security role.
Manually create a Common Data Service application user
The procedure to create this user is different from creating a licensed user. Use the following steps:
Navigate to Settings > Security > Users
In the view drop-down, select Application Users.
Click New. Then verify that you are using the Application user form.
If you do not see the Application ID, Application ID URI and Azure AD Object ID fields in the form, you must select the Application User form from the list:
Add the appropriate values to the fields:
Field Value
User Name A name for the user
Application ID The Application ID value for the application registered with Azure AD.
Full Name The name of your application.
Primary Email The email address for the user.
The Application ID URI and Azure AD Object ID fields are locked and you cannot set values for these fields.
When you create this user the values for these fields will be retrieved from Azure AD based on the Application ID value when you save the user.
Associate the application user with the custom security role you created.
See this link here for more details - docs.microsoft.com/.../authenticate-oauth
class SampleProgram
{
private static string serviceUrl = "yourorg.crm.dynamics.com";
private static string clientId = "51f81489-12ee-4a9e-aaae-a2591f45987d";
private static string userName = "you@yourorg.onmicrosoft.com";
private static string password = "yourpassword";
static void Main(string[] args)
{
AuthenticationContext authContext =
new AuthenticationContext("login.microsoftonline.com/common", false);
UserCredential credential = new UserCredential(userName, password);
AuthenticationResult result = authContext.AcquireToken(serviceUrl, clientId, credential);
//The access token
string accessToken = result.AccessToken;
using (HttpClient client = new HttpClient()) {
client.BaseAddress = new Uri(serviceUrl);
client.Timeout = new TimeSpan(0, 2, 0); //2 minutes
client.DefaultRequestHeaders.Add("OData-MaxVersion", "4.0");
client.DefaultRequestHeaders.Add("OData-Version", "4.0");
client.DefaultRequestHeaders.Accept.Add(
new MediaTypeWithQualityHeaderValue("application/json"));
HttpRequestMessage request =
new HttpRequestMessage(HttpMethod.Get, "/api/data/v9.0/WhoAmI");
//Set the access token
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
HttpResponseMessage response = client.SendAsync(request).Result;
if (response.IsSuccessStatusCode)
{
//Get the response content and parse it.
JObject body = JObject.Parse(response.Content.ReadAsStringAsync().Result);
Guid userId = (Guid)body["UserId"];
Console.WriteLine("Your system user ID is: {0}", userId);
}
}
}
In my sample code i am executing whoami request, you can add your code to get contacts by passing access token in header.