Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / CLR / December 2005

Tip: Looking for answers? Try searching our database.

Impersonation question for network resources

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Ben Fidge - 13 Dec 2005 11:58 GMT
Hi

I have a small WinForms app that needs to copy files from a shared drive on
a network. If I connect to the mapped drive using Explorer, a password dialog
pops-up and I have to provide credentials with permission to access this
resource on the machine where it's hosted. This is once for each desktop
logon session (ie after every reboot). If I do this, my app can access the
network resource fine, but otherwise fails with permission errors.

As my app runs when Windows starts, I don't want to first connect to the
mapped drive each and every time.

I've found some C# code that allows me to impersonate a
domain/account/password context, but it won't work in the scenario I want. I
know the code works because it allows me to impersonate local accounts, just
not access the network resource. When run, the following error occurs:

"Error No: 1326 - Error: Logon failure: unknown user name or bad password."

However, I know the domain/account/password are all correct.

The code is based on Win32 API's LogonUser. Am I barking up the wrong tree?

Thanks

Ben
Martin Kulov - 15 Dec 2005 22:43 GMT
Hi Ben,

Some code sample will be very usefull.

Best,
Martin

> Hi
>
[quoted text clipped - 28 lines]
>
> Ben
Ben Fidge - 16 Dec 2005 12:00 GMT
Hi Martin,

Here's the code I'm using. Please note that the Win32 API calls are wrapped
up in a static class called Impersonation:

IntPtr pWindowsIdentity     = IntPtr.Zero;
int iResult                 = Impersonation.LogonUser("MyAccount",
"MyDomain", "MyPassword", Impersonation.LOGON32_LOGON_INTERACTIVE,
Impersonation.LOGON32_PROVIDER_DEFAULT, ref pWindowsIdentity);
       
if (iResult == 0 && pWindowsIdentity != IntPtr.Zero) {
 WindowsIdentity oNewWI = new WindowsIdentity(pWindowsIdentity);
 Impersonation.CloseHandle(pWindowsIdentity);

 WindowsImpersonationContext oWIC = oNewWI.Impersonate();
         
 // .... Do code to access network resource here

     
 oWIC.Undo();                
}
else {
 int iError = Impersonation.GetLastError();
 throw new Exception(string.Format("Could not logon user using credentials
provided. Error No: {0} - Error: {1}", iError,
Impersonation.CreateLogonUserError(iError)));
}

Regards

Ben

> Hi Ben,
>
[quoted text clipped - 35 lines]
> >
> > Ben
Ben Fidge - 16 Dec 2005 13:37 GMT
Found it!!

First of all, I was assuming that LogonUser returned 0 (zero) on success,
wrongly. Secondly, I changed my code to use LOGON32_LOGON_NEW_CREDENTIALS
instead of LOGON32_LOGON_NETWORK, and it works a treat.

Ben

> Hi Martin,
>
[quoted text clipped - 67 lines]
> > >
> > > Ben

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.