0

Check this illustration please http://www.fa6er.com/Image1.png

Where to put username and password that the desktop application needs to access files in shared directory on a server within local network by IP?

string var_main_dir = @"\\192.168.1.244\c$\repository";

After this line the application make a connection to the server -BUT- stop and shows debug error message regarding

and invalid username and password

An unhandled exception of type 'System.IO.IOException' occurred in mscorlib.dll

Additional information: username and password

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
CrownFord
  • 709
  • 5
  • 15

1 Answers1

1

Before connecting to the server you will need to impersonate the login using the username and password you have. The following stack overflow should help a bit with writing that code: How do you do Impersonation in .NET?.

A shortened version is:

    public Impersonation(string domain, string username, string password)
    {
        var ok = LogonUser(username, domain, password,
                       LOGON32_LOGON_NEW_CREDENTIALS, 0, out this._handle);
        if (!ok)
        {
            var errorCode = Marshal.GetLastWin32Error();
            throw new ApplicationException(string.Format("Could not impersonate the elevated user.  LogonUser returned error code {0}.", errorCode));
        }

        this._context = WindowsIdentity.Impersonate(this._handle.DangerousGetHandle());
    }

Good luck!

Community
  • 1
  • 1
drew_w
  • 10,320
  • 4
  • 28
  • 49
  • There is also [that tiny little impersonator helper class](http://www.codeproject.com/Articles/10090/A-small-C-Class-for-impersonating-a-User) I've once wrote. – Uwe Keim Dec 11 '13 at 19:32
  • @Uwe Kiem, How to use your class.Do have I create new class? and paste it there? – CrownFord Dec 12 '13 at 08:36