Similar questions has been around a lot, but I couldn't find an answer that would help me with my specific problem. I am writing an application that needs to get files and do changes in a shared folder (local network), but I do not want to ask the user for his credentials.
If the user has already accessed the shared folder with his credentials once, would it be possible to use Windows ability to remember these credentials until restarting? Is it possible to "retrieve"/use these credentials or the "profile" through c# to access the shared location?
I have been developing my applciation to request the user for credentials and then impersonate, but I would really like to avoid that:
token = IntPtr.Zero;
var success = LogonUser(machine_username, remote_machine, machine_password, 9, 0, ref token);
using (WindowsImpersonationContext person = new WindowsIdentity(token).Impersonate())
{
try
{
// do something on the network location
}
catch (IOException)
{
Console.WriteLine("Connection with the server failed!");
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
finally
{
person.Undo();
CloseHandle(token);
}
}