Search code examples
c#windows-7uacgac

Register dll to gac as another user


I have an application that requeres to register a dll into a gac on a client computer everytime a new dll is deployed the problem is that the client computers only have restricted users with UAC turned on.

The application knows the credentials of a user that has admin rights.

The problem is if I have processStartInfo.UseShellExecute = false; then I can't get trough the UAC getting the error "The requested opperation requires elevation" and if I have processStartInfo.UseShellExecute = true; then it does not allow to run the whole thing as a different user: "The process object must have UseShellExecute property set to false in order to start the process as a user"

        internal static void Register(String assemblyName)
    {
        ProcessStartInfo processStartInfo = new ProcessStartInfo(lcDir + "gacutil.exe", string.Format("/i {0}.dll", assemblyName));
        processStartInfo.UseShellExecute = false;
        processStartInfo.WorkingDirectory = lcDir;
        processStartInfo.UserName = lcUser;
        processStartInfo.Password = Password;
        processStartInfo.Domain = lcDomain;

        processStartInfo.Verb = "runas";

        Process process = Process.Start(processStartInfo);
        process.WaitForExit();
    }

I would like to know what the "Best practice" for that kind of thing is? I know the whoe concept looks to windows like a virus.

What I would like to achive the following:

  • User dosen't need to know an account with admin rights
  • The credencal's of the admin user are saved in the program's database
  • As much as possable Automated registration.

Solution

  • You should run this part of your code as Administrator. What you need is impersonation.

    See this article

    Aricle Impersonation on Code project