Select to view content in your preferred language

Get List of Users from AGOL using C# .Net

666
6
08-18-2023 10:20 AM
Labels (1)
ParminderDhaliwal
New Contributor II

I am trying to get a list of AGOL users in my Organization, by passing the Admin User Credentials in a C# program. I would like to know how I can pass the credential to the portalTask. I am using Maps SDK for .Net version 200.1. 

var userName = "adminUser";
var password = "adminPassword";
var portalURL = "https://myOrg.maps.arcgis.com";
string tokenURL = portalURL + "/sharing/rest/generateToken";

var credentialTask = AuthenticationManager.Current.GenerateCredentialAsync(new Uri(tokenURL), userName, password);
credentialTask.Wait();
TokenCredential cred = credentialTask.Result;

var poralTask = ArcGISPortal.CreateAsync(new Uri(portalURL));poralTask.Wait();
ArcGISPortal portal = poralTask.Result;


var userTask = portal.FindUsersAsync(new PortalQueryParameters("username=A*"));
var users = userTask.Result;

Tags (2)
0 Kudos
6 Replies
PreetiMaske
Esri Regular Contributor

> var userTask = portal.FindUsersAsync(new PortalQueryParameters("username=A*"));

 

Looks like you are not awaiting this call 🙂

0 Kudos
ParminderDhaliwal
New Contributor II

Thanks PreetiMaske for your response.

I added await to the call but I am still getting the "'You do not have permissions to access this resource or perform this operation."

0 Kudos
JoeHershman
MVP Regular Contributor

What version of ArcGIS Enterprise are you using? 

I think the user is how you are getting the credential, the call for the Users is fine.

That is a obsolete method for getting credentials, you should really be using the OAuth pattern.

Also as a general comment you are not using an async calls the way they are designed, should call await not have .Wait at the end of a call

Thanks,
-Joe
0 Kudos
ParminderDhaliwal
New Contributor II

Thanks for your response JoeHershman. 
I am using ArcGIS Online (maps.arcgis.com). I agree my code is not correct that is why it is not working. Can you please supply any code sample to create a credential object using OAuth and attach it to portalTask or userTask.

0 Kudos
JoeHershman
MVP Regular Contributor

@ParminderDhaliwal 

Esri has a pretty in depth sample on setting up OAuth Authentication in Mobile/AGOL located here: Access services with OAuth 2.0   This is probably the best place to start as it requires setup in AGOL in addition to the code. 

I do not think that you can use the methods that just take a user name and password to generate a credential from AGOL anymore (I may be incorrect, but that is my understanding).  We have switched to using OAuth in all our applications

Thanks,
-Joe
0 Kudos
ParminderDhaliwal
New Contributor II

I ended up implementing the following solution using the REST endpoints.

var userName = "adminUser";
var password = "adminPassword";
 
string tokenURL = portalURL + "/sharing/rest/generateToken";
 
var credentialTask = AuthenticationManager.Current.GenerateCredentialAsync(new Uri(tokenURL), userName, password);
credentialTask.Wait();
TokenCredential cred = credentialTask.Result;
 
var client = new HttpClient();
 
int start = 1;
int number = 10;
while (start != -1)
{
    string url = string.Format("{0}/sharing/rest/portals/self/users/search?start={1}&num={2}&f=json&token={3}", portalURL, start, number, cred.Token);
    var request = new HttpRequestMessage(HttpMethod.Post, url);
    var response = await client.SendAsync(request);
    response.EnsureSuccessStatusCode();
 
    var jsonResponse = response.Content.ReadAsStringAsync().Result;
    Root? root = JsonConvert.DeserializeObject<Root?>(jsonResponse);
    foreach (Result user in root.Results)
    {
        Console.WriteLine(user.FullName);
    }
    start = root.NextStart;
}

Here are my Root and Result classes to store the json results from the call.

public class Result
{
public string UserName { get; set; }
public object UDN { get; set; }
public string ID { get; set; }
public string FullName { get; set; }
public List<string> Categories { get; set; }
public string EmailStatus { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public object PreferredView { get; set; }
public object Description { get; set; }
public string Email { get; set; }
public string UserType { get; set; }
public object IDPUsername { get; set; }
public object FavGroupId { get; set; }
public object LastLogin { get; set; }
public bool MFAEnabled { get; set; }
public bool ValidateUserProfile { get; set; }
public int StorageUsage { get; set; }
public int StorageQuota { get; set; }
public string OrgId { get; set; }
public string Role { get; set; }
public string Level { get; set; }
public string UserLicenseTypeId { get; set; }
public bool Disabled { get; set; }
public List<object> Tags { get; set; }
public string Culture { get; set; }
public object CultureFormat { get; set; }
public object Region { get; set; }
public string Units { get; set; }
public object Thumbnail { get; set; }
public string Access { get; set; }
public object Created { get; set; }
public object Modified { get; set; }
public string Provider { get; set; }
public List<object> Groups { get; set; }
}

public class Root
{
public int Total { get; set; }
public int Start { get; set; }
public int Num { get; set; }
public int NextStart { get; set; }
public List<Result> Results { get; set; }
}

 



0 Kudos