CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Jan 2006
    Posts
    4

    Accessing Network Resources

    Hi!

    i'm trying to read a file using default share in c#. Is there a method to send the username/password to the remote machine that have the necessary rights to access the default share and read the file. I did this under VC++ with the WNetAddConnection() function.But i can't find anything like this in C#.

    Please advice....
    Thx...Rares

  2. #2
    Join Date
    Mar 2006
    Posts
    6

    Re: Accessing Network Resources

    Hi,
    As far as I know, there're only System.Net to provide socket interface. So for more complicated functions like WNetAddconnection() from <winsock.h> or <winsock2.h> you should use P/Invoke

  3. #3
    Join Date
    Jan 2006
    Posts
    4

    Re: Accessing Network Resources

    Thanks a lot for ur answer. I don't want to use only WNetAddConnection. I would like to know if there is a method in .Net that can give me access to a network resource by providing a username and password that have access to that resource.

    Cheers

  4. #4
    Join Date
    Jun 2005
    Posts
    121

    Re: Accessing Network Resources

    Hi there,
    To answer your question i don't know a method that will do that for you.You will need to impersonate the user prior to accessing your network shared resources:
    Code:
                    using System.Security.Principal;
                    using System.Runtime.InteropServices;
                    .....................
    		#region Impersonate
    
    		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);
    
    		private bool impersonateValidUser(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;
    						}
    					}
    					else
    					{
    
    					}
    				}
    				else
    				{
    
    				}
    			}
    			else
    			{
    
    			}
    			if(token!= IntPtr.Zero)
    				CloseHandle(token);
    			if(tokenDuplicate!=IntPtr.Zero)
    				CloseHandle(tokenDuplicate);
    			return false;
    		}
    
    		private void undoImpersonation()
    		{
    			impersonationContext.Undo();
    		}
    		#endregion		
    
                    ..................................
                    if(impersonateValidUser(USER_NAME, DOMAIN, PASSWORD))
    		{
    			//here you can have your code for reading your file
    			undoImpersonation();
    		}

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured