Spratty
Spratty

Reputation: 125

C# Searchable Lists - Replace SQL Tables

I need to write an application that will perform some minor AD maintenance (basically taking a list of key/value pairs (AD username and a value taken from a SQL database) from another application in the form of a text file. The value in the text file is to be stored as a custom attribute per user in the AD. Unfortunately, the providing application does not produce a list of all users; only those users with a value in the field concerned, so any user who did have a value but now doesn't will not appear in the list - this means I can't use the list to directly remove the attribute value, only to add or modify it.

If I could use SQL Server I would just have a table of all users and a table of users with values (supplied by the other applications) and use a SET statement to blank the attribute in all users who do not appear in the provided list, then another SET to populate the value in all users who do appear in the provided list.

I'm new to C# and I don't know the best method to use to emulate that SET/SET functionality; I can read the AD to get all users, and I can read the file to get the users who now have a value to set, but I'd ideally like to read them into two "tables" and use some sort of comparison to set and unset the attribute as required. I can't think of a web search query to find what I need (my Google-fu is weak for subjects I'm new to) so any suggestions of the best way to proceed would be very gratefully received.

Example:

Original AD list:

AD Username    Madeup Attribute
------------------------------
abc1           value1
bcde           {no value}
fgh            value2

Provided list:

AD Username    Madeup Attribute
---------------------------------
bcde           value3
fgh            value4

Resulting AD values:

AD Username     Madeup Attribute
--------------------------------
abc1           {no value}
bcde           value3
fgh            value4

Apologies if my explanation is not brilliantly clear - another one of my "(many) weaknesses, it seems.

Upvotes: 0

Views: 108

Answers (2)

sa-es-ir
sa-es-ir

Reputation: 5082

You can read file into Dictionary<string, string> and update AD users like this:

        //Key is username and Value is attribute
        var userAttributes = new Dictionary<string, string>();
        
        //fill dictionary with file

        //get list of AD users
        var activeDirectoryUsers = new List<YourADUserModel>();

        //map the made up attribute from dictionary
        activeDirectoryUsers.ForEach(x =>
        {
            x.MadeUpAttribute= userAttributes.FirstOrDefault(x => x.Key == x.Username).Value;
        });

Upvotes: 1

Christopher Hamkins
Christopher Hamkins

Reputation: 1649

Using a Dictionary (see https://learn.microsoft.com/en-us/dotnet/api/system.collections.generic.dictionary-2?view=net-5.0) would be an easy way to do this.

In the first step, read in all the user ID's and add an entry with a null as value

using System.Collections.Generic;
...
Dictionary<string, string> dic = new  Dictionary<string, string>();

// For each user in the AD list:
dic.Add(username, null);  // Could als use empty string "" instead of null

// For each user in the provided list:
dic[username] = userattribute;

Then you can reference the attributes later and get the attribute for a user by using

string attr = dic[username];

Upvotes: 1

Related Questions