PC Review


Reply
Thread Tools Rating: Thread Rating: 1 votes, 1.00 average.

Access file on another computer with specific username / password

 
 
=?Utf-8?B?YmFsbWVyY2g=?=
Guest
Posts: n/a
 
      28th Nov 2005
I need to access files on computers around our network, some are on the same
domain as myself, and some are on others. I have an account for each domain
with rights to read files on each system, but I can't figure out how to
specify a username/password when using the System.IO namespace to read files.
Is this possible and if so, how might I accomplish the task?

Chris
 
Reply With Quote
 
 
 
 
Peter Huang [MSFT]
Guest
Posts: n/a
 
      29th Nov 2005
Hi

We need to call LogonUser API to impersonate the current thread running
under another account with the username/password.

841699 How to validate Windows user rights in a Visual Basic .NET
application
http://support.microsoft.com/?id=841699

Impersonate a Specific User in Code
How to implement impersonation in an ASP.NET application
http://support.microsoft.com/default...b;en-us;306158

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.

 
Reply With Quote
 
=?Utf-8?B?YmFsbWVyY2g=?=
Guest
Posts: n/a
 
      29th Nov 2005
Actually, using the System.Management namespace allows me to create a scope
with a specific username / password:

ManagementScope scope = new ManagementScope();
scope.Options.Username = computer.Domain.Name + @"\" +
computer.Domain.Username;
scope.Options.Password = computer.Domain.Password;

And using this namespace, I can copy the ini file I need to read off of
their computer to the applications and read it from there without adding an
impersonation class. Although if someone wants the API way ready to copy and
paste:

using System.Runtime.InteropServices;
using System.Security.Principal;
public class Impersonation
{
public const int LOGON32_LOGON_INTERACTIVE = 2;
public const int LOGON32_PROVIDER_DEFAULT = 0;

WindowsImpersonationContext impersonationContext;

[DllImport( "advapi32.dll" )]
public static extern int LogonUserA( String lpszUserName,
String lpszDomain,
String lpszPassword,
int dwLogonType,
int dwLogonProvider,
ref IntPtr phToken );
[DllImport( "advapi32.dll", CharSet = CharSet.Auto, SetLastError =
true )]
public static extern int DuplicateToken( IntPtr hToken,
int impersonationLevel,
ref IntPtr hNewToken );

[DllImport( "advapi32.dll", CharSet = CharSet.Auto, SetLastError =
true )]
public static extern bool RevertToSelf();

[DllImport( "kernel32.dll", CharSet = CharSet.Auto )]
public static extern bool CloseHandle( IntPtr handle );

public bool Impersonate( String userName, String domain, String
password )
{
WindowsIdentity tempWindowsIdentity;
IntPtr token = IntPtr.Zero;
IntPtr tokenDuplicate = IntPtr.Zero;

if ( RevertToSelf() )
{
if ( LogonUserA( userName, domain, password,
LOGON32_LOGON_INTERACTIVE,
LOGON32_PROVIDER_DEFAULT, ref token ) != 0 )
{
if ( DuplicateToken( token, 2, ref tokenDuplicate ) != 0 )
{
tempWindowsIdentity = new WindowsIdentity(
tokenDuplicate );
impersonationContext =
tempWindowsIdentity.Impersonate();
if ( impersonationContext != null )
{
CloseHandle( token );
CloseHandle( tokenDuplicate );
return true;
}
}
}
}
if ( token != IntPtr.Zero )
CloseHandle( token );
if ( tokenDuplicate != IntPtr.Zero )
CloseHandle( tokenDuplicate );
return false;
}

public void UndoImpersonation()
{
impersonationContext.Undo();
}

}

""Peter Huang" [MSFT]" wrote:

> Hi
>
> We need to call LogonUser API to impersonate the current thread running
> under another account with the username/password.
>
> 841699 How to validate Windows user rights in a Visual Basic .NET
> application
> http://support.microsoft.com/?id=841699
>
> Impersonate a Specific User in Code
> How to implement impersonation in an ASP.NET application
> http://support.microsoft.com/default...b;en-us;306158
>
> Best regards,
>
> Peter Huang
> Microsoft Online Partner Support
>
> Get Secure! - www.microsoft.com/security
> This posting is provided "AS IS" with no warranties, and confers no rights.
>
>

 
Reply With Quote
 
Peter Huang [MSFT]
Guest
Posts: n/a
 
      30th Nov 2005

Hi

Thanks for your knowledge sharing.

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to setup Username/Password for a drive in a Windows XP computer to backup files from a Vista computer? Athena Windows Vista General Discussion 1 31st Aug 2007 02:11 AM
How do I bind username password security to a specific database fi =?Utf-8?B?Q29uYW4gdGhlIExpYnJhcmlhbg==?= Microsoft Access Security 6 18th Jan 2006 05:24 PM
Re-enable username/password caching in IE6 on a specific page Moxy Windows XP Internet Explorer 4 27th Sep 2005 01:41 AM
Unable to access Encrypting File System despite knowing username/password jsausten@hotmail.com Windows XP Security 7 1st May 2005 09:12 AM
How do I set-up an Access username /password file? JPIII Microsoft Access Security 1 12th Nov 2004 05:05 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 07:02 PM.