Do not change the login account, switch identity access sharing

Usage scenarios

  • When doing operation and maintenance, it is necessary to visit the operation and maintenance department for sharing under the user desktop environment, and it is difficult to switch users
  • It is convenient to test the sharing permission settings of users back and forth

Remember to switch back to the original user identity after use, so as to avoid the user using your permission to share!

Remember to switch back to the original user identity after use, so as to avoid the user using your permission to share!

Basic principle

1. Use net use to disconnect existing users

net use * /del /y

2. Use the new account net use to share

REM Not necessarily used ipc$,You can use the existing path
net use \\share-server-ip\ipc$

3. Call explorer.exe to open the share

explorer \\share-server-ip

Tool download

The following is the source code and schematic diagram of C version tool, which can be customized and modified

https://download.csdn.net/download/leoforbest/10308818

autoit script

#include<GUIConstantsEx.au3>

$server = "\\192.168.241.135"
$NETBIOS_NAME = "TEST\"

MainGUI()

Func MainGUI()
   Local $button,$msg
   GUICreate("Share switching tool",300,200)

   $button = GUICtrlCreateButton("Next step",100,100,100)
   $label = GUICtrlCreateLabel("Please make sure that this program is copied to the local computer and then run!",50,30)
   $label2 = GUICtrlCreateLabel("Please close all open" & $server &"Window!",50,50)
   $button1 = GUICtrlCreateButton("Next step",100,150,100)
   $nameL = GUICtrlCreateLabel("accounts:",50,70)
   $passL = GUICtrlCreateLabel("Password:",50,90)
   $name = GUICtrlCreateInput("",90,70,120)
   $pass = GUICtrlCreateInput("",90,90,120)
   GUICtrlSetState($button1,$GUI_HIDE)
   GUICtrlSetState($nameL,$GUI_HIDE)
   GUICtrlSetState($passL,$GUI_HIDE)
   GUICtrlSetState($name,$GUI_HIDE)
   GUICtrlSetState($pass,$GUI_HIDE)

   Dim  $keys[1][2] = [["{Enter}",$button]]
   Dim  $keys2[1][2] = [["{Enter}",$button1]]
   GUISetAccelerators($keys)


   GUISetState()

   While 1
      $msg = GUIGetMsg()
      Select
      Case $msg = $GUI_EVENT_CLOSE
         ExitLoop
      Case $msg = $button
         GUICtrlSetData($label,"Disconnecting from" & $server & "Connection!")
         GUICtrlDelete($button)
         GUICtrlDelete($label2)
         ProcessWaitClose(RunWait("net use * /del /y","",@SW_HIDE),30)
         GUICtrlSetData($label,"   Please input user name and password correctly!")
         GUICtrlSetState($button1,$GUI_SHOW)
         GUICtrlSetState($nameL,$GUI_SHOW)
         GUICtrlSetState($passL,$GUI_SHOW)
         GUICtrlSetState($name,$GUI_SHOW)
         GUICtrlSetState($pass,$GUI_SHOW)
         GUISetAccelerators($keys2)

      Case $msg = $button1
         $nameget = GUICtrlRead($name)
         $passget = GUICtrlRead($pass)
         GUICtrlDelete($label)
         GUICtrlDelete($button1)
         GUICtrlDelete($nameL)
         GUICtrlDelete($passL)
         GUICtrlDelete($name)
         GUICtrlDelete($pass)
         GUICtrlCreateLabel("Building and" & $server & "New connection for!",50,90)
         ProcessWaitClose(Run("net use " & $server & "\ipc$ " & $passget & " /user:" & $NETBIOS_NAME & $nameget,"",@SW_HIDE),30)
         If @extended = 0 Then
            Run("explorer.exe " & $server)
            Exit(1)
         Else
            MsgBox(0,"Shared disk switching?","Incorrect account password, or" & $server & "Not completely closed, please try again!")
            Exit(0)
         EndIf
      EndSelect
   WEnd
EndFunc

c# client

Some key codes

public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            this.tbServer.Text = server;
        }

        private static string server = @"192.168.241.135";
        // Enter your domain NETBIOS name, non domain environment is empty
        private static string domain = @"";
        private delegate void setTextHandler(string text);
        private delegate void enableLoginHandler();
        private Thread th;
        private Process p;

        private void setText(string text)
        {
            if(this.lblLog.InvokeRequired)
            {
                setTextHandler handler = new setTextHandler(setText);
                this.lblLog.Invoke(handler, text);
            }
            else
            {
                this.lblLog.Text = text;
            }
        }

        private void enableLogin()
        {
            if (this.btnLogin.InvokeRequired)
            {
                enableLoginHandler handler = new enableLoginHandler(enableLogin);
                this.btnLogin.Invoke(handler);
            }
            else
            {
                this.btnLogin.Enabled = true;
            }
        }

        private void doMain()
        {
            p = new Process();
            p.StartInfo.FileName = "cmd";
            p.StartInfo.UseShellExecute = false;
            p.StartInfo.RedirectStandardOutput = true;
            p.StartInfo.RedirectStandardError = true;
            p.StartInfo.RedirectStandardInput = true;
            p.StartInfo.CreateNoWindow = true;
            p.Start();

            setText("Disconnecting old connection to server!");
            p.StandardInput.WriteLine("net use * /del /y");
            setText("Connecting to server with new account password!");

            p.StandardInput.WriteLine(String.Format(@"net use \\{0}\ipc$ {1} /user:{2}{3}", server, tbPwd.Text, domain, tbAccount.Text));
            p.StandardInput.WriteLine("exit");
            string ret = p.StandardError.ReadToEnd();
            setText("Opening share!");
            if (ret.Contains("Multiple connections between a user and a server or shared resource using more than one user name are not allowed"))
            {
                setText("Failed to access share!");
                MessageBox.Show("Failed, please close all open shared directories or files!" + ret, "Access to share failed");
            }
            else if (ret == "")
            {
                setText("Access sharing succeeded!");
                Process.Start("explorer.exe", String.Format(@"\\{0}", server));
            }
            else
            {
                setText("Failed to access share!");
                MessageBox.Show(ret, "Access to share failed");
            }
            p.WaitForExit();
            enableLogin();
        }

        private void btnLogin_Click(object sender, EventArgs e)
        {
            lblLog.Text = "";
            if (tbAccount.Text == "" || tbPwd.Text == "")
            {
                lblLog.Text = "User name or password cannot be empty!";
                return;
            }

            btnLogin.Enabled = false;
            th = new Thread(doMain);
            th.Start();    
        }

        private void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {
            if(p != null)
                p.Close();
        }

        private void tbAccount_TextChanged(object sender, EventArgs e)
        {
            lblLog.Text = "";
        }

    }

Added by recycles on Tue, 31 Mar 2020 22:15:28 +0300