Hello everybody,
I am going to call CRM(online) Web API from my public web site written in ASP .Net MVC. Site visitor can not enter Azure AD credentials and I can not use Microsoft.IdentityModel.Clients.ActiveDirectory library.
So, I started following steps described in this post: http://jopx.blogspot.cz/2017/06/using-postman-and-dynamics-365-web-api.html.
I receive token from Azure AD on Postman tool and I can request CRM Web API using this access_token.
But I can't receive a token from Azure AD in web site code. I receive error: (401) Unauthorized.
var tokenRequestUrl = string.Format(@"login.microsoftonline.com{0}/oauth2/token", azureTenantGuid); // Connect to the authentication server var request = (HttpWebRequest)WebRequest.Create(tokenRequestUrl); request.Method = "POST"; // Write our request to the request body using (var reqStream = await request.GetRequestStreamAsync()) { var postData = string.Format(@"client_id={0}&resource={1}&username={2}&password={3}&grant_type=password&client_secret={4}", clientId, crmUrl, userName, password, clientSecret); var postBytes = new ASCIIEncoding().GetBytes(postData); reqStream.Write(postBytes, 0, postBytes.Length); reqStream.Close(); } // Call the authentication server and parse out the response using (var response = (HttpWebResponse)request.GetResponse()) { var dataStream = response.GetResponseStream(); if (dataStream != null) { var reader = new StreamReader(dataStream); var json = reader.ReadToEnd(); ... } }
Is it possible to call from Web site? What is wrong in this code? Or do I need to make some settings on my local dev environment?
*This post is locked for comments