Introduction
In Part 2 we will enhance your previously created MVC web application to connect to Microsoft Dynamics 365 and execute basic commands using the Microsoft Dynamics 365 SDK.
Requirements
- S2S Authentication with Dynamics 365 – Part 1
- Open you Visual Studio solution that you created in the previous post.
Web.config
- Add the following to the appSettings section
1 2 3 4 5 6 7 |
<add key="ida:OrganizationHostName" value="https://{0}.crm4.dynamics.com"/> <add key="owin:appStartup" value="<your app namespace>.Startup" /> <add key="ida:ClientSecret" value="KEY HERE" /> |
Login to Azure AD navigate to App >egis<rations select the application you created in Part 1 of this blog. then navigate to Keys.
Copy the newly generated code and past it into the ida:ClientSecret value in your web.config file.
Set Home Controller access as anonymous
- Navigate to \Controllers\HomeController.cs
- Adding the [AllowAnonymous] keyword above your action will give the use the ability to access the page anonymously without Authentication.
1 2 3 4 5 |
[AllowAnonymous] public ActionResult Index() { return View(); } |
Create Contact List link
- Navigate to ../Views/Shared/_Layout.cshtml and add the following code to display the WhoAmI link on our web application.
We will only display this link if the user has logged in and has been authenticated.
1 2 3 4 5 6 7 8 9 |
<ul class="nav navbar-nav"> <li>@Html.ActionLink("Home", "Index", "Home")</li> <li>@Html.ActionLink("About", "About", "Home")</li> <li>@Html.ActionLink("Contact", "Contact", "Home")</li> @if (Request.IsAuthenticated) { <li>@Html.ActionLink("WhoAmI", "Index", "CrmContactList")</li> } </ul> |
Create CrmController
- Right click the Controllers folder and select Add > Controller…
- Select MVC 5 Controller – Empty
- Click Add
- Add the following properties to the CrmController
1 2 3 4 5 6 7 |
private string clientId = ConfigurationManager.AppSettings["ida:ClientId"]; private string authority = ConfigurationManager.AppSettings["ida:AADInstance"] + "common"; private string aadInstance = ConfigurationManager.AppSettings["ida:AADInstance"]; private string OrganizationHostName = ConfigurationManager.AppSettings["ida:OrganizationHostName"]; private string appKey = ConfigurationManager.AppSettings["ida:ClientSecret"]; |
- Create a new method called GetServiceUrl in the CrmController. This method takes the CRM Organisation name and returns the CRM organisations service URL.
1 2 3 4 5 6 7 8 9 10 11 |
private Uri GetServiceUrl(string organizationName) { var organizationUrl = new Uri( string.Format(OrganizationHostName, organizationName) ); return new Uri( organizationUrl + @"/xrmservices/2011/organization.svc/web?SdkClientVersion=8.2" ); } |
- Add the following code to your Index method
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
public ActionResult Index() { string tenantID = ClaimsPrincipal.Current.FindFirst("http://schemas.microsoft.com/identity/claims/tenantid").Value; // Clean organization name from user logged string organizationName = User.Identity.Name.Substring( User.Identity.Name.IndexOf('@') + 1, User.Identity.Name.IndexOf('.') - (User.Identity.Name.IndexOf('@') + 1)); //string crmResourceId = "https://[orgname].crm.microsoftonline.com"; var resource = string.Format(OrganizationHostName, organizationName); // Request a token using application credentials ClientCredential clientcred = new ClientCredential(clientId, appKey); AuthenticationContext authenticationContext = new AuthenticationContext(aadInstance + tenantID); AuthenticationResult authenticationResult = authenticationContext.AcquireToken(resource, clientcred); var requestedToken = authenticationResult.AccessToken; // Invoke SDK using using the requested token using (var sdkService = new OrganizationWebProxyClient( GetServiceUrl(organizationName), false) ) { // Query CRM sdkService.HeaderToken = requestedToken; OrganizationRequest request = new OrganizationRequest() { RequestName = "WhoAmI" }; OrganizationResponse response = sdkService.Execute(request); return View((object)string.Join(",", response.Results.ToList())); } } |
If If you get a error “AuthenticationContext does not contain AcquireToken” make sure that you have added version 2.xx.x of the Microsoft.IdentityModel.Clients.ActiveDirectory assembly reference to your project
Create Crm View
- Right click the Crm folder and select Add > View…
- In the Add View dialog, set the following values:
- Click Add
- Update your view to represent the following code.
1 2 3 4 5 6 7 8 9 10 |
@model string @{ ViewBag.Title = "CRM SDK Contact List"; } <h2>@ViewBag.Title.</h2> <p>Connected and executed sdk command.</p> <p>Value: @Model</p> |
- Press F5 and test you application.
Source Code