.Create ASP .NET Web API to create lead in dynamic CRM And consume WEB API in console application using HTTP Client with Basic Authentication.
What is Web API?
The ASP.NET Web API is an extensible framework for building HTTP based services that can be accessed in different applications on different platforms such as web, windows, mobile, etc.
Why Web API?
To consume third party data using mobile devices, tablets, browsers Web API is very usefull. Web API is easy to consume, less configuration required as compare to consume web services. Web API returns data in JSON as well as XML format.
In this blog, we are going to learn how to consume Web API in asp. Net console application using HTTP client with basic authentication.
In this blog, we are creating new lead in Dynamic CRM using ASP.Net Web API.
First, we will create new Web API then we are going to consume the API.
Please follow below steps to create Web API and consume it in Console Application.
Create new project in visual studio File – New Projet – ASP .NET Web Application – Select empty template – checked checkbox for Web API - OK.
Create new controller rename as LeadController Right click on Controller folder – Add – Controller,
Add below assembly references,
- Microsoft.Xrm.Sdk
- Microsoft.Xrm.Client
Configure connection string to connect with Dynamic CRM in App.config file
- <connectionStrings>
- <add name="CRMConnectionString" connectionString="Url=https://***.crm8.dynamics.com; Username=***; Password=***; AuthType=Office365;" />
- </connectionStrings>
For Basic Authentication create new class file as BasicAuthenticationAttribute.cs and add below code.
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Net;
- using System.Net.Http;
- using System.Security.Principal;
- using System.Threading;
- using System.Web;
- using System.Web.Http.Controllers;
- using System.Web.Http.Filters;
- namespace TrialWebAPI {
- public class BasicAuthenticationAttribute: AuthorizationFilterAttribute {
- public override void OnAuthorization(HttpActionContext actionContext) {
- if (actionContext.Request.Headers.Authorization != null) {
- var authToken = actionContext.Request.Headers.Authorization.Parameter;
- // decoding authToken we get decode value in 'Username:Password' format
- var decodeauthToken = System.Text.Encoding.UTF8.GetString(Convert.FromBase64String(authToken));
- // spliting decodeauthToken using ':'
- var arrUserNameandPassword = decodeauthToken.Split(':');
- // at 0th postion of array we get username and at 1st we get password
- if (IsAuthorizedUser(arrUserNameandPassword[0], arrUserNameandPassword[1])) {
- // setting current principle
- Thread.CurrentPrincipal = new GenericPrincipal(new GenericIdentity(arrUserNameandPassword[0]), null);
- } else {
- actionContext.Response = actionContext.Request.CreateResponse(HttpStatusCode.Unauthorized);
- }
- } else {
- actionContext.Response = actionContext.Request.CreateResponse(HttpStatusCode.Unauthorized);
- }
- }
- public static bool IsAuthorizedUser(string Username, string Password) {
- // In this method we can handle our database logic here...
- return Username == "Akash" && Password == "Vidhate";
- }
- }
- }
Create new class file as LeadClass.cs and add below code
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- namespace TrialWebAPI.Models {
- public class LeadClass {
- public string LeadId { get; set; }
- public string Subject { get; set; }
- public string FirstName { get; set; }
- public string LastName { get; set; }
- public string Company { get; set; }
- public string Website { get; set; }
- public Address Address { get; set; }
- }
- public class Address {
- public string Address_Line1 { get; set; }
- public string Address_Line2 { get; set; }
- public string Address_Line3 { get; set; }
- public string City { get; set; }
- public string State { get; set; }
- public string Postal_Code { get; set; }
- public string Country { get; set; }
- }
- }
Add below code in LeadController.cs file,
[BasicAuthentication] with GET and POST method is used for authentication purpose for API security.

Mahesh AllePosted Aug 9, 2019, 9:53 AM
Explain the code as it will help us to understand how code is working.