<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Getting Token (generateTorken) in C# from Windows Authenticated Enterprise in ArcGIS REST APIs and Services Questions</title>
    <link>https://community.esri.com/t5/arcgis-rest-apis-and-services-questions/getting-token-generatetorken-in-c-from-windows/m-p/1012505#M3663</link>
    <description>&lt;P&gt;Did you compare headers with what the browser is sending?&lt;/P&gt;</description>
    <pubDate>Mon, 28 Dec 2020 19:01:43 GMT</pubDate>
    <dc:creator>KirkKuykendall1</dc:creator>
    <dc:date>2020-12-28T19:01:43Z</dc:date>
    <item>
      <title>Getting Token (generateTorken) in C# from Windows Authenticated Enterprise</title>
      <link>https://community.esri.com/t5/arcgis-rest-apis-and-services-questions/getting-token-generatetorken-in-c-from-windows/m-p/1012188#M3661</link>
      <description>&lt;P&gt;I am trying to use the generateToken endpoint with a installation that uses Windows Authentication.&amp;nbsp; In Runtime or in Python API in this case you would send the username and password as empty strings.&amp;nbsp; However, doing so in a standalone C# program calling into the generateToken endpoint returns an error 401 unauthorized access.&lt;/P&gt;&lt;P&gt;I have tried to do this a couple ways one of which follows&lt;/P&gt;&lt;LI-CODE lang="csharp"&gt;HttpRequestMessage msg = new HttpRequestMessage(HttpMethod.Post, "https://PORTALSERVER/portal/sharing/rest/generateToken") { Content = TokenRequestContent() };
using HttpClient httpClient = new HttpClient();
httpClient.DefaultRequestHeaders.Referrer = new Uri("https://PORTALSERVER/portal");

var t = await httpClient.SendAsync(msg);
var s = await t.Content.ReadAsStringAsync();

private FormUrlEncodedContent TokenRequestContent()
{
	var parameters = new Dictionary&amp;lt;string, string&amp;gt;
	{
		{"username", ""},
		{"password", ""},
		{"client", "requestip" },
		{"expiration", "1440" },
		{"f", "pjson" },
	};

	var content = new FormUrlEncodedContent(parameters);
	return content;
}&lt;/LI-CODE&gt;&lt;P&gt;Which fails&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;&amp;lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"&amp;gt;
&amp;lt;html xmlns="http://www.w3.org/1999/xhtml"&amp;gt;
&amp;lt;head&amp;gt;
&amp;lt;meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"/&amp;gt;
&amp;lt;title&amp;gt;401 - Unauthorized: Access is denied due to invalid credentials.&amp;lt;/title&amp;gt;&lt;/LI-CODE&gt;&lt;P&gt;If I use the web browser to just make the call and send empty username and password I get the token as expected.&lt;/P&gt;&lt;P&gt;If I change and use the same code but to request from AGOL is works:&lt;/P&gt;&lt;LI-CODE lang="csharp"&gt;HttpRequestMessage msg = new HttpRequestMessage(HttpMethod.Post, "https://arcgis.com/sharing/rest/generateToken") { Content = TokenRequestContent() };
using HttpClient httpClient = new HttpClient();
httpClient.DefaultRequestHeaders.Referrer = new Uri("https://arcgis.com");

var t = await httpClient.SendAsync(msg);
var s = await t.Content.ReadAsStringAsync();

private FormUrlEncodedContent TokenRequestContent()
{
	var parameters = new Dictionary&amp;lt;string, string&amp;gt;
	{
		{"username", "AGOLUSER"},
		{"password", "AGOLPASSWORD"},
		{"client", "requestip" },
		{"expiration", "1440" },
		{"f", "pjson" },
	};

	var content = new FormUrlEncodedContent(parameters);
	return content;
}&lt;/LI-CODE&gt;&lt;P&gt;I will get a valid token returned in json as expected.&lt;/P&gt;&lt;P&gt;I have compared the Fiddler message for the request sent to the local portal both from my code and what is sent from the request done from the Web UI and both seem the same.&amp;nbsp;&lt;/P&gt;&lt;P&gt;Why would sending the same request with HttpClient not behave like the request if sent from a browser?&lt;/P&gt;</description>
      <pubDate>Mon, 28 Dec 2020 16:46:39 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-rest-apis-and-services-questions/getting-token-generatetorken-in-c-from-windows/m-p/1012188#M3661</guid>
      <dc:creator>JoeHershman</dc:creator>
      <dc:date>2020-12-28T16:46:39Z</dc:date>
    </item>
    <item>
      <title>Re: Getting Token (generateTorken) in C# from Windows Authenticated Enterprise</title>
      <link>https://community.esri.com/t5/arcgis-rest-apis-and-services-questions/getting-token-generatetorken-in-c-from-windows/m-p/1012504#M3662</link>
      <description>&lt;P&gt;Would seem you need to tell the HttpClient to use DefaultCredentials&lt;/P&gt;&lt;LI-CODE lang="csharp"&gt;using HttpClient httpClient = new HttpClient(new HttpClientHandler{UseDefaultCredentials = true});&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 28 Dec 2020 19:00:50 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-rest-apis-and-services-questions/getting-token-generatetorken-in-c-from-windows/m-p/1012504#M3662</guid>
      <dc:creator>JoeHershman</dc:creator>
      <dc:date>2020-12-28T19:00:50Z</dc:date>
    </item>
    <item>
      <title>Re: Getting Token (generateTorken) in C# from Windows Authenticated Enterprise</title>
      <link>https://community.esri.com/t5/arcgis-rest-apis-and-services-questions/getting-token-generatetorken-in-c-from-windows/m-p/1012505#M3663</link>
      <description>&lt;P&gt;Did you compare headers with what the browser is sending?&lt;/P&gt;</description>
      <pubDate>Mon, 28 Dec 2020 19:01:43 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-rest-apis-and-services-questions/getting-token-generatetorken-in-c-from-windows/m-p/1012505#M3663</guid>
      <dc:creator>KirkKuykendall1</dc:creator>
      <dc:date>2020-12-28T19:01:43Z</dc:date>
    </item>
  </channel>
</rss>

