/// <summary> /// Submits a request for administrator rights to the specified host. /// </summary> /// <param name="hostName"> /// The name of the host to which the request is submitted. /// </param> private void RequestAdminRights(string hostName) { // The address of the remote computer's service host. string remoteHostAddress = string.Format("net.tcp://{0}/MakeMeAdmin/Service", hostName); // Open a connection to the remote host. NetTcpBinding binding = new NetTcpBinding(SecurityMode.Transport); ChannelFactory <IAdminGroup> namedPipeFactory = new ChannelFactory <IAdminGroup>(binding, remoteHostAddress); IAdminGroup channel = namedPipeFactory.CreateChannel(); // Submit a request for administrator rights on the remote host. try { channel.AddUserToAdministratorsGroup(); } catch (System.ServiceModel.EndpointNotFoundException) { throw; } catch (Exception) { throw; // TODO: What do we do with this error? } }
/// <summary> /// This function runs when RunWorkerAsync() is called by the "grant admin rights" BackgroundWorker object. /// </summary> /// <param name="sender"> /// The BackgroundWorker that triggered the event. /// </param> /// <param name="e"> /// Data specific to this event. /// </param> private void addUserBackgroundWorker_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e) { NetNamedPipeBinding binding = new NetNamedPipeBinding(NetNamedPipeSecurityMode.Transport); ChannelFactory <IAdminGroup> namedPipeFactory = new ChannelFactory <IAdminGroup>(binding, Settings.NamedPipeServiceBaseAddress); IAdminGroup channel = namedPipeFactory.CreateChannel(); try { channel.AddUserToAdministratorsGroup(); } catch (System.ServiceModel.EndpointNotFoundException) { throw; } catch (Exception) { throw; // TODO: What do we do with this error? } namedPipeFactory.Close(); }