I recently went through this, and even though the concept is not simple, the
resulting code is, and doesn't fully use .NET, but uses LogonUser instead.
Firstly, declare some external functions in your class namespace:
namespace a
{
public class b
{
[DllImport("advapi32.DLL", SetLastError = true)]
public static extern int LogonUser(string lpszUsername, string
lpszDomain, string lpszPassword, int dwLogonType, int dwLogonProvider, out
IntPtr phToken);
[DllImport("advapi32.DLL")]
public static extern bool ImpersonateLoggedOnUser(IntPtr hToken); //
handle to token for logged-on user
[DllImport("advapi32.DLL")]
public static extern bool RevertToSelf();
....
Then, in your code you declare a variable:
IntPtr iToken;
if (LogonUser(user_name, remote_server_name (or ip), password, 9, 3, out
iToken) != 0)
{
ImpersonateLoggedOnUser(iToken);
//copy stuff
RevertToSelf();
}
Took me quite some time to work it down to all that.
I'd explain it a little more but I'm stuck for time right now.
HTH.
[quoted text, click to view] "Senthamarai" wrote:
> My program needs to access different network shares from different computers.
> One hidden user has permission to the network shared. Whenever the
> application needs to access any data from network shared, it has to
> impersonate the hidden user and then rever it back to the logged in user.
>
> I found few sample codes using LogonUser, but they don't work in Windows
> 2000 or NT machines. I'm ready to add/remove any security/policy informaiton
> for the network shares, but I don't want to change any policy settings in the
> client computer.
>
> Any solution to this problem (in Windows 2k/NT)?
>
That works great!
But I have a different requirement: I will have videos (WMV Files) stored
in a network share, I want my program (WM Player) to impersonate a particular
user and access the video. I guest WM Player uses its own impersonation
(Logged in user).
I use the following code:
IntPtr iToken;
if (LogonUser(textBox2.Text, textBox1.Text, textBox3.Text, 9, 3, out iToken)
!= 0)
{
ImpersonateLoggedOnUser(iToken);
}
axWindowsMediaPlayer1.URL = "\\\\Server\\public\\Video.wmv";
[quoted text, click to view] "BLiTZWiNG" wrote:
> I recently went through this, and even though the concept is not simple, the
> resulting code is, and doesn't fully use .NET, but uses LogonUser instead.
>
> Firstly, declare some external functions in your class namespace:
>
> namespace a
> {
> public class b
> {
> [DllImport("advapi32.DLL", SetLastError = true)]
> public static extern int LogonUser(string lpszUsername, string
> lpszDomain, string lpszPassword, int dwLogonType, int dwLogonProvider, out
> IntPtr phToken);
>
> [DllImport("advapi32.DLL")]
> public static extern bool ImpersonateLoggedOnUser(IntPtr hToken); //
> handle to token for logged-on user
>
> [DllImport("advapi32.DLL")]
> public static extern bool RevertToSelf();
>
> ...
>
> Then, in your code you declare a variable:
>
> IntPtr iToken;
> if (LogonUser(user_name, remote_server_name (or ip), password, 9, 3, out
> iToken) != 0)
> {
> ImpersonateLoggedOnUser(iToken);
> //copy stuff
> RevertToSelf();
> }
>
> Took me quite some time to work it down to all that.
>
> I'd explain it a little more but I'm stuck for time right now.
> HTH.
>
> "Senthamarai" wrote:
>
> > My program needs to access different network shares from different computers.
> > One hidden user has permission to the network shared. Whenever the
> > application needs to access any data from network shared, it has to
> > impersonate the hidden user and then rever it back to the logged in user.
> >
> > I found few sample codes using LogonUser, but they don't work in Windows
> > 2000 or NT machines. I'm ready to add/remove any security/policy informaiton
> > for the network shares, but I don't want to change any policy settings in the
> > client computer.
> >
> > Any solution to this problem (in Windows 2k/NT)?
> >
Don't see what you're looking for? Try a search.