We are using single sign on to login to the Epicor System. We have enabled windows authentication from IIS manager. Please see below screen 1.
Also, we have configured internet option as follows.
When we are trying to access Rest Service from client machine using URL, it opened json file without any issue.
But when We try to access rest service from C#.code in client machine, following error was appeared. This code was copied from other thread in this site. It is attached below. We passed windows login and password to this method. (This is our administrator account)
Error
401 - Unauthorized: Access is denied due to invalid credentials.
You do not have permission to view this directory or page using the credentials that you supplied.
 Code static void Main(string[] args) { PrepareHeaders(); QueryCustomer(); } private static void QueryCustomer()
{
//Stand up which service to call
HttpRequestMessage request = new HttpRequestMessage(
HttpMethod.Post,
ServiceUrl + "/Erp.Bo.CustomerSvc/GetByID");
//Add the json payload
request.Content =
new StringContent(
JsonConvert.SerializeObject(
new
{
custNum = 2
}),
Encoding.Default,
"application/json");
try
{
var stopwatch = new Stopwatch();
stopwatch.Start();
//Make the call
//ServicePointManager.ServerCertificateValidationCallback =
// delegate(object s, X509Certificate certificate,
// X509Chain chain, SslPolicyErrors sslPolicyErrors)
// { return true; }; // Put By Nuwan to Avoid the remote certificate is invalid according to the validation procedure
var response = httpClient.SendAsync(request).Result;
stopwatch.Stop();
var jsonResponse = GetObject(response);
Console.WriteLine("Success:{0}", response.StatusCode == HttpStatusCode.Created);
Console.WriteLine("Response:{0}", jsonResponse.ToString());
Console.WriteLine("Duration:{0}", stopwatch.Elapsed.TotalMilliseconds);
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
Console.ReadLine();
}
public static JObject GetObject(HttpResponseMessage response)
{
string stringContent = string.Empty;
if (response.Content != null)
{
stringContent = response.Content.ReadAsStringAsync().Result;
}
if (response.IsSuccessStatusCode)
{
if (!string.IsNullOrEmpty(stringContent))
return JObject.Parse(stringContent);
}
else
{
throw new HttpRequestException(!string.IsNullOrEmpty(stringContent) ? stringContent : response.ReasonPhrase);
}
return null;
}
/// <summary>
/// Set credentials and Session context
/// </summary>
private static void PrepareHeaders()
{
//var httpClientHandler = new HttpClientHandler()
//{
// Credentials = new NetworkCredential(userName, password, "STRGRP"),
//};
//var httpClient = new HttpClient(httpClientHandler);
//httpClient.DefaultRequestHeaders.Accept.Clear();
//httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
//basic authorization example - just user and password in the header.
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic",
Convert.ToBase64String(Encoding.ASCII.GetBytes(
"{userName}:{password}")));
//another option - token authentication - needs to be enabled in Epicor Admin Console
//comment out previous basic authorization example and uncomment these 2 lines
//string token = ObtainToken();
// httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);
//header to set company & plant(site)
var hdrCallSettings = new CallSettings(companyID, plantID, "", "");
httpClient.DefaultRequestHeaders.Add(hdrCallSettings.Name, JsonConvert.SerializeObject(hdrCallSettings));
}
}
Can you check this?