Quantcast
Channel: Web API
Viewing all articles
Browse latest Browse all 4850

How Web API console application connect to IFD resource server(CRM server)?

$
0
0

Hello,

I'm trying to build a Web API console application to consume my resource server(CRM)'s web service. But I have difficulty to connect my resource server, which is my CRM server(ADFS Authentication).

All information I have is: Resource Server Url, Server Domain, UserName and Password, but I don't have ADFS information. How can I get all ADFS information by code based on the information I have? The following is my code which needs to register with Microsoft Azure AD to get Client ID and RedirectUrl. But in my case, I don't want to use Azure AD.

class Program
	{
		//This was registered in Azure AD as a WEB APPLICATION AND/OR WEB API

		//Azure Application Client ID
		private const string _clientId = "00000000-0000-0000-0000-000000000000";
		// Azure Application REPLY URL - can be anything here but it must be registered ahead of time
		private const string _redirectUrl = "http://localhost/CrmWebApi";
		//CRM URL
		private const string _serviceUrl = "https://org.crm.dynamics.com";
		//O365 used for authentication w/o login prompt
		private const string _username = "administrator@org.onmicrosoft.com";
		private const string _password = "password";
		//Azure Directory OAUTH 2.0 AUTHORIZATION ENDPOINT
		private const string _authority = "https://login.microsoftonline.com/00000000-0000-0000-0000-000000000000";

		private static AuthenticationResult _authResult;

		static void Main(string[] args)
		{
			AuthenticationContext authContext =
				new AuthenticationContext(_authority, false);

			//Prompt for credentials
			//_authResult = authContext.AcquireToken(
			//	_serviceUrl, _clientId, new Uri(_redirectUrl));

			//No prompt for credentials
			UserCredential credentials = new UserCredential(_username, _password);
			_authResult = authContext.AcquireToken(
				_serviceUrl, _clientId, credentials);

			Task.WaitAll(Task.Run(async () => await DoWork()));
		}

		private static async Task DoWork()
		{
			try
			{
				using (HttpClient httpClient = new HttpClient())
				{
					httpClient.BaseAddress = new Uri(_serviceUrl);
					httpClient.Timeout = new TimeSpan(0, 2, 0);
					httpClient.DefaultRequestHeaders.Add("OData-MaxVersion", "4.0");
					httpClient.DefaultRequestHeaders.Add("OData-Version", "4.0");
					httpClient.DefaultRequestHeaders.Accept.Add(
						new MediaTypeWithQualityHeaderValue("application/json"));
					httpClient.DefaultRequestHeaders.Authorization =
						new AuthenticationHeaderValue("Bearer", _authResult.AccessToken);

					//Unbound Function
					//The URL will change in 2016 to include the API version - api/data/v8.0/WhoAmI
					HttpResponseMessage whoAmIResponse =
						await httpClient.GetAsync("api/data/WhoAmI");
					Guid userId;
					if (whoAmIResponse.IsSuccessStatusCode)
					{
						JObject jWhoAmIResponse =
							JObject.Parse(whoAmIResponse.Content.ReadAsStringAsync().Result);
						userId = (Guid)jWhoAmIResponse["UserId"];
						Console.WriteLine("WhoAmI " + userId);
					}
					else
						return;
				}
			}
			catch (Exception ex)
			{
				throw;
			}
		}
	}

I think the problem is coming from this line because I don't have  Client ID and RedirectUrl information.

httpClient.DefaultRequestHeaders.Authorization =
						new AuthenticationHeaderValue("Bearer", _authResult.AccessToken);

So Can anyone help me with this? Once agian, this is sonsole API, not web application. As long as it can talk to my CRM server, I'll be happy.

Thanks in advance.


Viewing all articles
Browse latest Browse all 4850


<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>