Ask a Question related to ASP.NET Security, Design and Development.
-
Gaetano D'Aquila #1
Connect to a netword share via UNC name
Hi,
There is anyone that have a piece of C# code that show how to use
Directory.GetDirectories() over a UNC path?
Example:
I need to use Directory.GetDirectories(\\server\resource)
I need to Impersonate "on-the-fly" an user before the call to
Directory.GetDirectories and then restore the old account.
Now I use this (at the end of e-mail) attached class to handle
Impersonification, then I use
CImpersonification i=new CImpersonification()
if i.impersonate("username","password","domain")
Directory.GetDirectories(\\\\server\\resource)
But not work.
Anyone can help me?
Thank you.
===================== START HERE ========================================
using System;
using System.Runtime.InteropServices;
using System.Security.Principal;
using System.Security.Permissions;
namespace test
{
public class CImpersonation
{
private WindowsIdentity newidentity;
private WindowsImpersonationContext impcontext;
// Declare the logon types as constants
const long LOGON32_LOGON_INTERACTIVE = 2;
const long LOGON32_LOGON_NETWORK = 3;
// Declare the logon providers as constants
const long LOGON32_PROVIDER_DEFAULT = 0;
const long LOGON32_PROVIDER_WINNT50 = 3;
const long LOGON32_PROVIDER_WINNT40 = 2;
const long LOGON32_PROVIDER_WINNT35 = 1;
[DllImport("advapi32.dll",EntryPoint = "LogonUser")]
private static extern bool LogonUser(
string lpszUsername,
string lpszDomain,
string lpszPassword,
int dwLogonType,
int dwLogonProvider,
ref IntPtr phToken);
public bool impersonate(
string Username,
string Password,
string Domain)
{
// This is the token returned by the API call
// Look forward to a future article covering
// the uses of it
IntPtr token = new IntPtr(0);
token = IntPtr.Zero;
// Call the API
if (LogonUser(
Username,
Domain,
Password,
(int)LOGON32_LOGON_NETWORK,
(int)LOGON32_PROVIDER_DEFAULT,
ref token))
{
//' Since the API returned TRUE, return TRUE to the caller
newidentity = WindowsIdentity.GetCurrent();
impcontext = WindowsIdentity.Impersonate(token);
return true;
}
else
{
//' Bad credentials, return FALSE
return false ;
}
}
public void revertimpersonation()
{
impcontext.Undo();
//newidentity = Nothing;
}
}
}
===================== STOP HERE ==========================================
Gaetano D'Aquila Guest
-
Automated Netword printing
NetSoft is a custom IE based web browser which uses JavaScript and cookies to facilitate automated printing to any local or network printer. Ever... -
How to use “RDS.Connect” to connect to a MS Access database?
The example I am working from uses the following code which does not work: RDS.Connect = "Provider='sqloledb';Integrated Security='SSPI';Initial ... -
Win 98 connect win 2003 share folder
Can a win 98 connects win 2003 share folders? I got a message ask for password with resource \\computername\IPC$ Both win 98 and win 2003 have... -
UNC Share Error
I'm trhing to create a WEB using VB.net and teh 1.1 net framwork on a remote Windows 2003 running IIS 6.0. If keep getting the error "Cannot create... -
Connect VPN...just fine. Can't map to share drive through VPN Connection???
Hello all; I am able to VPN to my network at work and check mail using Outlook 2000 with no problems.. I connect just fine. However, if I try to...



Reply With Quote

