0

Hi I want to create a console application to create new users account in my ORG Google Active Directory, using C#, can be with .Net or .Net Core, I did'nt find any info with example. Can you help me guys.

I've been tried search for info, but with no luck

  • Please add some precision to your question. There is no "Google Active Directory" product. You may (!?) need to use The [Directory API](https://developers.google.com/admin-sdk/directory) which is part of Google Admin SDK. It's confusing because it represents the combination of two seemingly disparate technologies (Google Workspace and Google Cloud). See this [Add User to Group and Org Unit using Google Directory API](https://stackoverflow.com/questions/32362286/adding-user-to-organization-unit-and-group-using-google-directory-api) (in C#) too. – DazWilkin Nov 23 '22 at 02:54
  • Thank you for your help @DazWilkin, yesterday I solved my issue, as you say I used Google Admin SDK, with a Service Account and now it works. – Alex Hurtado Campuzano Nov 24 '22 at 13:45

1 Answers1

0

After some search I found some documentations and I could solve my issue. Here is the code:

using System;
using System.IO;
using System.Threading.Tasks;
using Google.Apis.Admin.Directory.directory_v1.Data;
using Google.Apis.Admin.Directory.directory_v1;
using Google.Apis.Auth.OAuth2;
using Google.Apis.Services;
using ConsoleApp.Model;

namespace ConsoleApp
{
    class Program
    {
        static void Main()
        {

            try
            {
                new Program().Run().Wait();
            }
            catch (AggregateException ex)
            {
                foreach (var e in ex.InnerExceptions)
                {
                    Console.WriteLine("ERROR: " + e.Message);
                }
            }
        }
        DirectoryService Gservice;
        private async Task Run()
        {
            var gSettings = JObject.Parse(File.ReadAllText(AppContext.BaseDirectory + "googleServiceAccountSettings.json"));
            var credential = new ServiceAccountCredential(
                         new ServiceAccountCredential.Initializer(gSettings["client_email"].ToString())
                         {
                             ProjectId = gSettings["project_id"].ToString(),
                             User = gaduser,
                             Scopes = new[] { DirectoryService.Scope.AdminDirectoryUser }
                         }.FromPrivateKey(gSettings["private_key"].ToString()));

            Gservice = new DirectoryService(new BaseClientService.Initializer
            {
                HttpClientInitializer = credential,
                ApplicationName = "TESTNAME API"
            });
            var result = await InsertUserIntoGoogle(user);
            
            result = await ExistsUserInGoogle(user.Email);

        }

        public static async Task<bool> InsertUserIntoGoogle(ADUser user)
        {
            Console.WriteLine("Creating new user in Google: {0}", user);
            var usr = new User()
            {
                PrimaryEmail = user.Email,
                Name = new UserName()
                {
                    GivenName = user.FirstName,
                    FamilyName = user.LastName,
                    FullName = user.FirstName + " " + user.LastName,
                    DisplayName = user.FirstName + " " + user.LastName
                },
                Password = StandardPassword,
                OrgUnitPath = GOU
            };
            var request = Gservice.Users.Insert(usr);
            _ = await request.ExecuteAsync();

            Console.WriteLine("User created succesfully in Google: {0}", user);
            return true;
        }
        public async Task<bool> ExistsUserInGoogle(string userEmail)
        {
            try
            {
                var request = Gservice.Users.Get(userEmail);
                var result = await request.ExecuteAsync();
                return true;

            }
            catch (Exception e)
            {
                if (e.Message.Contains("HttpStatusCode is NotFound"))
                    return false;
                else
                    throw;
            }
        }
    }
}
  • PS: I had to install [Google ADMIN APi Package](https://www.nuget.org/packages/Google.Apis.Admin.Directory.directory_v1) – Alex Hurtado Campuzano Nov 24 '22 at 14:07
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Nov 28 '22 at 00:17