473,386 Members | 1,738 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Impersonate not working from command line

Hello,

I am developing a simple move file utility to move files from one domain to
another in the same internal network. My program runs fine when it reads the
parameters (user, domain, password etc) from a XML file to move files across
the internal network to a different domain. This is a console application
that should have the option of specifying the parameters at the commandline
prompt. When I specify the parameters at cmd line, it gives me a "Access
denied" (access to the file at the destination) error message.

I debugged the code in both the above cases (1-reading params from Xml file
and 2-reading params from commandline). The values of username, password,
destination domain, source file and destination file were the same in both
the cases. But I am getting the Access denied error only when I do this from
cmd line.

Do you know how I can fix this? Any help in this regard will be appreciated.

Thanks,
-Divya

I am using the following Impersonator class to login as the admin of the
destination domain -

public class Impersonator
{
public WindowsImpersonationContext impersonationContext;

[DllImport("advapi32.dll")]
public static extern int LogonUser(String lpszUsername, String lpszDomain,
String lpszPassword,
int dwLogonType, int dwLogonProvider, ref IntPtr phToken);

[DllImport("kernel32.dll")]
public extern static bool CloseHandle(IntPtr hToken);

public bool Impersonate(string userName, string domain, string password)
{
WindowsIdentity tempWindowsIdentity;
IntPtr token = IntPtr.Zero;
IntPtr tokenDuplicate = IntPtr.Zero;
// request default security provider a logon token with
LOGON32_LOGON_NEW_CREDENTIALS,
// token returned is impersonation token, no need to duplicate
if(LogonUser(userName, domain, password, 9, 0, ref token) != 0)
{
tempWindowsIdentity = new WindowsIdentity(token);
impersonationContext = tempWindowsIdentity.Impersonate();
// close impersonation token, no longer needed
CloseHandle(token);
if (impersonationContext != null)
return true;
}
return false; // Failed to impersonate.
}
}
Nov 17 '05 #1
2 4604

"Divya" <Di***@discussions.microsoft.com> wrote in message
news:35**********************************@microsof t.com...
Hello,

I am developing a simple move file utility to move files from one domain
to
another in the same internal network. My program runs fine when it reads
the
parameters (user, domain, password etc) from a XML file to move files
across
the internal network to a different domain. This is a console application
that should have the option of specifying the parameters at the
commandline
prompt. When I specify the parameters at cmd line, it gives me a "Access
denied" (access to the file at the destination) error message.

I debugged the code in both the above cases (1-reading params from Xml
file
and 2-reading params from commandline). The values of username, password,
destination domain, source file and destination file were the same in both
the cases. But I am getting the Access denied error only when I do this
from
cmd line.

Do you know how I can fix this? Any help in this regard will be
appreciated.

Thanks,
-Divya

I am using the following Impersonator class to login as the admin of the
destination domain -

public class Impersonator
{
public WindowsImpersonationContext impersonationContext;

[DllImport("advapi32.dll")]
public static extern int LogonUser(String lpszUsername, String lpszDomain,
String lpszPassword,
int dwLogonType, int dwLogonProvider, ref IntPtr phToken);

[DllImport("kernel32.dll")]
public extern static bool CloseHandle(IntPtr hToken);

public bool Impersonate(string userName, string domain, string password)
{
WindowsIdentity tempWindowsIdentity;
IntPtr token = IntPtr.Zero;
IntPtr tokenDuplicate = IntPtr.Zero;
// request default security provider a logon token with
LOGON32_LOGON_NEW_CREDENTIALS,
// token returned is impersonation token, no need to duplicate
if(LogonUser(userName, domain, password, 9, 0, ref token) != 0)
{
tempWindowsIdentity = new WindowsIdentity(token);
impersonationContext = tempWindowsIdentity.Impersonate();
// close impersonation token, no longer needed
CloseHandle(token);
if (impersonationContext != null)
return true;
}
return false; // Failed to impersonate.
}
}


There must be something wrong when collecting/passing the string arguments,
can you post the code that collects the commandline args and passes them to
the Impersonate method?

Willy.
Nov 17 '05 #2
Thanks for the pointer, Willy. I did not know how to use breakpoints in code
while doing command line processing. Once I figured that out, I stepped
through the code and realised that there was one step that was missing while
I handled the commandline parameter. Once I fixed that, it is working fine!

Thanks for all the help. I really appreciate it.

-Divya
"Willy Denoyette [MVP]" wrote:

"Divya" <Di***@discussions.microsoft.com> wrote in message
news:35**********************************@microsof t.com...
Hello,

I am developing a simple move file utility to move files from one domain
to
another in the same internal network. My program runs fine when it reads
the
parameters (user, domain, password etc) from a XML file to move files
across
the internal network to a different domain. This is a console application
that should have the option of specifying the parameters at the
commandline
prompt. When I specify the parameters at cmd line, it gives me a "Access
denied" (access to the file at the destination) error message.

I debugged the code in both the above cases (1-reading params from Xml
file
and 2-reading params from commandline). The values of username, password,
destination domain, source file and destination file were the same in both
the cases. But I am getting the Access denied error only when I do this
from
cmd line.

Do you know how I can fix this? Any help in this regard will be
appreciated.

Thanks,
-Divya

I am using the following Impersonator class to login as the admin of the
destination domain -

public class Impersonator
{
public WindowsImpersonationContext impersonationContext;

[DllImport("advapi32.dll")]
public static extern int LogonUser(String lpszUsername, String lpszDomain,
String lpszPassword,
int dwLogonType, int dwLogonProvider, ref IntPtr phToken);

[DllImport("kernel32.dll")]
public extern static bool CloseHandle(IntPtr hToken);

public bool Impersonate(string userName, string domain, string password)
{
WindowsIdentity tempWindowsIdentity;
IntPtr token = IntPtr.Zero;
IntPtr tokenDuplicate = IntPtr.Zero;
// request default security provider a logon token with
LOGON32_LOGON_NEW_CREDENTIALS,
// token returned is impersonation token, no need to duplicate
if(LogonUser(userName, domain, password, 9, 0, ref token) != 0)
{
tempWindowsIdentity = new WindowsIdentity(token);
impersonationContext = tempWindowsIdentity.Impersonate();
// close impersonation token, no longer needed
CloseHandle(token);
if (impersonationContext != null)
return true;
}
return false; // Failed to impersonate.
}
}


There must be something wrong when collecting/passing the string arguments,
can you post the code that collects the commandline args and passes them to
the Impersonate method?

Willy.

Nov 17 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Yoshitha | last post by:
Hi I've to access registry and also get cpu id from the web application. First i wrote code to get cpuid from web application then i got cpu id. Next i wrote code to set value into a registry and...
1
by: moemeelaung | last post by:
Hi ASP/Windows experts out there I really need help with this Impersonate function. I have machine A and B. A hosts my ASP page which is to create a user account on the machine B. The...
1
by: Oz | last post by:
This is long. Bear with me, as I will really go through all the convoluted stuff that shows there is a problem with streams (at least when used to redirect stdout). The basic idea is that my...
12
by: Nils M. Lunde | last post by:
Hi! I've made a Web Service using C# that is using impersonation. The WS is working fine on WinXP and Win2003Server, but I'm having problem getting it to work on Win2000. The problem is that...
8
by: BLiTZWiNG | last post by:
After playing with the code shown and utilising Willy Denyottes' help, I have come to the conclusion that there is some form of difference between the managed WindowsIdentity.Impersonate() over the...
2
by: joeted | last post by:
Hello, I am trying to run a batch file on a (remote) .NET server (i'm not the admin) using system.diagnostics.process I find that the batch file is successfully executed. However, when i...
6
by: David C | last post by:
I was testing impersonation on a web site and added the line below to my web.config file. Now it fails with the message The current identity (DOMAIN01\LegalWeb) does not have write access to...
3
by: archana | last post by:
Hi all, In web apage i want to copy file from one directory to some shared path. I set impersonate to true. Its copying the file using system.io.file.copy. But system.io.directory.exist for...
5
by: WT | last post by:
Hello, IIS6 on W2K3, .net 3.5, Sql 2005. All sp applied. My site is using windows authentication only and the web application connects to sql server residing on another server in the same...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.