Re: Impersonation question for network resources



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


"Ben Fidge" wrote:

> 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
>
> "Martin Kulov" wrote:
>
> > Hi Ben,
> >
> > Some code sample will be very usefull.
> >
> > Best,
> > Martin
> >
> >
> > "Ben Fidge" <BenFidge@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
> > news:131F85CA-AE34-411C-BA3B-902470935B7F@xxxxxxxxxxxxxxxx
> > > 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
> >
> >
.



Relevant Pages

  • RE: COM dll thread security issue while accessing from ASP.NET
    ... I save a reference to the current WindowsIdentity ... IIS authenticated user account. ... impersonate the identity of the WindowsIdentity object saved above. ... the 'Impersonator' event handler is running inside the COM dll thread, ...
    (microsoft.public.dotnet.security)
  • Re: Impersonating when creating a process from inside a SQL Server Assembly
    ... Joe Kaplan-MS MVP Directory Services Programming ... Co-author of "The .NET Developer's Guide to Directory Services Programming" ... the current WindowsIdentity is still WINDOWS SERVICE. ... I'm trying to impersonate a different user when ...
    (microsoft.public.dotnet.security)
  • Re: Problem with Protocol Transition
    ... I'm just setting httpcontext.current.user to be a new WindowsIdentity object. ... Can't seem to see any params relating to Impersonate ... request is a HttpWorkerRequest object and ... Dim user as WindowsIdentity = New WindowsIdentity, ...
    (microsoft.public.dotnet.framework.aspnet.security)
  • Re: Problem with Protocol Transition
    ... what is the OS because windowsidentity react a little bit ... Network service or a service account. ... I've just noticed that the problem goes away if I impersonate the user. ... Joe Kaplan-MS MVP Directory Services Programming ...
    (microsoft.public.dotnet.framework.aspnet.security)
  • Re: Impersonation question for network resources
    ... Please note that the Win32 API calls are wrapped ... IntPtr pWindowsIdentity = IntPtr.Zero; ... >> network resource fine, but otherwise fails with permission errors. ... >> know the code works because it allows me to impersonate local accounts, ...
    (microsoft.public.dotnet.framework.clr)