Select to view content in your preferred language

Using C# to access REST API

4278
3
02-25-2020 11:21 AM
DavidOBrien
New Contributor

I'm tasked with automating adding and deleting portal user accounts on our arcgis server.

The basic workflow is like this

1. Someone requests access.

2. Someone approves the access request.

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 

I can do all this but am having a heck of a time getting the .net app to authenticate and stay authenticated...

test application code 

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<KeyValuePair<string, string>>();
 nvc.Add(new KeyValuePair<string, string>("username", "adminaccount"));
 nvc.Add(new KeyValuePair<string, string>("password", "pass.w0rd"));
 nvc.Add(new KeyValuePair<string, string>("ip", ""));
 nvc.Add(new KeyValuePair<string, string>("client", "requestip"));
 nvc.Add(new KeyValuePair<string, string>("expiration", "1440"));
 nvc.Add(new KeyValuePair<string, string>("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<Token>(json);
 Console.WriteLine( token.token );

 // Try adding a user
 var user = new List<KeyValuePair<string, string>>();
 user.Add(new KeyValuePair<string, string>("username", "Gizmo"));
 user.Add(new KeyValuePair<string, string>("password", "Test1234"));
 user.Add(new KeyValuePair<string, string>("firstname", "Test"));
 user.Add(new KeyValuePair<string, string>("lastname", "User"));
 user.Add(new KeyValuePair<string, string>("role", "org_user"));
 user.Add(new KeyValuePair<string, string>("userLicense", "creatorUT"));
 user.Add(new KeyValuePair<string, string>("email", "test_email@mydomainname.com"));
 user.Add(new KeyValuePair<string, string>("description", "Original Description"));
 user.Add(new KeyValuePair<string, string>("token", token.token));
 user.Add(new KeyValuePair<string, string>("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();
 }
 }
}

This code outputs the generated token and an error message from the adduser call in my script. 

FBZ_w8FKzdo4HtoDUMLpwYne _cvspl2y5I5Z1y13hMBHyV50qlDQcTtU5e8Y7N-9f9fUr786nlgesMwk1o3k_ m0zBPeCls6j_eGCew1yuMr-PZt_9Gmk0FNz8Ah7LAEVn pddXcnVZhyYHtpkXeVmf3wMRNjxzZiaOn 5dAZr3-yY.
{"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." ]}}

How can I do this??

Tags (2)
0 Kudos
3 Replies
JoeHershman
MVP Regular Contributor

Did you ever get this figured out?

Thanks,
-Joe
0 Kudos
PrashantKirpan
Frequent Contributor

var handler = new HttpClientHandler { UseCookies = false };
var client = new HttpClient(handler);
client.DefaultRequestHeaders.Add("Cookie", $"agstoken={token}");

0 Kudos
PrashantKirpan
Frequent Contributor

It works for me..

var handler = new HttpClientHandler { UseCookies = false };
var client = new HttpClient(handler);
client.DefaultRequestHeaders.Add("Cookie", $"agstoken={token}");

0 Kudos