I have also discovered from @rocketsarefast's answer that Windows will clear the old network credentials when there is a new login attempt.
However, his net use "\\10.0.0.5\c$" "badpassword" /user:"baduser" command is way too slow, especially when the client has to wait up to several seconds for the server to respond with a rejection, which is terrible and inconvenient.
For my use case, I was able to use the Win32 API to map the network share \\Server\Share to the S:\ drive. It disconnects the drive first and then prompts the user with the Windows network login dialog so that the person can logout and login as a different user.
Here is the C code main.c:
#ifndef UNICODE
#define UNICODE
#endif
#define WINVER 0x0500
#define _WIN32_WINNT 0x0500
#define _WIN32_IE 0x0500
#pragma comment(lib, L"mpr.lib")
#include <windows.h>
//#include <stdio.h>
int wmain(const size_t argc, const wchar_t** argv) {
NETRESOURCE networkResource = {0};
networkResource.dwType = RESOURCETYPE_DISK;
networkResource.lpLocalName = L"S:";
networkResource.lpRemoteName = L"\\\\Server\\Share";
networkResource.lpProvider = NULL;
DWORD result = 0;
result = WNetCancelConnection2(networkResource.lpLocalName, CONNECT_UPDATE_PROFILE, TRUE);
// wprintf(L"WNetCancelConnection2 result: %d\n", result);
result = WNetAddConnection2(&networkResource, NULL, NULL, CONNECT_INTERACTIVE | CONNECT_PROMPT);
// wprintf(L"WNetAddConnection2 result: %d\n", result);
// getchar();
return EXIT_SUCCESS;
}
Here is the CMakeLists.txt:
cmake_minimum_required(VERSION 3.7)
project(MapNetworkDrive)
set(CMAKE_C_STANDARD 11)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c11 -g3 -pedantic -Wall -Wextra -O0")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${GCC_COVERAGE_LINK_FLAGS} -municode -mwindows")
set(SOURCE_FILES main.c)
add_executable(MapNetworkDrive ${SOURCE_FILES})
target_link_libraries(MapNetworkDrive mpr.lib)
Compile using MinGW-w64 - for 32 and 64 bit Windows:
As an alternative to C, here is an easy C# tutorial on the API:
net useto see a list of connections, then pick a connection and add the /delete argument like Nate suggested. – Safado Sep 08 '11 at 14:10C:\> net use * /din ServerFault - Delete all mapped credentials and below. Adding this comment in case readers do not scroll down. – Abraham Mar 05 '15 at 16:10net use * /dwill remove the path fromnet usebut Win explorer might still remember the password. Log-off/log-on helps take care of Win explorer. – niCk cAMel Nov 14 '17 at 10:11net use .../delete(yes, you could also login/logoff after this command, but for me, restarting "workstationservice" was sufficient.). It's Servicename: LanmanWorkstation // path: C:\WINDOWS\System32\svchost.exe -k NetworkService -p //sc stop lanmanworkstationsc start lanmanworkstationg(with elevated privileges) – MacMartin Mar 16 '18 at 08:14net use /delete, without restarting LanmanWorkstation, it takes a while untildir \\the\deletedshareceases to show the share content. A binary search produced this time is 13 seconds for me. It seems this "waiting period" is extended when the share is accessed again during the time. – Moritz Both Jul 30 '18 at 08:56