Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Trying to use System.DirectoryServices.AccountManagement in Node.js #188

Open
corey-Robinson1337 opened this issue Jan 3, 2024 · 2 comments

Comments

@corey-Robinson1337
Copy link

I have an electron app where I would like to grab the email of the current user automatically. To do this I'm using edge-js,

however, after I get everything set up I get this error --

PS C:\Users\crobinson\Desktop\git_repos\electron-edge-js-quick-start> npm start

> [email protected] start
> electron --core .
C:\Users\crobinson\Desktop\git_repos\electron-edge-js-quick-start\src\QuickStart.Core\bin\Debug\net8.0
username crobinson
Platform: Win32NT
Version: 10.0.22000.0
Service Pack:
Error: System.DirectoryServices.AccountManagement is not supported on this platform.

this is the relevant snippet of C# code

using System;
using System.Threading.Tasks;
using System.DirectoryServices.AccountManagement;
using System.Reflection.PortableExecutable;
namespace QuickStart.Core
{
    public class LocalMethods
    {
        public async Task<object> GetAppDomainDirectory(dynamic input)
        {
            return AppDomain.CurrentDomain.BaseDirectory;
        }

        public async Task<object> GetCurrentTime(dynamic input)
        {
            return DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
        }

        public async Task<object> UseDynamicInput(dynamic input)
        {
            return $".NET Core welcomes {input}";
        }

        public async Task<object> ThrowException(dynamic input)
        {
            throw new Exception("Sample Exception");
        }
        public async Task<object> GetCurrentUserEmail(dynamic input)
        {
            try
            {
                Console.WriteLine("username "+Environment.UserName);
                OperatingSystem os = Environment.OSVersion;

                Console.WriteLine($"Platform: {os.Platform}");
                Console.WriteLine($"Version: {os.Version}");
                Console.WriteLine($"Service Pack: {os.ServicePack}");
                using (PrincipalContext context = new PrincipalContext(ContextType.Domain))
                {
                    UserPrincipal user = UserPrincipal.FindByIdentity(context, Environment.UserName);

                    if (user != null)
                    {
                        return user.EmailAddress;
                    }
                }
            }
            catch (Exception ex)
            {
                return $"Error: {ex.Message}";
            }

            return null;
        }
    }
}

and this is how I'm calling it in my node js application

// main.js
const { app, BrowserWindow, Tray, screen, nativeImage } = require('electron');
//const electron = require('electron');
const path = require('path');
const os = require('os');
const configFile = path.join(__dirname, 'config.json');
const fs = require('fs');

let mainWindow;
let tray;

let namespace = "QuickStart.Core"
const baseNetAppPath = path.join(__dirname, '\\src\\QuickStart.Core\\bin\\Debug\\net8.0');

process.env.EDGE_USE_CORECLR = 1;
process.env.EDGE_APP_ROOT = baseNetAppPath;

var edge = require('electron-edge-js');

var baseDll = path.join(baseNetAppPath, namespace + '.dll');
var localTypeName = 'QuickStart.Core.LocalMethods';
var externalTypeName = 'QuickStart.Core.ExternalMethods';
var getAppDomainDirectory = edge.func({
  assemblyFile: baseDll,
  typeName: localTypeName,
  methodName: 'GetAppDomainDirectory'
});
getAppDomainDirectory('', function(error, result) {
  if (error) throw error;
  console.log(result);
});


var GetCurrentUserEmail = edge.func({
  assemblyFile: baseDll,
  typeName: localTypeName,
  methodName: 'GetCurrentUserEmail'
});
GetCurrentUserEmail('', function(error, result) {
  if (error) throw error;
  console.log(result);
});

This seems odd because I am on Windows and the C# code even confirms this is the case.

@agracio
Copy link
Owner

agracio commented Jan 13, 2024

Probably because underlying edge-js project compiled for netcoreapp1.1, not 100% sure.

@agracio
Copy link
Owner

agracio commented Mar 16, 2024

It could be related to #191 take a look at solution provided there.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants