Ask a Question related to ASP.NET Security, Design and Development.
-
Mark Duregon #1
Access File Share from ASP.NET using Unmanaged Code
Hi,
We have an application that requires appropriate users to run command files on an adhoc basis. We have implmented a library that uses the following code:
using System;
using System.Runtime.InteropServices;
using System.Security.Principal;
using System.Security.Permissions;
namespace SAMIS.Porteco.Utilities
{
public enum LogonType : int
{
LOGON32_LOGON_INTERACTIVE = 2,
LOGON32_LOGON_NETWORK = 3,
LOGON32_LOGON_BATCH = 4,
LOGON32_LOGON_SERVICE = 5,
LOGON32_LOGON_UNLOCK = 7,
LOGON32_LOGON_NETWORK_CLEARTEXT = 8, // Only for Win2K or higher
LOGON32_LOGON_NEW_CREDENTIALS = 9 // Only for Win2K or higher
};
public enum LogonProvider : int
{
LOGON32_PROVIDER_DEFAULT = 0,
LOGON32_PROVIDER_WINNT35 = 1,
LOGON32_PROVIDER_WINNT40 = 2,
LOGON32_PROVIDER_WINNT50 = 3
};
class SecuUtil32
{
[DllImport("advapi32.dll", SetLastError=true)]
public static extern bool LogonUser(String lpszUsername, String lpszDomain, String lpszPassword,
int dwLogonType, int dwLogonProvider, ref IntPtr TokenHandle);
[DllImport("kernel32.dll", CharSet=CharSet.Auto)]
public extern static bool CloseHandle(IntPtr handle);
[DllImport("advapi32.dll", CharSet=CharSet.Auto, SetLastError=true)]
public extern static bool DuplicateToken(IntPtr ExistingTokenHandle,
int SECURITY_IMPERSONATION_LEVEL, ref IntPtr DuplicateTokenHandle);
}
/// <summary>
/// Summary description for NetworkSecurity.
/// </summary>
public class NetworkSecurity
{
private NetworkSecurity() {}
public static WindowsImpersonationContext ImpersonateUser(string domain, string login, string password,
LogonType logonType, LogonProvider logonProvider)
{
IntPtr tokenHandle = new IntPtr(0);
IntPtr dupeTokenHandle = new IntPtr(0);
try
{
const int SecurityImpersonation = 2;
tokenHandle = IntPtr.Zero;
dupeTokenHandle = IntPtr.Zero;
//
// Call LogonUser to obtain a handle to an access token.
//
bool returnValue = SecuUtil32.LogonUser(login, domain, password, (int)logonType,
(int)logonProvider, ref tokenHandle);
if (false == returnValue)
{
int ret = Marshal.GetLastWin32Error();
string strErr = String.Format("LogonUser failed with error code : {0}", ret);
throw new ApplicationException(strErr, null);
}
bool retVal = SecuUtil32.DuplicateToken(tokenHandle, SecurityImpersonation, ref dupeTokenHandle);
if (false == retVal)
{
SecuUtil32.CloseHandle(tokenHandle);
throw new ApplicationException("Failed to duplicate token", null);
}
//
// The token that is passed to the following constructor must
// be a primary token in order to use it for impersonation.
//
WindowsIdentity newId = new WindowsIdentity(dupeTokenHandle);
WindowsImpersonationContext impersonatedUser = newId.Impersonate();
return impersonatedUser;
}
catch (Exception ex)
{
throw new ApplicationException(ex.Message, ex);
}
return null;
}
}
}
The problem we are having is that while network resources are not restricted entirely because the batch files are able to run sql scripts against the Oracle database, FTP etc. but the user cannot access a network share either by unc path or trying to map a drive as part of the script. This problem only occurs when trying to run the script in this fashion as it works when run manually through a command prompt whic is expected, an also on a scheduled basis by the Windows Scheduler.
Is their a permission I need to request/grant on the assembly and if so which assembly (the library/web or both). I have tried granting full trust to the assemblies without success.
Alternatively is their a way to run a defined task from the scheduler. I read the documentation (all 2 lines of it) for the scheduler and did not get the impression that it is possible.
Regards,
Mark.
P.S. I cannot give you an exception or error messages that occur when I try to run the task from the web application, because as soon as I try to access a network resource using the page I have created it simply hangs/timesout but works perfectly when dealing with only local file resources. FYI all command files are on the local machine but need to access network shares to ctp then delete files.
Platform: Windows 2000 Server w/ 1.0 Framework
Mark Duregon Guest
-
Can I share an Access MDW file for security.
Hi all, I am trying to set up a web application to connect to an old Access 97 database. The database uses a workgroup.mdw workgroup file for its... -
ASP.NET App with Unmanaged Code - HELP!
I think the following is a security configuration issue, can someone help? I've been stumped on this for a couple of days now and I'm feeling the... -
Web Services and Unmanaged Code
Hello, We are new in the .NET environment, and we have a working web app under Windows environment. I'm building a .NET Web Service in C#,... -
Can you share a code behind file with a page and usercontrol?
Tying not to spaghetti code which seems to be easy to do in .net, im trying to do my main .net html in index.aspx, use repeated .net html in an... -
Security problem with Managed Code calling Unmanaged Code in a Web Page
Hello, I have a web page which contains an ActiveX control (unmanaged) and a Windows Forms User Control (managed). Both reside on a web page and... -
David Coe, MCAD #2
RE: Access File Share from ASP.NET using Unmanaged Code
Do you have impersonation enabled in your web.config file, and Windows authentication setup in IIS?
"Mark Duregon" wrote:
> Hi,
>
> We have an application that requires appropriate users to run command files on an adhoc basis. We have implmented a library that uses the following code:
>
> using System;
> using System.Runtime.InteropServices;
> using System.Security.Principal;
> using System.Security.Permissions;
>
> namespace SAMIS.Porteco.Utilities
> {
> public enum LogonType : int
> {
> LOGON32_LOGON_INTERACTIVE = 2,
> LOGON32_LOGON_NETWORK = 3,
> LOGON32_LOGON_BATCH = 4,
> LOGON32_LOGON_SERVICE = 5,
> LOGON32_LOGON_UNLOCK = 7,
> LOGON32_LOGON_NETWORK_CLEARTEXT = 8, // Only for Win2K or higher
> LOGON32_LOGON_NEW_CREDENTIALS = 9 // Only for Win2K or higher
> };
>
> public enum LogonProvider : int
> {
> LOGON32_PROVIDER_DEFAULT = 0,
> LOGON32_PROVIDER_WINNT35 = 1,
> LOGON32_PROVIDER_WINNT40 = 2,
> LOGON32_PROVIDER_WINNT50 = 3
> };
>
> class SecuUtil32
> {
> [DllImport("advapi32.dll", SetLastError=true)]
> public static extern bool LogonUser(String lpszUsername, String lpszDomain, String lpszPassword,
> int dwLogonType, int dwLogonProvider, ref IntPtr TokenHandle);
>
> [DllImport("kernel32.dll", CharSet=CharSet.Auto)]
> public extern static bool CloseHandle(IntPtr handle);
>
> [DllImport("advapi32.dll", CharSet=CharSet.Auto, SetLastError=true)]
> public extern static bool DuplicateToken(IntPtr ExistingTokenHandle,
> int SECURITY_IMPERSONATION_LEVEL, ref IntPtr DuplicateTokenHandle);
> }
>
> /// <summary>
> /// Summary description for NetworkSecurity.
> /// </summary>
> public class NetworkSecurity
> {
> private NetworkSecurity() {}
>
> public static WindowsImpersonationContext ImpersonateUser(string domain, string login, string password,
> LogonType logonType, LogonProvider logonProvider)
> {
> IntPtr tokenHandle = new IntPtr(0);
> IntPtr dupeTokenHandle = new IntPtr(0);
> try
> {
> const int SecurityImpersonation = 2;
>
> tokenHandle = IntPtr.Zero;
> dupeTokenHandle = IntPtr.Zero;
>
> //
> // Call LogonUser to obtain a handle to an access token.
> //
> bool returnValue = SecuUtil32.LogonUser(login, domain, password, (int)logonType,
> (int)logonProvider, ref tokenHandle);
>
> if (false == returnValue)
> {
> int ret = Marshal.GetLastWin32Error();
> string strErr = String.Format("LogonUser failed with error code : {0}", ret);
> throw new ApplicationException(strErr, null);
> }
>
> bool retVal = SecuUtil32.DuplicateToken(tokenHandle, SecurityImpersonation, ref dupeTokenHandle);
>
> if (false == retVal)
> {
> SecuUtil32.CloseHandle(tokenHandle);
> throw new ApplicationException("Failed to duplicate token", null);
> }
>
> //
> // The token that is passed to the following constructor must
> // be a primary token in order to use it for impersonation.
> //
> WindowsIdentity newId = new WindowsIdentity(dupeTokenHandle);
> WindowsImpersonationContext impersonatedUser = newId.Impersonate();
>
> return impersonatedUser;
> }
> catch (Exception ex)
> {
> throw new ApplicationException(ex.Message, ex);
> }
>
> return null;
> }
> }
> }
>
> The problem we are having is that while network resources are not restricted entirely because the batch files are able to run sql scripts against the Oracle database, FTP etc. but the user cannot access a network share either by unc path or trying to map a drive as part of the script. This problem only occurs when trying to run the script in this fashion as it works when run manually through a command prompt whic is expected, an also on a scheduled basis by the Windows Scheduler.
>
> Is their a permission I need to request/grant on the assembly and if so which assembly (the library/web or both). I have tried granting full trust to the assemblies without success.
>
> Alternatively is their a way to run a defined task from the scheduler. I read the documentation (all 2 lines of it) for the scheduler and did not get the impression that it is possible.
>
> Regards,
> Mark.
>
> P.S. I cannot give you an exception or error messages that occur when I try to run the task from the web application, because as soon as I try to access a network resource using the page I have created it simply hangs/timesout but works perfectly when dealing with only local file resources. FYI all command files are on the local machine but need to access network shares to ctp then delete files.
>
> Platform: Windows 2000 Server w/ 1.0 FrameworkDavid Coe, MCAD Guest
-
Mark Duregon #3
RE: Access File Share from ASP.NET using Unmanaged Code
We use Forms authentication and if I set impersonate to true then I get an Access Denied excpetion when trying to access our Business Facade layer:
Access is denied: 'BusinessFacade'.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.IO.FileLoadException: Access is denied: 'BusinessFacade'.
Source Error:
An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.
Stack Trace:
[FileLoadException: Access is denied: 'BusinessFacade'.]
SAMIS.Porteco.Web.Global.Application_AuthenticateR equest(Object sender, EventArgs e) +0
System.Web.SyncEventExecutionStep.Execute() +60
System.Web.HttpApplication.ExecuteStep(IExecutionS tep step, Boolean& completedSynchronously) +87
--------------------------------------------------------------------------------
Version Information: Microsoft .NET Framework Version:1.0.3705.288; ASP.NET Version:1.0.3705.288
"David Coe, MCAD" wrote:
> Do you have impersonation enabled in your web.config file, and Windows authentication setup in IIS?
>
> "Mark Duregon" wrote:
>> > Hi,
> >
> > We have an application that requires appropriate users to run command files on an adhoc basis. We have implmented a library that uses the following code:
> >
> > using System;
> > using System.Runtime.InteropServices;
> > using System.Security.Principal;
> > using System.Security.Permissions;
> >
> > namespace SAMIS.Porteco.Utilities
> > {
> > public enum LogonType : int
> > {
> > LOGON32_LOGON_INTERACTIVE = 2,
> > LOGON32_LOGON_NETWORK = 3,
> > LOGON32_LOGON_BATCH = 4,
> > LOGON32_LOGON_SERVICE = 5,
> > LOGON32_LOGON_UNLOCK = 7,
> > LOGON32_LOGON_NETWORK_CLEARTEXT = 8, // Only for Win2K or higher
> > LOGON32_LOGON_NEW_CREDENTIALS = 9 // Only for Win2K or higher
> > };
> >
> > public enum LogonProvider : int
> > {
> > LOGON32_PROVIDER_DEFAULT = 0,
> > LOGON32_PROVIDER_WINNT35 = 1,
> > LOGON32_PROVIDER_WINNT40 = 2,
> > LOGON32_PROVIDER_WINNT50 = 3
> > };
> >
> > class SecuUtil32
> > {
> > [DllImport("advapi32.dll", SetLastError=true)]
> > public static extern bool LogonUser(String lpszUsername, String lpszDomain, String lpszPassword,
> > int dwLogonType, int dwLogonProvider, ref IntPtr TokenHandle);
> >
> > [DllImport("kernel32.dll", CharSet=CharSet.Auto)]
> > public extern static bool CloseHandle(IntPtr handle);
> >
> > [DllImport("advapi32.dll", CharSet=CharSet.Auto, SetLastError=true)]
> > public extern static bool DuplicateToken(IntPtr ExistingTokenHandle,
> > int SECURITY_IMPERSONATION_LEVEL, ref IntPtr DuplicateTokenHandle);
> > }
> >
> > /// <summary>
> > /// Summary description for NetworkSecurity.
> > /// </summary>
> > public class NetworkSecurity
> > {
> > private NetworkSecurity() {}
> >
> > public static WindowsImpersonationContext ImpersonateUser(string domain, string login, string password,
> > LogonType logonType, LogonProvider logonProvider)
> > {
> > IntPtr tokenHandle = new IntPtr(0);
> > IntPtr dupeTokenHandle = new IntPtr(0);
> > try
> > {
> > const int SecurityImpersonation = 2;
> >
> > tokenHandle = IntPtr.Zero;
> > dupeTokenHandle = IntPtr.Zero;
> >
> > //
> > // Call LogonUser to obtain a handle to an access token.
> > //
> > bool returnValue = SecuUtil32.LogonUser(login, domain, password, (int)logonType,
> > (int)logonProvider, ref tokenHandle);
> >
> > if (false == returnValue)
> > {
> > int ret = Marshal.GetLastWin32Error();
> > string strErr = String.Format("LogonUser failed with error code : {0}", ret);
> > throw new ApplicationException(strErr, null);
> > }
> >
> > bool retVal = SecuUtil32.DuplicateToken(tokenHandle, SecurityImpersonation, ref dupeTokenHandle);
> >
> > if (false == retVal)
> > {
> > SecuUtil32.CloseHandle(tokenHandle);
> > throw new ApplicationException("Failed to duplicate token", null);
> > }
> >
> > //
> > // The token that is passed to the following constructor must
> > // be a primary token in order to use it for impersonation.
> > //
> > WindowsIdentity newId = new WindowsIdentity(dupeTokenHandle);
> > WindowsImpersonationContext impersonatedUser = newId.Impersonate();
> >
> > return impersonatedUser;
> > }
> > catch (Exception ex)
> > {
> > throw new ApplicationException(ex.Message, ex);
> > }
> >
> > return null;
> > }
> > }
> > }
> >
> > The problem we are having is that while network resources are not restricted entirely because the batch files are able to run sql scripts against the Oracle database, FTP etc. but the user cannot access a network share either by unc path or trying to map a drive as part of the script. This problem only occurs when trying to run the script in this fashion as it works when run manually through a command prompt whic is expected, an also on a scheduled basis by the Windows Scheduler.
> >
> > Is their a permission I need to request/grant on the assembly and if so which assembly (the library/web or both). I have tried granting full trust to the assemblies without success.
> >
> > Alternatively is their a way to run a defined task from the scheduler. I read the documentation (all 2 lines of it) for the scheduler and did not get the impression that it is possible.
> >
> > Regards,
> > Mark.
> >
> > P.S. I cannot give you an exception or error messages that occur when I try to run the task from the web application, because as soon as I try to access a network resource using the page I have created it simply hangs/timesout but works perfectly when dealing with only local file resources. FYI all command files are on the local machine but need to access network shares to ctp then delete files.
> >
> > Platform: Windows 2000 Server w/ 1.0 FrameworkMark Duregon Guest
-
Joe Kaplan \(MVP - ADSI\) #4
Re: Access File Share from ASP.NET using Unmanaged Code
How are you calling the script files in this app? Are you using the Process
class? In that case, you need to be aware that it will start the new
process with the current process' token, not the impersonation token. Since
it would appear that you have a primary token, you could get around this by
calling CreateProcessWithTokenW instead.
If that isn't how you are calling the scripts, then how are you doing it?
Joe K.
"Mark Duregon" <msdnonline@aspect.com.au> wrote in message
news:5A1E8402-3951-4673-B370-DBB71E5C85CA@microsoft.com...files on an adhoc basis. We have implmented a library that uses the> Hi,
>
> We have an application that requires appropriate users to run command
following code:lpszDomain, String lpszPassword,>
> using System;
> using System.Runtime.InteropServices;
> using System.Security.Principal;
> using System.Security.Permissions;
>
> namespace SAMIS.Porteco.Utilities
> {
> public enum LogonType : int
> {
> LOGON32_LOGON_INTERACTIVE = 2,
> LOGON32_LOGON_NETWORK = 3,
> LOGON32_LOGON_BATCH = 4,
> LOGON32_LOGON_SERVICE = 5,
> LOGON32_LOGON_UNLOCK = 7,
> LOGON32_LOGON_NETWORK_CLEARTEXT = 8, // Only for Win2K or higher
> LOGON32_LOGON_NEW_CREDENTIALS = 9 // Only for Win2K or higher
> };
>
> public enum LogonProvider : int
> {
> LOGON32_PROVIDER_DEFAULT = 0,
> LOGON32_PROVIDER_WINNT35 = 1,
> LOGON32_PROVIDER_WINNT40 = 2,
> LOGON32_PROVIDER_WINNT50 = 3
> };
>
> class SecuUtil32
> {
> [DllImport("advapi32.dll", SetLastError=true)]
> public static extern bool LogonUser(String lpszUsername, Stringdomain, string login, string password,> int dwLogonType, int dwLogonProvider, ref IntPtr TokenHandle);
>
> [DllImport("kernel32.dll", CharSet=CharSet.Auto)]
> public extern static bool CloseHandle(IntPtr handle);
>
> [DllImport("advapi32.dll", CharSet=CharSet.Auto, SetLastError=true)]
> public extern static bool DuplicateToken(IntPtr ExistingTokenHandle,
> int SECURITY_IMPERSONATION_LEVEL, ref IntPtr DuplicateTokenHandle);
> }
>
> /// <summary>
> /// Summary description for NetworkSecurity.
> /// </summary>
> public class NetworkSecurity
> {
> private NetworkSecurity() {}
>
> public static WindowsImpersonationContext ImpersonateUser(string(int)logonType,> LogonType logonType, LogonProvider logonProvider)
> {
> IntPtr tokenHandle = new IntPtr(0);
> IntPtr dupeTokenHandle = new IntPtr(0);
> try
> {
> const int SecurityImpersonation = 2;
>
> tokenHandle = IntPtr.Zero;
> dupeTokenHandle = IntPtr.Zero;
>
> //
> // Call LogonUser to obtain a handle to an access token.
> //
> bool returnValue = SecuUtil32.LogonUser(login, domain, password,: {0}", ret);> (int)logonProvider, ref tokenHandle);
>
> if (false == returnValue)
> {
> int ret = Marshal.GetLastWin32Error();
> string strErr = String.Format("LogonUser failed with error codeSecurityImpersonation, ref dupeTokenHandle);> throw new ApplicationException(strErr, null);
> }
>
> bool retVal = SecuUtil32.DuplicateToken(tokenHandle,null);>
> if (false == retVal)
> {
> SecuUtil32.CloseHandle(tokenHandle);
> throw new ApplicationException("Failed to duplicate token",newId.Impersonate();> }
>
> //
> // The token that is passed to the following constructor must
> // be a primary token in order to use it for impersonation.
> //
> WindowsIdentity newId = new WindowsIdentity(dupeTokenHandle);
> WindowsImpersonationContext impersonatedUser =restricted entirely because the batch files are able to run sql scripts>
> return impersonatedUser;
> }
> catch (Exception ex)
> {
> throw new ApplicationException(ex.Message, ex);
> }
>
> return null;
> }
> }
> }
>
> The problem we are having is that while network resources are not
against the Oracle database, FTP etc. but the user cannot access a network
share either by unc path or trying to map a drive as part of the script.
This problem only occurs when trying to run the script in this fashion as it
works when run manually through a command prompt whic is expected, an also
on a scheduled basis by the Windows Scheduler.which assembly (the library/web or both). I have tried granting full trust>
> Is their a permission I need to request/grant on the assembly and if so
to the assemblies without success.read the documentation (all 2 lines of it) for the scheduler and did not get>
> Alternatively is their a way to run a defined task from the scheduler. I
the impression that it is possible.try to run the task from the web application, because as soon as I try to>
> Regards,
> Mark.
>
> P.S. I cannot give you an exception or error messages that occur when I
access a network resource using the page I have created it simply
hangs/timesout but works perfectly when dealing with only local file
resources. FYI all command files are on the local machine but need to
access network shares to ctp then delete files.>
> Platform: Windows 2000 Server w/ 1.0 Framework
Joe Kaplan \(MVP - ADSI\) Guest
-
Mark Duregon #5
Re: Access File Share from ASP.NET using Unmanaged Code
Thanks, but I did mention that I am using Windows 2000 and 1.0 of the framework.
"Joe Kaplan (MVP - ADSI)" wrote:
> How are you calling the script files in this app? Are you using the Process
> class? In that case, you need to be aware that it will start the new
> process with the current process' token, not the impersonation token. Since
> it would appear that you have a primary token, you could get around this by
> calling CreateProcessWithTokenW instead.
>
> If that isn't how you are calling the scripts, then how are you doing it?
>
> Joe K.
>
> "Mark Duregon" <msdnonline@aspect.com.au> wrote in message
> news:5A1E8402-3951-4673-B370-DBB71E5C85CA@microsoft.com...> files on an adhoc basis. We have implmented a library that uses the> > Hi,
> >
> > We have an application that requires appropriate users to run command
> following code:> lpszDomain, String lpszPassword,> >
> > using System;
> > using System.Runtime.InteropServices;
> > using System.Security.Principal;
> > using System.Security.Permissions;
> >
> > namespace SAMIS.Porteco.Utilities
> > {
> > public enum LogonType : int
> > {
> > LOGON32_LOGON_INTERACTIVE = 2,
> > LOGON32_LOGON_NETWORK = 3,
> > LOGON32_LOGON_BATCH = 4,
> > LOGON32_LOGON_SERVICE = 5,
> > LOGON32_LOGON_UNLOCK = 7,
> > LOGON32_LOGON_NETWORK_CLEARTEXT = 8, // Only for Win2K or higher
> > LOGON32_LOGON_NEW_CREDENTIALS = 9 // Only for Win2K or higher
> > };
> >
> > public enum LogonProvider : int
> > {
> > LOGON32_PROVIDER_DEFAULT = 0,
> > LOGON32_PROVIDER_WINNT35 = 1,
> > LOGON32_PROVIDER_WINNT40 = 2,
> > LOGON32_PROVIDER_WINNT50 = 3
> > };
> >
> > class SecuUtil32
> > {
> > [DllImport("advapi32.dll", SetLastError=true)]
> > public static extern bool LogonUser(String lpszUsername, String> domain, string login, string password,> > int dwLogonType, int dwLogonProvider, ref IntPtr TokenHandle);
> >
> > [DllImport("kernel32.dll", CharSet=CharSet.Auto)]
> > public extern static bool CloseHandle(IntPtr handle);
> >
> > [DllImport("advapi32.dll", CharSet=CharSet.Auto, SetLastError=true)]
> > public extern static bool DuplicateToken(IntPtr ExistingTokenHandle,
> > int SECURITY_IMPERSONATION_LEVEL, ref IntPtr DuplicateTokenHandle);
> > }
> >
> > /// <summary>
> > /// Summary description for NetworkSecurity.
> > /// </summary>
> > public class NetworkSecurity
> > {
> > private NetworkSecurity() {}
> >
> > public static WindowsImpersonationContext ImpersonateUser(string> (int)logonType,> > LogonType logonType, LogonProvider logonProvider)
> > {
> > IntPtr tokenHandle = new IntPtr(0);
> > IntPtr dupeTokenHandle = new IntPtr(0);
> > try
> > {
> > const int SecurityImpersonation = 2;
> >
> > tokenHandle = IntPtr.Zero;
> > dupeTokenHandle = IntPtr.Zero;
> >
> > //
> > // Call LogonUser to obtain a handle to an access token.
> > //
> > bool returnValue = SecuUtil32.LogonUser(login, domain, password,> : {0}", ret);> > (int)logonProvider, ref tokenHandle);
> >
> > if (false == returnValue)
> > {
> > int ret = Marshal.GetLastWin32Error();
> > string strErr = String.Format("LogonUser failed with error code> SecurityImpersonation, ref dupeTokenHandle);> > throw new ApplicationException(strErr, null);
> > }
> >
> > bool retVal = SecuUtil32.DuplicateToken(tokenHandle,> null);> >
> > if (false == retVal)
> > {
> > SecuUtil32.CloseHandle(tokenHandle);
> > throw new ApplicationException("Failed to duplicate token",> newId.Impersonate();> > }
> >
> > //
> > // The token that is passed to the following constructor must
> > // be a primary token in order to use it for impersonation.
> > //
> > WindowsIdentity newId = new WindowsIdentity(dupeTokenHandle);
> > WindowsImpersonationContext impersonatedUser => restricted entirely because the batch files are able to run sql scripts> >
> > return impersonatedUser;
> > }
> > catch (Exception ex)
> > {
> > throw new ApplicationException(ex.Message, ex);
> > }
> >
> > return null;
> > }
> > }
> > }
> >
> > The problem we are having is that while network resources are not
> against the Oracle database, FTP etc. but the user cannot access a network
> share either by unc path or trying to map a drive as part of the script.
> This problem only occurs when trying to run the script in this fashion as it
> works when run manually through a command prompt whic is expected, an also
> on a scheduled basis by the Windows Scheduler.> which assembly (the library/web or both). I have tried granting full trust> >
> > Is their a permission I need to request/grant on the assembly and if so
> to the assemblies without success.> read the documentation (all 2 lines of it) for the scheduler and did not get> >
> > Alternatively is their a way to run a defined task from the scheduler. I
> the impression that it is possible.> try to run the task from the web application, because as soon as I try to> >
> > Regards,
> > Mark.
> >
> > P.S. I cannot give you an exception or error messages that occur when I
> access a network resource using the page I have created it simply
> hangs/timesout but works perfectly when dealing with only local file
> resources. FYI all command files are on the local machine but need to
> access network shares to ctp then delete files.>> >
> > Platform: Windows 2000 Server w/ 1.0 Framework
>
>Mark Duregon Guest
-
[MSFT] #6
Re: Access File Share from ASP.NET using Unmanaged Code
I agree with Joe. CreateProcessWithTokenW should be able to work in this
senario (Wins2000 and framework 1.0). Here is a example:
[url]http://support.microsoft.com/default.aspx?scid=kb;en-us;285879[/url]
Luke
[MSFT] Guest
-
Joe Kaplan \(MVP - ADSI\) #7
Re: Access File Share from ASP.NET using Unmanaged Code
Yes, but my question was how are you launching the vbscript processes? I
understand the Win2K/1.0 part.
Joe K.
"Mark Duregon" <msdnonline@aspect.com.au> wrote in message
news:47607F44-4AF2-4501-857F-5E486376C687@microsoft.com...framework.> Thanks, but I did mention that I am using Windows 2000 and 1.0 of theProcess>
> "Joe Kaplan (MVP - ADSI)" wrote:
>> > How are you calling the script files in this app? Are you using theSince> > class? In that case, you need to be aware that it will start the new
> > process with the current process' token, not the impersonation token.by> > it would appear that you have a primary token, you could get around thisit?> > calling CreateProcessWithTokenW instead.
> >
> > If that isn't how you are calling the scripts, then how are you doingSetLastError=true)]> >
> > Joe K.
> >
> > "Mark Duregon" <msdnonline@aspect.com.au> wrote in message
> > news:5A1E8402-3951-4673-B370-DBB71E5C85CA@microsoft.com...> > files on an adhoc basis. We have implmented a library that uses the> > > Hi,
> > >
> > > We have an application that requires appropriate users to run command
> > following code:> > lpszDomain, String lpszPassword,> > >
> > > using System;
> > > using System.Runtime.InteropServices;
> > > using System.Security.Principal;
> > > using System.Security.Permissions;
> > >
> > > namespace SAMIS.Porteco.Utilities
> > > {
> > > public enum LogonType : int
> > > {
> > > LOGON32_LOGON_INTERACTIVE = 2,
> > > LOGON32_LOGON_NETWORK = 3,
> > > LOGON32_LOGON_BATCH = 4,
> > > LOGON32_LOGON_SERVICE = 5,
> > > LOGON32_LOGON_UNLOCK = 7,
> > > LOGON32_LOGON_NETWORK_CLEARTEXT = 8, // Only for Win2K or higher
> > > LOGON32_LOGON_NEW_CREDENTIALS = 9 // Only for Win2K or higher
> > > };
> > >
> > > public enum LogonProvider : int
> > > {
> > > LOGON32_PROVIDER_DEFAULT = 0,
> > > LOGON32_PROVIDER_WINNT35 = 1,
> > > LOGON32_PROVIDER_WINNT40 = 2,
> > > LOGON32_PROVIDER_WINNT50 = 3
> > > };
> > >
> > > class SecuUtil32
> > > {
> > > [DllImport("advapi32.dll", SetLastError=true)]
> > > public static extern bool LogonUser(String lpszUsername, String> > > int dwLogonType, int dwLogonProvider, ref IntPtr TokenHandle);
> > >
> > > [DllImport("kernel32.dll", CharSet=CharSet.Auto)]
> > > public extern static bool CloseHandle(IntPtr handle);
> > >
> > > [DllImport("advapi32.dll", CharSet=CharSet.Auto,ExistingTokenHandle,> > > public extern static bool DuplicateToken(IntPtrDuplicateTokenHandle);> > > int SECURITY_IMPERSONATION_LEVEL, ref IntPtrpassword,> > domain, string login, string password,> > > }
> > >
> > > /// <summary>
> > > /// Summary description for NetworkSecurity.
> > > /// </summary>
> > > public class NetworkSecurity
> > > {
> > > private NetworkSecurity() {}
> > >
> > > public static WindowsImpersonationContext ImpersonateUser(string> > > LogonType logonType, LogonProvider logonProvider)
> > > {
> > > IntPtr tokenHandle = new IntPtr(0);
> > > IntPtr dupeTokenHandle = new IntPtr(0);
> > > try
> > > {
> > > const int SecurityImpersonation = 2;
> > >
> > > tokenHandle = IntPtr.Zero;
> > > dupeTokenHandle = IntPtr.Zero;
> > >
> > > //
> > > // Call LogonUser to obtain a handle to an access token.
> > > //
> > > bool returnValue = SecuUtil32.LogonUser(login, domain,code> > (int)logonType,> > > (int)logonProvider, ref tokenHandle);
> > >
> > > if (false == returnValue)
> > > {
> > > int ret = Marshal.GetLastWin32Error();
> > > string strErr = String.Format("LogonUser failed with errornetwork> > : {0}", ret);> > SecurityImpersonation, ref dupeTokenHandle);> > > throw new ApplicationException(strErr, null);
> > > }
> > >
> > > bool retVal = SecuUtil32.DuplicateToken(tokenHandle,> > null);> > >
> > > if (false == retVal)
> > > {
> > > SecuUtil32.CloseHandle(tokenHandle);
> > > throw new ApplicationException("Failed to duplicate token",> > newId.Impersonate();> > > }
> > >
> > > //
> > > // The token that is passed to the following constructor must
> > > // be a primary token in order to use it for impersonation.
> > > //
> > > WindowsIdentity newId = new WindowsIdentity(dupeTokenHandle);
> > > WindowsImpersonationContext impersonatedUser => > restricted entirely because the batch files are able to run sql scripts> > >
> > > return impersonatedUser;
> > > }
> > > catch (Exception ex)
> > > {
> > > throw new ApplicationException(ex.Message, ex);
> > > }
> > >
> > > return null;
> > > }
> > > }
> > > }
> > >
> > > The problem we are having is that while network resources are not
> > against the Oracle database, FTP etc. but the user cannot access aas it> > share either by unc path or trying to map a drive as part of the script.
> > This problem only occurs when trying to run the script in this fashionalso> > works when run manually through a command prompt whic is expected, anso> > on a scheduled basis by the Windows Scheduler.> > >
> > > Is their a permission I need to request/grant on the assembly and iftrust> > which assembly (the library/web or both). I have tried granting fullI> > to the assemblies without success.> > >
> > > Alternatively is their a way to run a defined task from the scheduler.get> > read the documentation (all 2 lines of it) for the scheduler and did notI> > the impression that it is possible.> > >
> > > Regards,
> > > Mark.
> > >
> > > P.S. I cannot give you an exception or error messages that occur whento> > try to run the task from the web application, because as soon as I try> > access a network resource using the page I have created it simply
> > hangs/timesout but works perfectly when dealing with only local file
> > resources. FYI all command files are on the local machine but need to
> > access network shares to ctp then delete files.> >> > >
> > > Platform: Windows 2000 Server w/ 1.0 Framework
> >
> >
Joe Kaplan \(MVP - ADSI\) Guest
-
Mark Duregon #8
Re: Access File Share from ASP.NET using Unmanaged Code
Sorry Joe,
I read your suggestion and looked at the documentation for CreateProcessWithTokenW and saw that it stated it was a Win2003 feature only. I then saw [MSFT] post a reply with a link that does suggest an article that does give an example that suggests it was introduced as part of Win2000. I am now grappling with trying to convert that example to C# with my extremly rusty VB skills, gave up on that and tried to create a VB.NET library of just that code but it appears to be VB6 and not VB.NET.
Does anyone have this code in C#? I think my VB skills are too rusty and I am not having any luck whatsoever with this.
"Joe Kaplan (MVP - ADSI)" wrote:
> Yes, but my question was how are you launching the vbscript processes? I
> understand the Win2K/1.0 part.
>
> Joe K.
>
> "Mark Duregon" <msdnonline@aspect.com.au> wrote in message
> news:47607F44-4AF2-4501-857F-5E486376C687@microsoft.com...> framework.> > Thanks, but I did mention that I am using Windows 2000 and 1.0 of the> Process> >
> > "Joe Kaplan (MVP - ADSI)" wrote:
> >> > > How are you calling the script files in this app? Are you using the> Since> > > class? In that case, you need to be aware that it will start the new
> > > process with the current process' token, not the impersonation token.> by> > > it would appear that you have a primary token, you could get around this> it?> > > calling CreateProcessWithTokenW instead.
> > >
> > > If that isn't how you are calling the scripts, then how are you doing> SetLastError=true)]> > >
> > > Joe K.
> > >
> > > "Mark Duregon" <msdnonline@aspect.com.au> wrote in message
> > > news:5A1E8402-3951-4673-B370-DBB71E5C85CA@microsoft.com...
> > > > Hi,
> > > >
> > > > We have an application that requires appropriate users to run command
> > > files on an adhoc basis. We have implmented a library that uses the
> > > following code:
> > > >
> > > > using System;
> > > > using System.Runtime.InteropServices;
> > > > using System.Security.Principal;
> > > > using System.Security.Permissions;
> > > >
> > > > namespace SAMIS.Porteco.Utilities
> > > > {
> > > > public enum LogonType : int
> > > > {
> > > > LOGON32_LOGON_INTERACTIVE = 2,
> > > > LOGON32_LOGON_NETWORK = 3,
> > > > LOGON32_LOGON_BATCH = 4,
> > > > LOGON32_LOGON_SERVICE = 5,
> > > > LOGON32_LOGON_UNLOCK = 7,
> > > > LOGON32_LOGON_NETWORK_CLEARTEXT = 8, // Only for Win2K or higher
> > > > LOGON32_LOGON_NEW_CREDENTIALS = 9 // Only for Win2K or higher
> > > > };
> > > >
> > > > public enum LogonProvider : int
> > > > {
> > > > LOGON32_PROVIDER_DEFAULT = 0,
> > > > LOGON32_PROVIDER_WINNT35 = 1,
> > > > LOGON32_PROVIDER_WINNT40 = 2,
> > > > LOGON32_PROVIDER_WINNT50 = 3
> > > > };
> > > >
> > > > class SecuUtil32
> > > > {
> > > > [DllImport("advapi32.dll", SetLastError=true)]
> > > > public static extern bool LogonUser(String lpszUsername, String
> > > lpszDomain, String lpszPassword,
> > > > int dwLogonType, int dwLogonProvider, ref IntPtr TokenHandle);
> > > >
> > > > [DllImport("kernel32.dll", CharSet=CharSet.Auto)]
> > > > public extern static bool CloseHandle(IntPtr handle);
> > > >
> > > > [DllImport("advapi32.dll", CharSet=CharSet.Auto,> ExistingTokenHandle,> > > > public extern static bool DuplicateToken(IntPtr> DuplicateTokenHandle);> > > > int SECURITY_IMPERSONATION_LEVEL, ref IntPtr> password,> > > > }
> > > >
> > > > /// <summary>
> > > > /// Summary description for NetworkSecurity.
> > > > /// </summary>
> > > > public class NetworkSecurity
> > > > {
> > > > private NetworkSecurity() {}
> > > >
> > > > public static WindowsImpersonationContext ImpersonateUser(string
> > > domain, string login, string password,
> > > > LogonType logonType, LogonProvider logonProvider)
> > > > {
> > > > IntPtr tokenHandle = new IntPtr(0);
> > > > IntPtr dupeTokenHandle = new IntPtr(0);
> > > > try
> > > > {
> > > > const int SecurityImpersonation = 2;
> > > >
> > > > tokenHandle = IntPtr.Zero;
> > > > dupeTokenHandle = IntPtr.Zero;
> > > >
> > > > //
> > > > // Call LogonUser to obtain a handle to an access token.
> > > > //
> > > > bool returnValue = SecuUtil32.LogonUser(login, domain,> code> > > (int)logonType,
> > > > (int)logonProvider, ref tokenHandle);
> > > >
> > > > if (false == returnValue)
> > > > {
> > > > int ret = Marshal.GetLastWin32Error();
> > > > string strErr = String.Format("LogonUser failed with error> network> > > : {0}", ret);
> > > > throw new ApplicationException(strErr, null);
> > > > }
> > > >
> > > > bool retVal = SecuUtil32.DuplicateToken(tokenHandle,
> > > SecurityImpersonation, ref dupeTokenHandle);
> > > >
> > > > if (false == retVal)
> > > > {
> > > > SecuUtil32.CloseHandle(tokenHandle);
> > > > throw new ApplicationException("Failed to duplicate token",
> > > null);
> > > > }
> > > >
> > > > //
> > > > // The token that is passed to the following constructor must
> > > > // be a primary token in order to use it for impersonation.
> > > > //
> > > > WindowsIdentity newId = new WindowsIdentity(dupeTokenHandle);
> > > > WindowsImpersonationContext impersonatedUser =
> > > newId.Impersonate();
> > > >
> > > > return impersonatedUser;
> > > > }
> > > > catch (Exception ex)
> > > > {
> > > > throw new ApplicationException(ex.Message, ex);
> > > > }
> > > >
> > > > return null;
> > > > }
> > > > }
> > > > }
> > > >
> > > > The problem we are having is that while network resources are not
> > > restricted entirely because the batch files are able to run sql scripts
> > > against the Oracle database, FTP etc. but the user cannot access a> as it> > > share either by unc path or trying to map a drive as part of the script.
> > > This problem only occurs when trying to run the script in this fashion> also> > > works when run manually through a command prompt whic is expected, an> so> > > on a scheduled basis by the Windows Scheduler.
> > > >
> > > > Is their a permission I need to request/grant on the assembly and if> trust> > > which assembly (the library/web or both). I have tried granting full> I> > > to the assemblies without success.
> > > >
> > > > Alternatively is their a way to run a defined task from the scheduler.> get> > > read the documentation (all 2 lines of it) for the scheduler and did not> I> > > the impression that it is possible.
> > > >
> > > > Regards,
> > > > Mark.
> > > >
> > > > P.S. I cannot give you an exception or error messages that occur when> to> > > try to run the task from the web application, because as soon as I try>> > > access a network resource using the page I have created it simply
> > > hangs/timesout but works perfectly when dealing with only local file
> > > resources. FYI all command files are on the local machine but need to
> > > access network shares to ctp then delete files.
> > > >
> > > > Platform: Windows 2000 Server w/ 1.0 Framework
> > >
> > >
> > >
>
>Mark Duregon Guest
-
Mark Duregon #9
Re: Access File Share from ASP.NET using Unmanaged Code
Sorry Joe:
I read your response and looked at the documentation for CreateProcessWithTokenW which stated that Win2003 was required. I then saw the response from [MSFT] with a link to Support Article that indicated that the feature was introduced in Win2000. Since then I have been using the code in the article and trying to convert it to C#. I gave up on that because I could get it to compile but not run successfully (my result kept coming back as 0). I have now tried to create a VB.NET library using the code from that article. I think the code in that article was VB6 (I am not entirely sure my VB is far too rusty), but I have managed to get that to compile but I now get a runtime exception of a variable not being set to an instance.
To answer your question, I am using the Process class.
Again my apologies to you Joe, I must have read the wrong piece of documentation.
Does anyone happen to now of an example of this in C#?
Regards Mark.
"Joe Kaplan (MVP - ADSI)" wrote:
> Yes, but my question was how are you launching the vbscript processes? I
> understand the Win2K/1.0 part.
>
> Joe K.
>
> "Mark Duregon" <msdnonline@aspect.com.au> wrote in message
> news:47607F44-4AF2-4501-857F-5E486376C687@microsoft.com...> framework.> > Thanks, but I did mention that I am using Windows 2000 and 1.0 of the> Process> >
> > "Joe Kaplan (MVP - ADSI)" wrote:
> >> > > How are you calling the script files in this app? Are you using the> Since> > > class? In that case, you need to be aware that it will start the new
> > > process with the current process' token, not the impersonation token.> by> > > it would appear that you have a primary token, you could get around this> it?> > > calling CreateProcessWithTokenW instead.
> > >
> > > If that isn't how you are calling the scripts, then how are you doing> SetLastError=true)]> > >
> > > Joe K.
> > >
> > > "Mark Duregon" <msdnonline@aspect.com.au> wrote in message
> > > news:5A1E8402-3951-4673-B370-DBB71E5C85CA@microsoft.com...
> > > > Hi,
> > > >
> > > > We have an application that requires appropriate users to run command
> > > files on an adhoc basis. We have implmented a library that uses the
> > > following code:
> > > >
> > > > using System;
> > > > using System.Runtime.InteropServices;
> > > > using System.Security.Principal;
> > > > using System.Security.Permissions;
> > > >
> > > > namespace SAMIS.Porteco.Utilities
> > > > {
> > > > public enum LogonType : int
> > > > {
> > > > LOGON32_LOGON_INTERACTIVE = 2,
> > > > LOGON32_LOGON_NETWORK = 3,
> > > > LOGON32_LOGON_BATCH = 4,
> > > > LOGON32_LOGON_SERVICE = 5,
> > > > LOGON32_LOGON_UNLOCK = 7,
> > > > LOGON32_LOGON_NETWORK_CLEARTEXT = 8, // Only for Win2K or higher
> > > > LOGON32_LOGON_NEW_CREDENTIALS = 9 // Only for Win2K or higher
> > > > };
> > > >
> > > > public enum LogonProvider : int
> > > > {
> > > > LOGON32_PROVIDER_DEFAULT = 0,
> > > > LOGON32_PROVIDER_WINNT35 = 1,
> > > > LOGON32_PROVIDER_WINNT40 = 2,
> > > > LOGON32_PROVIDER_WINNT50 = 3
> > > > };
> > > >
> > > > class SecuUtil32
> > > > {
> > > > [DllImport("advapi32.dll", SetLastError=true)]
> > > > public static extern bool LogonUser(String lpszUsername, String
> > > lpszDomain, String lpszPassword,
> > > > int dwLogonType, int dwLogonProvider, ref IntPtr TokenHandle);
> > > >
> > > > [DllImport("kernel32.dll", CharSet=CharSet.Auto)]
> > > > public extern static bool CloseHandle(IntPtr handle);
> > > >
> > > > [DllImport("advapi32.dll", CharSet=CharSet.Auto,> ExistingTokenHandle,> > > > public extern static bool DuplicateToken(IntPtr> DuplicateTokenHandle);> > > > int SECURITY_IMPERSONATION_LEVEL, ref IntPtr> password,> > > > }
> > > >
> > > > /// <summary>
> > > > /// Summary description for NetworkSecurity.
> > > > /// </summary>
> > > > public class NetworkSecurity
> > > > {
> > > > private NetworkSecurity() {}
> > > >
> > > > public static WindowsImpersonationContext ImpersonateUser(string
> > > domain, string login, string password,
> > > > LogonType logonType, LogonProvider logonProvider)
> > > > {
> > > > IntPtr tokenHandle = new IntPtr(0);
> > > > IntPtr dupeTokenHandle = new IntPtr(0);
> > > > try
> > > > {
> > > > const int SecurityImpersonation = 2;
> > > >
> > > > tokenHandle = IntPtr.Zero;
> > > > dupeTokenHandle = IntPtr.Zero;
> > > >
> > > > //
> > > > // Call LogonUser to obtain a handle to an access token.
> > > > //
> > > > bool returnValue = SecuUtil32.LogonUser(login, domain,> code> > > (int)logonType,
> > > > (int)logonProvider, ref tokenHandle);
> > > >
> > > > if (false == returnValue)
> > > > {
> > > > int ret = Marshal.GetLastWin32Error();
> > > > string strErr = String.Format("LogonUser failed with error> network> > > : {0}", ret);
> > > > throw new ApplicationException(strErr, null);
> > > > }
> > > >
> > > > bool retVal = SecuUtil32.DuplicateToken(tokenHandle,
> > > SecurityImpersonation, ref dupeTokenHandle);
> > > >
> > > > if (false == retVal)
> > > > {
> > > > SecuUtil32.CloseHandle(tokenHandle);
> > > > throw new ApplicationException("Failed to duplicate token",
> > > null);
> > > > }
> > > >
> > > > //
> > > > // The token that is passed to the following constructor must
> > > > // be a primary token in order to use it for impersonation.
> > > > //
> > > > WindowsIdentity newId = new WindowsIdentity(dupeTokenHandle);
> > > > WindowsImpersonationContext impersonatedUser =
> > > newId.Impersonate();
> > > >
> > > > return impersonatedUser;
> > > > }
> > > > catch (Exception ex)
> > > > {
> > > > throw new ApplicationException(ex.Message, ex);
> > > > }
> > > >
> > > > return null;
> > > > }
> > > > }
> > > > }
> > > >
> > > > The problem we are having is that while network resources are not
> > > restricted entirely because the batch files are able to run sql scripts
> > > against the Oracle database, FTP etc. but the user cannot access a> as it> > > share either by unc path or trying to map a drive as part of the script.
> > > This problem only occurs when trying to run the script in this fashion> also> > > works when run manually through a command prompt whic is expected, an> so> > > on a scheduled basis by the Windows Scheduler.
> > > >
> > > > Is their a permission I need to request/grant on the assembly and if> trust> > > which assembly (the library/web or both). I have tried granting full> I> > > to the assemblies without success.
> > > >
> > > > Alternatively is their a way to run a defined task from the scheduler.> get> > > read the documentation (all 2 lines of it) for the scheduler and did not> I> > > the impression that it is possible.
> > > >
> > > > Regards,
> > > > Mark.
> > > >
> > > > P.S. I cannot give you an exception or error messages that occur when> to> > > try to run the task from the web application, because as soon as I try>> > > access a network resource using the page I have created it simply
> > > hangs/timesout but works perfectly when dealing with only local file
> > > resources. FYI all command files are on the local machine but need to
> > > access network shares to ctp then delete files.
> > > >
> > > > Platform: Windows 2000 Server w/ 1.0 Framework
> > >
> > >
> > >
>
>Mark Duregon Guest
-
Mark Duregon #10
Re: Access File Share from ASP.NET using Unmanaged Code
Sorry Joe:
I read your response and looked at the documentation for CreateProcessWithTokenW which stated that Win2003 was required. I then saw the response from [MSFT] with a link to Support Article that indicated that the feature was introduced in Win2000. Since then I have been using the code in the article and trying to convert it to C#. I gave up on that because I could get it to compile but not run successfully (my result kept coming back as 0). I have now tried to create a VB.NET library using the code from that article. I think the code in that article was VB6 (I am not entirely sure my VB is far too rusty), but I have managed to get that to compile but I now get a runtime exception of a variable not being set to an instance.
To answer your question, I am using the Process class.
Again my apologies to you Joe, I must have read the wrong piece of documentation.
Does anyone happen to now of an example of this in C#?
Regards,
Mark.
"Joe Kaplan (MVP - ADSI)" wrote:
> Yes, but my question was how are you launching the vbscript processes? I
> understand the Win2K/1.0 part.
>
> Joe K.
>
> "Mark Duregon" <msdnonline@aspect.com.au> wrote in message
> news:47607F44-4AF2-4501-857F-5E486376C687@microsoft.com...> framework.> > Thanks, but I did mention that I am using Windows 2000 and 1.0 of the> Process> >
> > "Joe Kaplan (MVP - ADSI)" wrote:
> >> > > How are you calling the script files in this app? Are you using the> Since> > > class? In that case, you need to be aware that it will start the new
> > > process with the current process' token, not the impersonation token.> by> > > it would appear that you have a primary token, you could get around this> it?> > > calling CreateProcessWithTokenW instead.
> > >
> > > If that isn't how you are calling the scripts, then how are you doing> SetLastError=true)]> > >
> > > Joe K.
> > >
> > > "Mark Duregon" <msdnonline@aspect.com.au> wrote in message
> > > news:5A1E8402-3951-4673-B370-DBB71E5C85CA@microsoft.com...
> > > > Hi,
> > > >
> > > > We have an application that requires appropriate users to run command
> > > files on an adhoc basis. We have implmented a library that uses the
> > > following code:
> > > >
> > > > using System;
> > > > using System.Runtime.InteropServices;
> > > > using System.Security.Principal;
> > > > using System.Security.Permissions;
> > > >
> > > > namespace SAMIS.Porteco.Utilities
> > > > {
> > > > public enum LogonType : int
> > > > {
> > > > LOGON32_LOGON_INTERACTIVE = 2,
> > > > LOGON32_LOGON_NETWORK = 3,
> > > > LOGON32_LOGON_BATCH = 4,
> > > > LOGON32_LOGON_SERVICE = 5,
> > > > LOGON32_LOGON_UNLOCK = 7,
> > > > LOGON32_LOGON_NETWORK_CLEARTEXT = 8, // Only for Win2K or higher
> > > > LOGON32_LOGON_NEW_CREDENTIALS = 9 // Only for Win2K or higher
> > > > };
> > > >
> > > > public enum LogonProvider : int
> > > > {
> > > > LOGON32_PROVIDER_DEFAULT = 0,
> > > > LOGON32_PROVIDER_WINNT35 = 1,
> > > > LOGON32_PROVIDER_WINNT40 = 2,
> > > > LOGON32_PROVIDER_WINNT50 = 3
> > > > };
> > > >
> > > > class SecuUtil32
> > > > {
> > > > [DllImport("advapi32.dll", SetLastError=true)]
> > > > public static extern bool LogonUser(String lpszUsername, String
> > > lpszDomain, String lpszPassword,
> > > > int dwLogonType, int dwLogonProvider, ref IntPtr TokenHandle);
> > > >
> > > > [DllImport("kernel32.dll", CharSet=CharSet.Auto)]
> > > > public extern static bool CloseHandle(IntPtr handle);
> > > >
> > > > [DllImport("advapi32.dll", CharSet=CharSet.Auto,> ExistingTokenHandle,> > > > public extern static bool DuplicateToken(IntPtr> DuplicateTokenHandle);> > > > int SECURITY_IMPERSONATION_LEVEL, ref IntPtr> password,> > > > }
> > > >
> > > > /// <summary>
> > > > /// Summary description for NetworkSecurity.
> > > > /// </summary>
> > > > public class NetworkSecurity
> > > > {
> > > > private NetworkSecurity() {}
> > > >
> > > > public static WindowsImpersonationContext ImpersonateUser(string
> > > domain, string login, string password,
> > > > LogonType logonType, LogonProvider logonProvider)
> > > > {
> > > > IntPtr tokenHandle = new IntPtr(0);
> > > > IntPtr dupeTokenHandle = new IntPtr(0);
> > > > try
> > > > {
> > > > const int SecurityImpersonation = 2;
> > > >
> > > > tokenHandle = IntPtr.Zero;
> > > > dupeTokenHandle = IntPtr.Zero;
> > > >
> > > > //
> > > > // Call LogonUser to obtain a handle to an access token.
> > > > //
> > > > bool returnValue = SecuUtil32.LogonUser(login, domain,> code> > > (int)logonType,
> > > > (int)logonProvider, ref tokenHandle);
> > > >
> > > > if (false == returnValue)
> > > > {
> > > > int ret = Marshal.GetLastWin32Error();
> > > > string strErr = String.Format("LogonUser failed with error> network> > > : {0}", ret);
> > > > throw new ApplicationException(strErr, null);
> > > > }
> > > >
> > > > bool retVal = SecuUtil32.DuplicateToken(tokenHandle,
> > > SecurityImpersonation, ref dupeTokenHandle);
> > > >
> > > > if (false == retVal)
> > > > {
> > > > SecuUtil32.CloseHandle(tokenHandle);
> > > > throw new ApplicationException("Failed to duplicate token",
> > > null);
> > > > }
> > > >
> > > > //
> > > > // The token that is passed to the following constructor must
> > > > // be a primary token in order to use it for impersonation.
> > > > //
> > > > WindowsIdentity newId = new WindowsIdentity(dupeTokenHandle);
> > > > WindowsImpersonationContext impersonatedUser =
> > > newId.Impersonate();
> > > >
> > > > return impersonatedUser;
> > > > }
> > > > catch (Exception ex)
> > > > {
> > > > throw new ApplicationException(ex.Message, ex);
> > > > }
> > > >
> > > > return null;
> > > > }
> > > > }
> > > > }
> > > >
> > > > The problem we are having is that while network resources are not
> > > restricted entirely because the batch files are able to run sql scripts
> > > against the Oracle database, FTP etc. but the user cannot access a> as it> > > share either by unc path or trying to map a drive as part of the script.
> > > This problem only occurs when trying to run the script in this fashion> also> > > works when run manually through a command prompt whic is expected, an> so> > > on a scheduled basis by the Windows Scheduler.
> > > >
> > > > Is their a permission I need to request/grant on the assembly and if> trust> > > which assembly (the library/web or both). I have tried granting full> I> > > to the assemblies without success.
> > > >
> > > > Alternatively is their a way to run a defined task from the scheduler.> get> > > read the documentation (all 2 lines of it) for the scheduler and did not> I> > > the impression that it is possible.
> > > >
> > > > Regards,
> > > > Mark.
> > > >
> > > > P.S. I cannot give you an exception or error messages that occur when> to> > > try to run the task from the web application, because as soon as I try>> > > access a network resource using the page I have created it simply
> > > hangs/timesout but works perfectly when dealing with only local file
> > > resources. FYI all command files are on the local machine but need to
> > > access network shares to ctp then delete files.
> > > >
> > > > Platform: Windows 2000 Server w/ 1.0 Framework
> > >
> > >
> > >
>
>Mark Duregon Guest
-
[MSFT] #11
Re: Access File Share from ASP.NET using Unmanaged Code
I found a sample in C# code, hope this help:
[url]http://groups.google.com/groups?hl=zh-CN&lr=&ie=UTF-8&selm=uvWXoOGWDHA.1728%[/url]
40TK2MSFTNGP11.phx.gbl&rnum=6
Luke
[MSFT] Guest
-
Joe Kaplan \(MVP - ADSI\) #12
Re: Access File Share from ASP.NET using Unmanaged Code
Also, [url]www.pinvoke.net[/url] has tons of pinvoke stuff on their wiki. It is a good
reference for this stuff (although I didn't find that one here).
Joe K.
"[MSFT]" <lukezhan@online.microsoft.com> wrote in message
news:rWakQRjaEHA.2804@cpmsftngxa06.phx.gbl...[url]http://groups.google.com/groups?hl=zh-CN&lr=&ie=UTF-8&selm=uvWXoOGWDHA.1728%[/url]> I found a sample in C# code, hope this help:
>
>> 40TK2MSFTNGP11.phx.gbl&rnum=6
>
> Luke
>
Joe Kaplan \(MVP - ADSI\) Guest
-
Joe Kaplan \(MVP - ADSI\) #13
Re: Access File Share from ASP.NET using Unmanaged Code
So, the issue with using the Process class is that it won't create the
process using the token that is being impersonated on the thread, but will
use the process token instead. I believe that explains why it wasn't
working the way you were expecting.
I'm not sure what's wrong with your code without checking it out, but you
might consider jumping over to the interop group to get some help with that
(or perhaps try a Google groups search to see if it is already asked and
answered).
The good news is that your code to create the token that you were using for
impersonation will be useful to pass into this, so once you get this
working, you should be all set.
I hope it works out.
Joe K.
"Mark Duregon" <msdnonline@aspect.com.au> wrote in message
news:40DB93B2-531D-44C8-A478-7DC79A0A8390@microsoft.com...CreateProcessWithTokenW which stated that Win2003 was required. I then saw> Sorry Joe:
>
> I read your response and looked at the documentation for
the response from [MSFT] with a link to Support Article that indicated that
the feature was introduced in Win2000. Since then I have been using the
code in the article and trying to convert it to C#. I gave up on that
because I could get it to compile but not run successfully (my result kept
coming back as 0). I have now tried to create a VB.NET library using the
code from that article. I think the code in that article was VB6 (I am not
entirely sure my VB is far too rusty), but I have managed to get that to
compile but I now get a runtime exception of a variable not being set to an
instance.documentation.>
> To answer your question, I am using the Process class.
>
> Again my apologies to you Joe, I must have read the wrong piece ofI>
> Does anyone happen to now of an example of this in C#?
>
> Regards Mark.
>
> "Joe Kaplan (MVP - ADSI)" wrote:
>> > Yes, but my question was how are you launching the vbscript processes?new> > understand the Win2K/1.0 part.
> >
> > Joe K.
> >
> > "Mark Duregon" <msdnonline@aspect.com.au> wrote in message
> > news:47607F44-4AF2-4501-857F-5E486376C687@microsoft.com...> > framework.> > > Thanks, but I did mention that I am using Windows 2000 and 1.0 of the> > Process> > >
> > > "Joe Kaplan (MVP - ADSI)" wrote:
> > >
> > > > How are you calling the script files in this app? Are you using the> > > > class? In that case, you need to be aware that it will start thetoken.> > > > process with the current process' token, not the impersonationthis> > Since> > > > it would appear that you have a primary token, you could get arounddoing> > by> > > > calling CreateProcessWithTokenW instead.
> > > >
> > > > If that isn't how you are calling the scripts, then how are youcommand> > it?> > > >
> > > > Joe K.
> > > >
> > > > "Mark Duregon" <msdnonline@aspect.com.au> wrote in message
> > > > news:5A1E8402-3951-4673-B370-DBB71E5C85CA@microsoft.com...
> > > > > Hi,
> > > > >
> > > > > We have an application that requires appropriate users to runhigher> > > > files on an adhoc basis. We have implmented a library that uses the
> > > > following code:
> > > > >
> > > > > using System;
> > > > > using System.Runtime.InteropServices;
> > > > > using System.Security.Principal;
> > > > > using System.Security.Permissions;
> > > > >
> > > > > namespace SAMIS.Porteco.Utilities
> > > > > {
> > > > > public enum LogonType : int
> > > > > {
> > > > > LOGON32_LOGON_INTERACTIVE = 2,
> > > > > LOGON32_LOGON_NETWORK = 3,
> > > > > LOGON32_LOGON_BATCH = 4,
> > > > > LOGON32_LOGON_SERVICE = 5,
> > > > > LOGON32_LOGON_UNLOCK = 7,
> > > > > LOGON32_LOGON_NETWORK_CLEARTEXT = 8, // Only for Win2K orhigher> > > > > LOGON32_LOGON_NEW_CREDENTIALS = 9 // Only for Win2K orString> > > > > };
> > > > >
> > > > > public enum LogonProvider : int
> > > > > {
> > > > > LOGON32_PROVIDER_DEFAULT = 0,
> > > > > LOGON32_PROVIDER_WINNT35 = 1,
> > > > > LOGON32_PROVIDER_WINNT40 = 2,
> > > > > LOGON32_PROVIDER_WINNT50 = 3
> > > > > };
> > > > >
> > > > > class SecuUtil32
> > > > > {
> > > > > [DllImport("advapi32.dll", SetLastError=true)]
> > > > > public static extern bool LogonUser(String lpszUsername,TokenHandle);> > > > lpszDomain, String lpszPassword,
> > > > > int dwLogonType, int dwLogonProvider, ref IntPtrImpersonateUser(string> > SetLastError=true)]> > > > >
> > > > > [DllImport("kernel32.dll", CharSet=CharSet.Auto)]
> > > > > public extern static bool CloseHandle(IntPtr handle);
> > > > >
> > > > > [DllImport("advapi32.dll", CharSet=CharSet.Auto,> > ExistingTokenHandle,> > > > > public extern static bool DuplicateToken(IntPtr> > DuplicateTokenHandle);> > > > > int SECURITY_IMPERSONATION_LEVEL, ref IntPtr> > > > > }
> > > > >
> > > > > /// <summary>
> > > > > /// Summary description for NetworkSecurity.
> > > > > /// </summary>
> > > > > public class NetworkSecurity
> > > > > {
> > > > > private NetworkSecurity() {}
> > > > >
> > > > > public static WindowsImpersonationContexterror> > password,> > > > domain, string login, string password,
> > > > > LogonType logonType, LogonProvider logonProvider)
> > > > > {
> > > > > IntPtr tokenHandle = new IntPtr(0);
> > > > > IntPtr dupeTokenHandle = new IntPtr(0);
> > > > > try
> > > > > {
> > > > > const int SecurityImpersonation = 2;
> > > > >
> > > > > tokenHandle = IntPtr.Zero;
> > > > > dupeTokenHandle = IntPtr.Zero;
> > > > >
> > > > > //
> > > > > // Call LogonUser to obtain a handle to an access token.
> > > > > //
> > > > > bool returnValue = SecuUtil32.LogonUser(login, domain,> > > > (int)logonType,
> > > > > (int)logonProvider, ref tokenHandle);
> > > > >
> > > > > if (false == returnValue)
> > > > > {
> > > > > int ret = Marshal.GetLastWin32Error();
> > > > > string strErr = String.Format("LogonUser failed withtoken",> > code> > > > : {0}", ret);
> > > > > throw new ApplicationException(strErr, null);
> > > > > }
> > > > >
> > > > > bool retVal = SecuUtil32.DuplicateToken(tokenHandle,
> > > > SecurityImpersonation, ref dupeTokenHandle);
> > > > >
> > > > > if (false == retVal)
> > > > > {
> > > > > SecuUtil32.CloseHandle(tokenHandle);
> > > > > throw new ApplicationException("Failed to duplicatemust> > > > null);
> > > > > }
> > > > >
> > > > > //
> > > > > // The token that is passed to the following constructorimpersonation.> > > > > // be a primary token in order to use it forWindowsIdentity(dupeTokenHandle);> > > > > //
> > > > > WindowsIdentity newId = newscripts> > > > > WindowsImpersonationContext impersonatedUser =
> > > > newId.Impersonate();
> > > > >
> > > > > return impersonatedUser;
> > > > > }
> > > > > catch (Exception ex)
> > > > > {
> > > > > throw new ApplicationException(ex.Message, ex);
> > > > > }
> > > > >
> > > > > return null;
> > > > > }
> > > > > }
> > > > > }
> > > > >
> > > > > The problem we are having is that while network resources are not
> > > > restricted entirely because the batch files are able to run sqlscript.> > network> > > > against the Oracle database, FTP etc. but the user cannot access a> > > > share either by unc path or trying to map a drive as part of thefashion> > > > This problem only occurs when trying to run the script in thisan> > as it> > > > works when run manually through a command prompt whic is expected,if> > also> > > > on a scheduled basis by the Windows Scheduler.
> > > > >
> > > > > Is their a permission I need to request/grant on the assembly andfull> > so> > > > which assembly (the library/web or both). I have tried grantingscheduler.> > trust> > > > to the assemblies without success.
> > > > >
> > > > > Alternatively is their a way to run a defined task from thenot> > I> > > > read the documentation (all 2 lines of it) for the scheduler and didwhen> > get> > > > the impression that it is possible.
> > > > >
> > > > > Regards,
> > > > > Mark.
> > > > >
> > > > > P.S. I cannot give you an exception or error messages that occurtry> > I> > > > try to run the task from the web application, because as soon as Ito> > to> > > > access a network resource using the page I have created it simply
> > > > hangs/timesout but works perfectly when dealing with only local file
> > > > resources. FYI all command files are on the local machine but need> >> > > > access network shares to ctp then delete files.
> > > > >
> > > > > Platform: Windows 2000 Server w/ 1.0 Framework
> > > >
> > > >
> > > >
> >
> >
Joe Kaplan \(MVP - ADSI\) Guest
-
Yan-Hong Huang[MSFT] #14
Re: Access File Share from ASP.NET using Unmanaged Code
Hello Mark,
I was reviewing the issue thread. Do you have completed the code
successfully? Luke and Joe has provided much userful resource on it. If you
have any more concerns, please feel free to post here and we will follow up.
Thanks very much.
Best regards,
Yanhong Huang
Microsoft Community Support
Get Secure! ¨C [url]www.microsoft.com/security[/url]
Register to Access MSDN Managed Newsgroups!
-http://support.microsoft.com/default.aspx?scid=/servicedesks/msdn/nospam.as
p&SD=msdn
This posting is provided "AS IS" with no warranties, and confers no rights.
Yan-Hong Huang[MSFT] Guest



Reply With Quote

