User permission problem

S

schaf

Hi NG !
I try to solve a problem with the LogonUser() function, as you can see
in the topic:
http://groups.google.ch/group/micro...browse_thread/thread/cffc0f53f5f75153/?hl=de#

Now I have a special occurence of the function after the call.
I run the code under my username mmueller on my local machine which is
in the domain ABC. Within the code I do a call of the LogonUser with a
user admin and the password abc for a pc B which is not in the domain.
As domain name I enter the IP address (192.168.2.2) of the PC.
Unfortunately that does not work, but if I create a user mmueller on
the machine B (192.168.2.2), the call would end in a error 1326, but it
is possible to access the file system or the start/stop services.
But why ? Any idea ? Which requirements must be fulfilled, that the
call of LogonUser would succeed ?

Thanks
Regards
Marcel Hug
 
W

Willy Denoyette [MVP]

You not only have to call LogonUser, you also have to impersonate using the
token obtained by LogonUser. Please post your code.

Willy.

| Hi NG !
| I try to solve a problem with the LogonUser() function, as you can see
| in the topic:
|
http://groups.google.ch/group/micro...browse_thread/thread/cffc0f53f5f75153/?hl=de#
|
| Now I have a special occurence of the function after the call.
| I run the code under my username mmueller on my local machine which is
| in the domain ABC. Within the code I do a call of the LogonUser with a
| user admin and the password abc for a pc B which is not in the domain.
| As domain name I enter the IP address (192.168.2.2) of the PC.
| Unfortunately that does not work, but if I create a user mmueller on
| the machine B (192.168.2.2), the call would end in a error 1326, but it
| is possible to access the file system or the start/stop services.
| But why ? Any idea ? Which requirements must be fulfilled, that the
| call of LogonUser would succeed ?
|
| Thanks
| Regards
| Marcel Hug
|
 
S

schaf

Hi Willy !
You not only have to call LogonUser, you also have to impersonate using the
token obtained by LogonUser. Please post your code.

Yes I know and I do it as you can see in the source code below. But the
behavior is very suspect. When I have the same user on the remote
computer (not in the same domain) as I have local, I can use LogonUser
with different user names registered on the remote computer. I get a
error message but I can access the file system or the services. As soon
as I delete or rename my the account on the remote computer I get no
access. Any suggestions ?

Here my source code of interest:

private WindowsImpersonationContext _ImpersonateUser() {
// initialize tokens
IntPtr pExistingTokenHandle = new IntPtr(0);
IntPtr pDuplicateTokenHandle = new IntPtr(0);
pExistingTokenHandle = IntPtr.Zero;
pDuplicateTokenHandle = IntPtr.Zero;

// if domain name was blank, assume local machine
if (m_sDomain == "") {
m_sDomain = System.Environment.MachineName;
}

try {
string sResult = null;

// get handle to token
bool bImpersonated = LogonUser(m_sUserName, m_sDomain,
m_sPassword,
LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_DEFAULT, ref
pExistingTokenHandle);

// did impersonation fail?
if (false == bImpersonated) {
int nErrorCode = Marshal.GetLastWin32Error();
sResult = "LogonUser() failed with error code: " + nErrorCode
+ "\r\n";

// show the reason why LogonUser failed
MessageBox.Show(this, sResult, "Error", MessageBoxButtons.OK,
MessageBoxIcon.Error);
}

bool bRetVal = DuplicateToken(pExistingTokenHandle, (int)
SECURITY_IMPERSONATION_LEVEL.SecurityImpersonation, ref
pDuplicateTokenHandle);

// did DuplicateToken fail?
if (false == bRetVal) {
int nErrorCode = Marshal.GetLastWin32Error();
CloseHandle(pExistingTokenHandle); // close existing handle
sResult += "DuplicateToken() failed with error code: " +
nErrorCode + "\r\n";

// show the reason why DuplicateToken failed
MessageBox.Show(this, sResult, "Error", MessageBoxButtons.OK,
MessageBoxIcon.Error);
return null;
} else {
// create new identity using new primary token
WindowsIdentity newId = new
WindowsIdentity(pDuplicateTokenHandle);
WindowsImpersonationContext impersonatedUser =
newId.Impersonate();
return impersonatedUser;
}
} catch (Exception ex) {
throw ex;
} finally {
// close handle(s)
if (pExistingTokenHandle != IntPtr.Zero) {
CloseHandle(pExistingTokenHandle);
}
if (pDuplicateTokenHandle != IntPtr.Zero) {
CloseHandle(pDuplicateTokenHandle);
}
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top