Run Java Program with newly created Windows User

-1

I'm trying to create new Windows User and then use him to open a process from a .jar file. The problem is that the new user doesn't have permission to Java and it doesn't open the process, but if it succeed it's giving an error like this:

Could not open windows registry node Software\JavaSoft\Prefs\com\c at root 0x80000001

And here is my code:

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.DirectoryServices;
using System.Linq;
using System.Security;
using System.Text;
using System.Threading.Tasks;

namespace OpenExe
{
    class Program
    {
        private Process proc;
        private static string Username { get; set; }
        public static void createUser(string Name, string Pass)
        {
            try
            {
                DirectoryEntry AD = new DirectoryEntry("WinNT://" + Environment.MachineName + ",computer");
                DirectoryEntry NewUser = AD.Children.Add(Name, "user");
                NewUser.Invoke("SetPassword", new object[] { Pass });
                NewUser.Invoke("Put", new object[] { "Description", "Test User from .NET" });
                NewUser.CommitChanges();
                DirectoryEntry grp;

                grp = AD.Children.Find("Administrators", "group");
                if (grp != null) { grp.Invoke("Add", new object[] { NewUser.Path.ToString() }); }
                Console.WriteLine("Account Created Successfully");
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }

        public void exec(string filename, string username, string password)
        {
            using(proc = new Process())
            {
                SecureString ssPwd = new SecureString();
                proc.StartInfo.WorkingDirectory = @"D:\";
                proc.StartInfo.UseShellExecute = false;
                proc.StartInfo.FileName = "java";
                proc.StartInfo.Arguments = @"-jar D:\rawclient.jar";
                proc.StartInfo.UserName = username;
                for (int x = 0; x < password.Length; x++)
                {
                    ssPwd.AppendChar(password[x]);
                }
                proc.StartInfo.Password = ssPwd; 
                proc.Exited += new EventHandler(process_Exited);
                proc.Start();
            }
        }

        private void process_Exited (object sender, EventArgs e)
        {
            DirectoryEntry localDirectory = new DirectoryEntry("WinNT://" + Environment.MachineName.ToString());
            DirectoryEntries users = localDirectory.Children;
            DirectoryEntry user = users.Find(Username);
            users.Remove(user);
        }
        static void Main(string[] args)
        {
            string filename = @"D:/rawclient.jar";
            string username = Guid.NewGuid().ToString("n").Substring(0, 8);
            Username = username;
            string pass = Guid.NewGuid().ToString("n").Substring(0, 8);
            createUser(username, pass);
            Program prog = new Program();
            prog.exec(filename, username, pass);
        }
    }
}
java
c#
windows
asked on Stack Overflow Oct 10, 2019 by GhoSTBG

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0