0

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);
                }
            }
CameFromSpace
  • 113
  • 1
  • 4
  • 11
  • 2
    Are you programming in C#? Then why the C language tag? Please pay attention to the tags you select. – Some programmer dude May 18 '18 at 12:02
  • Possible duplicate of [Accessing Samba Share with authentication, No Drive Mapping - C#](https://stackoverflow.com/questions/8317217/accessing-samba-share-with-authentication-no-drive-mapping-c-sharp) – Lanting May 18 '18 at 12:10
  • Thank you for your answer. I do not think that https://stackoverflow.com/questions/659013/accessing-a-shared-file-unc-from-a-remote-non-trusted-domain-with-credentials that is used as an answer to the post you are referring is also the answer to my problem. Or if it is I am failing to identify the solution... – CameFromSpace May 18 '18 at 12:34

0 Answers0