<?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: Using C# to access REST API in ArcGIS REST APIs and Services Questions</title>
    <link>https://community.esri.com/t5/arcgis-rest-apis-and-services-questions/using-c-to-access-rest-api/m-p/1012597#M3664</link>
    <description>&lt;P&gt;Did you ever get this figured out?&lt;/P&gt;</description>
    <pubDate>Tue, 29 Dec 2020 00:21:35 GMT</pubDate>
    <dc:creator>JoeHershman</dc:creator>
    <dc:date>2020-12-29T00:21:35Z</dc:date>
    <item>
      <title>Using C# to access REST API</title>
      <link>https://community.esri.com/t5/arcgis-rest-apis-and-services-questions/using-c-to-access-rest-api/m-p/201207#M980</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I'm tasked with automating adding and deleting portal user accounts on&amp;nbsp;our arcgis server.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;The basic workflow is like this&lt;/P&gt;&lt;P&gt;1. Someone requests access.&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;2. Someone approves the access request.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;3. several times a day autosys will be running a .net console app on a server (not a arcgis server) to query the access request database and provision any approved accounts&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;I can do all this but am having a heck of a time getting the .net app to authenticate and stay authenticated...&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;test application code&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;&lt;SPAN style="color: #000080;"&gt;using System;
using System.Net.Http;
using System.Threading.Tasks;
using Newtonsoft.Json;
using System.Collections.Generic;

namespace str_stp
{
 public class Token
 {
 public string ssl { get; set; }
 public string token { get; set; }
 public string expires { get; set; }
 }
 class Program
 {
 static void Main()
 {
 RunAsync().GetAwaiter().GetResult();
 }
 static async Task RunAsync()
 {
 try
 {
 // Create a new user 
 var nvc = new List&amp;lt;KeyValuePair&amp;lt;string, string&amp;gt;&amp;gt;();
 nvc.Add(new KeyValuePair&amp;lt;string, string&amp;gt;("username", "adminaccount"));
 nvc.Add(new KeyValuePair&amp;lt;string, string&amp;gt;("password", "pass.w0rd"));
 nvc.Add(new KeyValuePair&amp;lt;string, string&amp;gt;("ip", ""));
 nvc.Add(new KeyValuePair&amp;lt;string, string&amp;gt;("client", "requestip"));
 nvc.Add(new KeyValuePair&amp;lt;string, string&amp;gt;("expiration", "1440"));
 nvc.Add(new KeyValuePair&amp;lt;string, string&amp;gt;("f","pjson"));
 HttpRequestMessage msg = new HttpRequestMessage(HttpMethod.Post, "https://mygis-portal-sit.mydomainname.com/arcgis/sharing/rest/generateToken") { Content = new FormUrlEncodedContent(nvc) };
 var client1 = new HttpClient();
 client1.DefaultRequestHeaders.Referrer = new Uri("https://mygis-portal-sit.mydomainname.com/arcgis"); 
 var response = await client1.SendAsync(msg);
 string json = await response.Content.ReadAsStringAsync();
 Token token = JsonConvert.DeserializeObject&amp;lt;Token&amp;gt;(json);
 Console.WriteLine( token.token );

 // Try adding a user
 var user = new List&amp;lt;KeyValuePair&amp;lt;string, string&amp;gt;&amp;gt;();
 user.Add(new KeyValuePair&amp;lt;string, string&amp;gt;("username", "Gizmo"));
 user.Add(new KeyValuePair&amp;lt;string, string&amp;gt;("password", "Test1234"));
 user.Add(new KeyValuePair&amp;lt;string, string&amp;gt;("firstname", "Test"));
 user.Add(new KeyValuePair&amp;lt;string, string&amp;gt;("lastname", "User"));
 user.Add(new KeyValuePair&amp;lt;string, string&amp;gt;("role", "org_user"));
 user.Add(new KeyValuePair&amp;lt;string, string&amp;gt;("userLicense", "creatorUT"));
 user.Add(new KeyValuePair&amp;lt;string, string&amp;gt;("email", "test_email@mydomainname.com"));
 user.Add(new KeyValuePair&amp;lt;string, string&amp;gt;("description", "Original Description"));
 user.Add(new KeyValuePair&amp;lt;string, string&amp;gt;("token", token.token));
 user.Add(new KeyValuePair&amp;lt;string, string&amp;gt;("f", "pjson"));
 HttpRequestMessage useradd = new HttpRequestMessage(HttpMethod.Post, "https://mygis-portal-sit.mydomainname.com/arcgis/portaladmin/security/users/createUser") { Content = new FormUrlEncodedContent(user) };
 client1.DefaultRequestHeaders.Referrer = new Uri("https://mygis-portal-sit.mydomainname.com/arcgis");
 response = await client1.SendAsync(useradd);
 json = await response.Content.ReadAsStringAsync();
 Console.WriteLine(json);
 }
 catch (Exception e)
 {
 Console.WriteLine(e.Message);
 }
 Console.ReadLine();
 }
 }
}&lt;/SPAN&gt;&lt;/PRE&gt;&lt;P&gt;This code outputs the generated token and an error message from the adduser call in my script.&amp;nbsp;&lt;/P&gt;&lt;P style="border: 1px solid #F00;"&gt;FBZ_w8FKzdo4HtoDUMLpwYne _cvspl2y5I5Z1y13hMBHyV50qlDQcTtU5e8Y7N-9f9fUr786nlgesMwk1o3k_ m0zBPeCls6j_eGCew1yuMr-PZt_9Gmk0FNz8Ah7LAEVn pddXcnVZhyYHtpkXeVmf3wMRNjxzZiaOn 5dAZr3-yY.&lt;BR /&gt; &lt;SPAN style="color: #ff0000;"&gt;{"error" : {"code" : 498,"message" : "Invalid Token.","details" : [ "Token would have expired, regenerate token and send the request again.", "If the token is generated based on the referrer make sure the referrer information is available with every request in header." ]}}&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;How can I do this??&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 10:00:53 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-rest-apis-and-services-questions/using-c-to-access-rest-api/m-p/201207#M980</guid>
      <dc:creator>DavidOBrien</dc:creator>
      <dc:date>2021-12-11T10:00:53Z</dc:date>
    </item>
    <item>
      <title>Re: Using C# to access REST API</title>
      <link>https://community.esri.com/t5/arcgis-rest-apis-and-services-questions/using-c-to-access-rest-api/m-p/1012597#M3664</link>
      <description>&lt;P&gt;Did you ever get this figured out?&lt;/P&gt;</description>
      <pubDate>Tue, 29 Dec 2020 00:21:35 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-rest-apis-and-services-questions/using-c-to-access-rest-api/m-p/1012597#M3664</guid>
      <dc:creator>JoeHershman</dc:creator>
      <dc:date>2020-12-29T00:21:35Z</dc:date>
    </item>
    <item>
      <title>Re: Using C# to access REST API</title>
      <link>https://community.esri.com/t5/arcgis-rest-apis-and-services-questions/using-c-to-access-rest-api/m-p/1035279#M3751</link>
      <description>&lt;P&gt;It works for me..&lt;/P&gt;&lt;P&gt;var handler = new HttpClientHandler { UseCookies = false };&lt;BR /&gt;var client = new HttpClient(handler);&lt;BR /&gt;client.DefaultRequestHeaders.Add("Cookie", $"agstoken={token}");&lt;/P&gt;</description>
      <pubDate>Thu, 11 Mar 2021 02:30:14 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-rest-apis-and-services-questions/using-c-to-access-rest-api/m-p/1035279#M3751</guid>
      <dc:creator>PrashantKirpan</dc:creator>
      <dc:date>2021-03-11T02:30:14Z</dc:date>
    </item>
    <item>
      <title>Re: Using C# to access REST API</title>
      <link>https://community.esri.com/t5/arcgis-rest-apis-and-services-questions/using-c-to-access-rest-api/m-p/1035281#M3752</link>
      <description>&lt;P&gt;var handler = new HttpClientHandler { UseCookies = false };&lt;BR /&gt;var client = new HttpClient(handler);&lt;BR /&gt;client.DefaultRequestHeaders.Add("Cookie", $"agstoken={token}");&lt;/P&gt;</description>
      <pubDate>Thu, 11 Mar 2021 02:31:02 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-rest-apis-and-services-questions/using-c-to-access-rest-api/m-p/1035281#M3752</guid>
      <dc:creator>PrashantKirpan</dc:creator>
      <dc:date>2021-03-11T02:31:02Z</dc:date>
    </item>
  </channel>
</rss>

