I want to access share and cancel access when I need to.
I use the following code:
class Program
{
const string Share = "\\\\srv\\share";
static void ListFiles()
{
foreach (var dir in Directory.EnumerateDirectories(Share))
Console.WriteLine(dir);
}
static void Main(string[] args)
{
using (var connection = new NetworkConnection(Share, new System.Net.NetworkCredential("user", "password")))
ListFiles();
ListFiles();
}
}
First ListFiles() call succeeds. I expect second ListFiles() call to fail but it also succeeds. How to cancel connection properly? Seems that WNetCancelConnection2 doesnt work.
This is also stated here. I wonder if anybody can make it work.
FIY here's a Network connection class:
public class NetworkConnection : IDisposable
{
string _networkName;
public NetworkConnection(string networkName,
NetworkCredential credentials)
{
_networkName = networkName;
var netResource = new NetResource()
{
Scope = ResourceScope.GlobalNetwork,
ResourceType = ResourceType.Disk,
DisplayType = ResourceDisplaytype.Share,
RemoteName = networkName
};
var userName = string.IsNullOrEmpty(credentials.Domain)
? credentials.UserName
: string.Format(#"{0}\{1}", credentials.Domain, credentials.UserName);
var result = WNetAddConnection2(
netResource,
credentials.Password,
userName,
0);
if (result != 0)
{
throw new Win32Exception(result, "Error connecting to remote share");
}
}
~NetworkConnection()
{
Dispose(false);
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
WNetCancelConnection2(_networkName, 0, true);
}
[DllImport("mpr.dll")]
private static extern int WNetAddConnection2(NetResource netResource,
string password, string username, int flags);
[DllImport("mpr.dll")]
private static extern int WNetCancelConnection2(string name, int flags,
bool force);
}
[StructLayout(LayoutKind.Sequential)]
public class NetResource
{
public ResourceScope Scope;
public ResourceType ResourceType;
public ResourceDisplaytype DisplayType;
public int Usage;
public string LocalName;
public string RemoteName;
public string Comment;
public string Provider;
}
public enum ResourceScope : int
{
Connected = 1,
GlobalNetwork,
Remembered,
Recent,
Context
};
public enum ResourceType : int
{
Any = 0,
Disk = 1,
Print = 2,
Reserved = 8,
}
public enum ResourceDisplaytype : int
{
Generic = 0x0,
Domain = 0x01,
Server = 0x02,
Share = 0x03,
File = 0x04,
Group = 0x05,
Network = 0x06,
Root = 0x07,
Shareadmin = 0x08,
Directory = 0x09,
Tree = 0x0a,
Ndscontainer = 0x0b
}
I met similar issues. And I waited 60 seconds in the code and then the network connection is really closed. Paulik is right, the credential is cached somewhere which made my next connection with a different credential fail. However, if I close with WNetCancelConnection2(pathname, flag, false) and wait for 60 seconds, my new credential will work okay. Don't know the reason but it just works.
I faced the same issues, and unfortunately, didn't manage to find appropriate solution. I can only describe my research result.
This method uses 'net use /delete' command under the hood. It works fine and deletes network connection. You can check that by calling 'net use' in command window. The connection is closed, but, credentials are still cached somewhere.
So the main reason of this issue is cached credentials. You can try to find them in Credential Manager calling 'control keymgr.dll' in Run window, but in my case they were not displayed. Credentials might be cached by explorer.exe, so you can try to open Task Manager and restart it.
The only reliable way to get rid of them is to restart Workstation Service
using 'net stop' 'net start' commands.
Related
I have WCF service that is hosted on windows service. I installed this service using Windows installer. Sometimes, when i stop service using C# code, it stucks on stopping. So i thought, why not kill service if service is not stopping within 2 minutes. My code is below to stop service:
var service = ServiceController.GetServices()
.FirstOrDefault(s => s.ServiceName == serviceName);
try
{
if (service == null || service.Status != ServiceControllerStatus.Running) return;
if(service.CanStop)
{
session.LogInfo($"Stopping '{serviceName}'.");
TimeSpan timeout = TimeSpan.FromMilliseconds(ServiceStopTime);
service.Stop();
service.WaitForStatus(ServiceControllerStatus.Stopped, timeout);
session.LogInfo($"'{serviceName}' stopped successfully.");
}
It is working as expected. I want to kill my process if service does not stop. Here is my code to kill process.
var processName = GetProcessNameByWindowsService(serviceName);
if (processName == null) return;
Process[] procs = Process.GetProcessesByName(processName);
if (procs.Length > 0)
{
foreach (Process proc in procs)
{
session.LogInfo($"Killing Process'{processName}'.");
proc.Kill();
session.LogInfo($"'{processName}' killed successfully.");
}
}
It is working as expected too but the problem is when i kill the process, the service does not stop. It assigns new process to service and service keep runs. After googled and investing some time i found the cause that is the window service recovery option which is restart the service if it fails. I want to change/set the recovery option for service in case of first failure, second failure and subsequent failure to take no action using C# code. I googled but did not find anything. So i want to know how i can change the recovery option of installed window service using C#?
After investing time finally i have found the solution with the help of this link. I have written two helper classes to set/update recovery option of windows service. First of all i wrote a static helper class which is below:
using System;
using System.Runtime.InteropServices;
namespace HRTC.CustomActions.Helpers
{
public static class ServiceRecoveryOptionHelper
{
//Action Enum
public enum RecoverAction
{
None = 0, Restart = 1, Reboot = 2, RunCommand = 3
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
public struct ServiceFailureActions
{
public int dwResetPeriod;
[MarshalAs(UnmanagedType.LPWStr)]
public string lpRebootMsg;
[MarshalAs(UnmanagedType.LPWStr)]
public string lpCommand;
public int cActions;
public IntPtr lpsaActions;
}
[StructLayout(LayoutKind.Sequential)]
public class ScAction
{
public int type;
public uint dwDelay;
}
// Win32 function to open the service control manager
[DllImport("advapi32.dll")]
public static extern IntPtr OpenSCManager(string lpMachineName, string lpDatabaseName, int dwDesiredAccess);
// Win32 function to open a service instance
[DllImport("advapi32.dll")]
public static extern IntPtr OpenService(IntPtr hScManager, string lpServiceName, int dwDesiredAccess);
// Win32 function to change the service config for the failure actions.
[DllImport("advapi32.dll", EntryPoint = "ChangeServiceConfig2")]
public static extern bool ChangeServiceFailureActions(IntPtr hService, int dwInfoLevel,
[MarshalAs(UnmanagedType.Struct)]
ref ServiceFailureActions lpInfo);
[DllImport("advapi32.dll", CharSet = CharSet.Unicode, SetLastError = true, EntryPoint = "QueryServiceConfig2W")]
public static extern Boolean QueryServiceConfig2(IntPtr hService, UInt32 dwInfoLevel, IntPtr buffer, UInt32 cbBufSize, out UInt32 pcbBytesNeeded);
[DllImport("kernel32.dll")]
public static extern int GetLastError();
}
public class FailureAction
{
// Default constructor
public FailureAction() { }
// Constructor
public FailureAction(ServiceRecoveryOptionHelper.RecoverAction actionType, int actionDelay)
{
Type = actionType;
Delay = actionDelay;
}
// Property to set recover action type
public ServiceRecoveryOptionHelper.RecoverAction Type { get; set; } = ServiceRecoveryOptionHelper.RecoverAction.None;
// Property to set recover action delay
public int Delay { get; set; }
}
}
Then i already have static class for windows services that have different methods like to start windows service, stop windows service and install service etc. I added new static method in this class to change recovery option of windows service which receive 4 parameters. First one is the service name, and other three are the recovery options of first,second and subsequent recovery options respectively. Below is it's implementation.
using System;
using System.Collections;
using System.Runtime.InteropServices;
namespace HRTC.CustomActions.Helpers
{
public class LocalServiceHelper
{
//Change service recovery option settings
private const int ServiceAllAccess = 0xF01FF;
private const int ScManagerAllAccess = 0xF003F;
private const int ServiceConfigFailureActions = 0x2;
private const int ErrorAccessDenied = 5;
public static void ChangeRevoveryOption(string serviceName, ServiceRecoveryOptionHelper.RecoverAction firstFailureAction,
ServiceRecoveryOptionHelper.RecoverAction secondFailureAction, ServiceRecoveryOptionHelper.RecoverAction thirdFailureAction)
{
try
{
// Open the service control manager
var scmHndl = ServiceRecoveryOptionHelper.OpenSCManager(null, null, ScManagerAllAccess);
if (scmHndl.ToInt32() <= 0)
return;
// Open the service
var svcHndl = ServiceRecoveryOptionHelper.OpenService(scmHndl, serviceName, ServiceAllAccess);
if (svcHndl.ToInt32() <= 0)
return;
var failureActions = new ArrayList
{
// First Failure Actions and Delay (msec)
new FailureAction(firstFailureAction, 0),
// Second Failure Actions and Delay (msec)
new FailureAction(secondFailureAction, 0),
// Subsequent Failures Actions and Delay (msec)
new FailureAction(thirdFailureAction, 0)
};
var numActions = failureActions.Count;
var myActions = new int[numActions * 2];
var currInd = 0;
foreach (FailureAction fa in failureActions)
{
myActions[currInd] = (int) fa.Type;
myActions[++currInd] = fa.Delay;
currInd++;
}
// Need to pack 8 bytes per struct
var tmpBuf = Marshal.AllocHGlobal(numActions * 8);
// Move array into marshallable pointer
Marshal.Copy(myActions, 0, tmpBuf, numActions * 2);
// Set the SERVICE_FAILURE_ACTIONS struct
var config =
new ServiceRecoveryOptionHelper.ServiceFailureActions
{
cActions = 3,
dwResetPeriod = 0,
lpCommand = null,
lpRebootMsg = null,
lpsaActions = new IntPtr(tmpBuf.ToInt32())
};
// Call the ChangeServiceFailureActions() abstraction of ChangeServiceConfig2()
var result =
ServiceRecoveryOptionHelper.ChangeServiceFailureActions(svcHndl, ServiceConfigFailureActions,
ref config);
//Check the return
if (!result)
{
var err = ServiceRecoveryOptionHelper.GetLastError();
if (err == ErrorAccessDenied)
{
throw new Exception("Access Denied while setting Failure Actions");
}
// Free the memory
Marshal.FreeHGlobal(tmpBuf);
}
}
catch (Exception)
{
throw new Exception("Unable to set service recovery options");
}
}
}
}
That's it. You just only need to call the method to change recovery option of windows service. For example:
LocalServiceHelper.ChangeRevoveryOption("ServiceName",
ServiceRecoveryOptionHelper.RecoverAction.Restart,
ServiceRecoveryOptionHelper.RecoverAction.Restart,
ServiceRecoveryOptionHelper.RecoverAction.None);
It will update the recovery option of windows service as you will mention when calling the method. Hope this help. Happy codding! :)
I cannot find anything related to the error that I am encountering.
The problem is that I am encountering a CryptographicException on store.Add below:
"The request is not supported."
The MSDN Documentation is not very helpful, it states that:
The certificate could not be added to the store.
I used the following code as a guide, with the following change:
They are adding to StoreName.My and I want to add to
StoreName.Root
I want to use an IDisposable class to impersonate the user.
My proof of concept code (abridged/shoddy):
public class UserLogonImpersonator : IDisposable
{
private WindowsImpersonationContext _impersonationContext = null;
private const int LOGON_INTERACTIVE = 2;
private const int PROVIDER_DEFAULT = 0;
[DllImport("advapi32.dll", SetLastError=true)]
private static extern int LogonUser(
string lpszUserName,
string lpszDomain,
string lpszPassword,
int dwLogonType,
int dwLogonProvider,
ref IntPtr phToken);
public UserLogonImpersonator()
{
_impersonationContext = null;
IntPtr userHandle = IntPtr.Zero;
const string domain = "domain";
const string user = "user";
const string hcpw = "password";
try
{
int logonResult = LogonUser(user, domain, hcpw,
LOGON_INTERACTIVE, PROVIDER_DEFAULT, ref userHandle);
bool isLoggedOn = logonResult != 0;
if (isLoggedOn)
{
_impersonationContext =
WindowsIdentity.Impersonate(userHandle);
}
}
catch(Exception e)
{
// Handle Exception
}
}
public void Dispose()
{
// UndoImpersonation();
}
// Private methods ...
}
public class CertService
{
public void AddCertToRootStore()
{
using(new UserLogonImpersonator())
{
X509Certificate2 rootCert =
new X509Certificate2(certData.CertFilePath);
X509Store store =
new X509Store(StoreName.Root, StoreLocation.CurrentUser);
store.Open(OpenFlags.MaxAllowed);
store.Add(rootCert);
store.Close();
}
}
}
I can remove the impersonation and no exceptions are thrown, but that is not the correct user's store.
With the impersonation, I can put the cert into StoreName.AuthRoot without exception. This is not the store that I want the cert to go into.
Neither of these exception-free solutions will work. I require that the program be run with elevated privileges and go into another user's store.
I solved this by doing it manually.
I wanted to do this to automate "test chain certificates." Our third party CA gave us a set of certificates for a .local domain.
Our real-life use case would already have the root and chain certificate installed.
I need to access the following directory found from another domain into my c# program. How shall I proceed? Should I user Impersonate method?
string[] files = Directory.GetFiles(#"\\testweb\folder\test", "*.txt");
Please help.
Before doing that wou will need to programatically (if that's how you wanna solve it) create a connection to the domain with a proper user. You can do it using this class:
public class MprWrapper
{
[DllImport("Mpr.dll")]
private static extern int WNetUseConnection(
IntPtr hwndOwner,
_NETRESOURCE lpNetResource,
string lpPassword,
string lpUserID,
int dwFlags,
string lpAccessName,
string lpBufferSize,
string lpResult
);
struct _NETRESOURCE
{
public int dwScope;
public int dwType;
public int dwDisplayType;
public int dwUsage;
public string lpLocalName;
public string lpRemoteName;
public string lpComment;
public string lpProvider;
}
public static void WNetUseConnection(string remoteName, string user, string pass)
{
_NETRESOURCE myStruct = new _NETRESOURCE
{
dwType = 1, //it's a disk (0 is any, 2 is printer)
lpRemoteName = remoteName
};
int error = WNetUseConnection(new IntPtr(0), myStruct, pass, user, 0, null, null, null);
if (error != 0)
{
throw new Exception("That didn't work either");
}
// if we reach here then everything worked!!!
}
}
You connect with
MprWrapper.WNetUseConnection(#"\\DomainAddressHere", #"Domain\User", "Password1");
Then your getfiles method will work fine. This potentially leaves an open connection (but it doesn't create more than one) but anyway you may wanna create code to close it, handle everything correctly, etc. This is merely a starting point.
I am trying to Connect to a network resource using WNetAddConnection2 but its failing with error code ERROR_BAD_NET_NAME (Error Code 67).
But if use "net use" command with the same user name and password its succeeding though.
Any clues ?
public class NETRESOURCE
{
public int dwScope;
public int dwType;
public int dwDisplayType;
public int dwUsage;
public string LocalName;
public string RemoteName;
public string Comment;
public string Provider;
}
[DllImport("mpr.dll")]
public static extern int WNetAddConnection2(NETRESOURCE netResource, string password, string username, int flags);
public class ServerConnect
{
private string serverName;
private string userName;
private string password;
public int nResult;
public ServerConnect()
{
serverName = "";
userName = "";
password = "";
nResult = -1;
}
public void SetConnectionParam(string serName, string uName, string pwd)
{
serverName = serName;
userName = uName;
password = pwd;
}
public void Connect()
{
NETRESOURCE myResource = new NETRESOURCE();
myResource.dwScope = 0;
myResource.dwType = 0x00000001; //RESOURCETYPE_DISK
myResource.dwDisplayType = 0;
myResource.LocalName = "";
myResource.RemoteName = serverName;
myResource.dwUsage = 0;
myResource.Comment = "";
myResource.Provider = "";
nResult = WNetAddConnection2(myResource, password, userName, 0);
}
};
public void ConnectToDataServer(string serverName)
{
ServerConnect oConnect = new ServerConnect();
oConnect.SetConnectionParam(serverName, #"Domain\username", #"password");
Thread connectionThread = new Thread(new ThreadStart(oConnect.Connect));
connectionThread.Start();
while (!connectionThread.IsAlive) ;// Wait till thread starts and Alive
int nCount = 0;
while (connectionThread.IsAlive)
{
Thread.Sleep(500);
nCount++;
if (nCount == 10) // wait for 5 secs
{
//WriteLine(this, "Failed to Connect to to server " + serverName , LogStatus.Error);
connectionThread.Abort();
Thread.Sleep(1000);
}
}
//WriteLine(this, oConnect.nResult.ToString(), LogStatus.Success);
}
public void ConnectToServer()
{
ConnectToDataServer(#"\\ServerName");
}
For one, we would need to see your code, as WNetAddConnection2 is a Windows functions and thus P/Invoked, and P/Invoke operations are always very, very hairy.
On the note of assuming you've invoked correctly, there could be permissions in the way (this is especially true if you're running Windows 8). Try making sure to Run as Administrator when you luanch VS, as this will usually extend the same credentials to executing applications, whereas the console often has different permissions.
This question already has answers here:
Accessing a Shared File (UNC) From a Remote, Non-Trusted Domain With Credentials
(9 answers)
Closed 5 years ago.
I have a windows forms app running on a machine that is not on a domain, that needs to be able to move a file from the local filesystem to a UNC path. I have a username and password for that path. I was wondering is there any way to do this directly with out execing the net.exe command?
Ideally I wouldn't have to map a drive.
You can use WNetAddConnection to accomplish this. You will have to pInvoke. the code below worked for me after I set up the pInvoke declarations. The second block of code (below) contains the pInvoke declarations -- just stick it inside of a class.
public static void CopyFile(string from, string shareName, string username, string password)
{
NETRESOURCE nr = new NETRESOURCE();
nr.dwType = ResourceType.RESOURCETYPE_DISK;
nr.lpLocalName = null;
nr.lpRemoteName = shareName;
nr.lpProvider = null;
int result = WNetAddConnection2(nr, password, username, 0);
System.IO.File.Copy(from, System.IO.Path.Combine(shareName, System.IO.Path.GetFileName(from)));
}
You will need to paste the following supporting code into a class (taken from pInvoke.Net). Make sure to add a using statment to your code:
using System.Runtime.InteropServices
[DllImport("Mpr.dll", EntryPoint = "WNetAddConnection2", CallingConvention = CallingConvention.Winapi)]
private static extern int WNetAddConnection2(NETRESOURCE lpNetResource, string lpPassword,
string lpUsername, System.UInt32 dwFlags);
[StructLayout(LayoutKind.Sequential)]
private class NETRESOURCE
{
public ResourceScope dwScope = 0;
public ResourceType dwType = 0;
public ResourceDisplayType dwDisplayType = 0;
public ResourceUsage dwUsage = 0;
public string lpLocalName = null;
public string lpRemoteName = null;
public string lpComment = null;
public string lpProvider = null;
};
public enum ResourceScope
{
RESOURCE_CONNECTED = 1,
RESOURCE_GLOBALNET,
RESOURCE_REMEMBERED,
RESOURCE_RECENT,
RESOURCE_CONTEXT
};
public enum ResourceType
{
RESOURCETYPE_ANY,
RESOURCETYPE_DISK,
RESOURCETYPE_PRINT,
RESOURCETYPE_RESERVED
};
public enum ResourceUsage
{
RESOURCEUSAGE_CONNECTABLE = 0x00000001,
RESOURCEUSAGE_CONTAINER = 0x00000002,
RESOURCEUSAGE_NOLOCALDEVICE = 0x00000004,
RESOURCEUSAGE_SIBLING = 0x00000008,
RESOURCEUSAGE_ATTACHED = 0x00000010,
RESOURCEUSAGE_ALL = (RESOURCEUSAGE_CONNECTABLE | RESOURCEUSAGE_CONTAINER | RESOURCEUSAGE_ATTACHED),
};
public enum ResourceDisplayType
{
RESOURCEDISPLAYTYPE_GENERIC,
RESOURCEDISPLAYTYPE_DOMAIN,
RESOURCEDISPLAYTYPE_SERVER,
RESOURCEDISPLAYTYPE_SHARE,
RESOURCEDISPLAYTYPE_FILE,
RESOURCEDISPLAYTYPE_GROUP,
RESOURCEDISPLAYTYPE_NETWORK,
RESOURCEDISPLAYTYPE_ROOT,
RESOURCEDISPLAYTYPE_SHAREADMIN,
RESOURCEDISPLAYTYPE_DIRECTORY,
RESOURCEDISPLAYTYPE_TREE,
RESOURCEDISPLAYTYPE_NDSCONTAINER
};
The accepted answer on this question here seems like it would be worth looking into; it suggests using the Win32 API function WNetUseConnection.
From MSDN:
The WNetUseConnection function makes a
connection to a network resource. The
function can redirect a local device
to a network resource.
Which seems to accomplish what you're looking for, with no mention of net.exe. Does this help?