Sorry if this has already been answered but I have googled and BINGed for days and not found a way to do this.
We recently upgraded to CRM 2016. As a part of this, I rewrote a good many automated processes, (many are run as windows services), some run in an external internet facing application. This all went well. Now we need to implement outlook integration, which requires us to use ADFS for single signon. However, I have not been able to find a way to authenticate without a ui prompting for credentials. This means that my windows services won't work because of the login prompt. I really need to pass the credientials programatically. Is this possible? If not, I am faced with having to rewrite all the integration code to use IOrganizationService and move away from the simple rest interface which is already fully tested and very stable.
The error I get when trying to pass credetials is:
The authorization server does not support the requested 'grant_type': 'password'.
Here is the code I am working with:
class clsTest
{
private static WebClient client;
private static string authorityURL = "sts.somedomain.com/.../authorize";
private static string resource = "https://crm.somedomain.com/";
private static string redirectUrl = "externalapp.somedomain.com";
private static string clientID = "some-unique-id-registred-in-adfs";
static void Main(string[] args)
{
authorize("domain\\userid", "password"); //GENERATES EXCEPTION
authorizeWithUI(); //WORKS BUT PROMPTS FOR LOGIN
}
private static void authorize(string user, string password)
{
UserCredential cred = new UserCredential(user, password);
AuthenticationContext _authenticationContext = new AuthenticationContext(authorityURL, false);
AuthenticationResult result = _authenticationContext.AcquireTokenAsync(resource, clientID, cred).Result;
string rtn = new AuthenticationHeaderValue("Bearer", result.AccessToken).ToString();
}
private static void authorizeWithUI()
{
AuthenticationContext authContext = new AuthenticationContext(authorityURL, false);
AuthenticationResult result = authContext.AcquireToken(resource, clientID, new Uri(redirectUrl));
AuthenticationHeaderValue ahv = new AuthenticationHeaderValue("Bearer", result.AccessToken);
WebClient client = getNewHttpClient(resource);
client.Headers[HttpRequestHeader.Authorization] = ahv.ToString();
client.Headers[HttpRequestHeader.ContentType] = "application/json";
client.Headers[HttpRequestHeader.Accept] = "application/json";
string json = client.DownloadString(resource + "stringmaps?$filter=attributename eq ('new_plstrainer') and objecttypecode eq 'opportunity'");
string rtn = ahv.ToString();
}
private static WebClient getNewHttpClient(string webAPIBaseAddress = "crm.somedomain.com/.../v8.2")
{
client = new WebClient();
client.Headers[HttpRequestHeader.ContentType] = "application/json";
client.Headers[HttpRequestHeader.Accept] = "application/json";
client.BaseAddress = webAPIBaseAddress;
return client;
}
}
Thanks
Randy S.