Visual Basic Q&A

As a software engineer, I focus on .NET, especially asp.net, C#, WCF and so on, and I am also very interested in Search Engine Optimization.

Entries Tagged ‘System’

How to implement impersonation in an ASP.NET application

Symptoms
This article describes different ways to implement impersonation in an ASP.NET application.
Resolution
If you want to impersonate a user on a thread in ASP.NET, you can use one of the following methods, based on your requirments: Impersonate the IIS authenticated account or userImpersonate a specific user for all the requests of an ASP.NET application Impersonate the authenticating user in codeImpersonate a specific user in codeNote You can use the following code to determine what user the thread is executing as:

System.Security.Principal.WindowsIdentity.GetCurrent().Name
Impersonate the IIS Authenticated Account or User To impersonate the Microsoft Internet Information Services (IIS) authenticating user on every request for every page in an ASP.NET application, you must include an <identity> tag in the Web.config file of this application and set the impersonate attribute to true. For example:

<identity impersonate=”true” />
Impersonate a Specific User for All the Requests of an ASP.NET Application To impersonate a specific user for all the requests on all pages of an ASP.NET application, you can specify the userName and password attributes in the <identity> tag of the Web.config file for that application. For example:

<identity impersonate=”true” userName=”accountname” password=”password” /> Note The identity of the process that impersonates a specific user on a thread must have the “Act as part of the operating system” privilege. By default, the Aspnet_wp.exe process runs under a computer account named ASPNET. However, this account does not have the required privileges to impersonate a specific user. You receive an error message if you try to impersonate a specific user. This information applies only to the .NET Framework 1.0. This privilege is not required for the .NET Framework 1.1.
To work around this problem, use one of the following methods: Grant the “Act as part of the operating system” privilege to the ASPNET account (the least privileged account).
Note Although you can use this method to work around the problem, Microsoft does not recommend this method.Change the account that the Aspnet_wp.exe process runs under to the System account in the <processModel> configuration section of the Machine.config file.
Impersonate the Authenticating User in Code To impersonate the authenticating user (User.Identity) only when you run a particular section of code, you can use the code to follow. This method requires that the authenticating user identity is of type WindowsIdentity.
Visual Basic .NET

Dim impersonationContext As System.Security.Principal.WindowsImpersonationContextDim currentWindowsIdentity As System.Security.Principal.WindowsIdentitycurrentWindowsIdentity = CType(User.Identity, System.Security.Principal.WindowsIdentity)impersonationContext = currentWindowsIdentity.Impersonate()’Insert your code that runs under the security context of the authenticating user here.impersonationContext.Undo() Visual C# .NET

System.Security.Principal.WindowsImpersonationContext impersonationContext;impersonationContext =((System.Security.Principal.WindowsIdentity)User.Identity).Impersonate();//Insert your code that runs under the security context of the authenticating user here.impersonationContext.Undo(); Visual J# .NET

System.Security.Principal.WindowsImpersonationContext impersonationContext;impersonationContext =((System.Security.Principal.WindowsIdentity)get_User().get_Identity()).Impersonate();//Insert your code that runs under the security context of the authenticating user here.impersonationContext.Undo();
Impersonate a Specific User in Code To impersonate a specific user only when you run a particular section of code, use the following code:
Visual Basic .NET

<%@ Page Language=”VB” %><%@ Import Namespace = “System.Web” %><%@ Import Namespace = “System.Web.Security” %><%@ Import Namespace = “System.Security.Principal” %><%@ Import Namespace = “System.Runtime.InteropServices” %><script runat=server>Dim LOGON32_LOGON_INTERACTIVE As Integer = 2Dim LOGON32_PROVIDER_DEFAULT As Integer = 0Dim impersonationContext As WindowsImpersonationContextDeclare Function LogonUserA Lib “advapi32.dll” (ByVal lpszUsername As String, _ByVal lpszDomain As String, _ByVal lpszPassword As String, _ByVal dwLogonType As Integer, _ByVal dwLogonProvider As Integer, _ByRef phToken As IntPtr) As IntegerDeclare Auto Function DuplicateToken Lib “advapi32.dll” ( _ByVal ExistingTokenHandle As IntPtr, _ByVal ImpersonationLevel As Integer, _ByRef DuplicateTokenHandle As IntPtr) As IntegerDeclare Auto Function RevertToSelf Lib “advapi32.dll” () As LongDeclare Auto Function CloseHandle Lib “kernel32.dll” (ByVal handle As IntPtr) As LongPublic Sub Page_Load(ByVal s As Object, ByVal e As EventArgs)If impersonateValidUser(“username”, “domain”, “password”) Then’Insert your code that runs under the security context of a specific user here.undoImpersonation()Else’Your impersonation failed. Therefore, include a fail-safe mechanism here.End IfEnd SubPrivate Function impersonateValidUser(ByVal userName As String, _ByVal domain As String, ByVal password As String) As BooleanDim tempWindowsIdentity As WindowsIdentityDim token As IntPtr = IntPtr.ZeroDim tokenDuplicate As IntPtr = IntPtr.ZeroimpersonateValidUser = FalseIf RevertToSelf() ThenIf LogonUserA(userName, domain, password, LOGON32_LOGON_INTERACTIVE,LOGON32_PROVIDER_DEFAULT, token) <> 0 ThenIf DuplicateToken(token, 2, tokenDuplicate) <> 0 ThentempWindowsIdentity = New WindowsIdentity(tokenDuplicate)impersonationContext = tempWindowsIdentity.Impersonate()If Not impersonationContext Is Nothing ThenimpersonateValidUser = TrueEnd IfEnd IfEnd IfEnd IfIf Not tokenDuplicate.Equals(IntPtr.Zero) ThenCloseHandle(tokenDuplicate)End IfIf Not token.Equals(IntPtr.Zero) ThenCloseHandle(token)End IfEnd FunctionPrivate Sub undoImpersonation()impersonationContext.Undo()End Sub</script>Visual C# .NET

<%@ Page Language=”C#”%><%@ Import Namespace = “System.Web” %><%@ Import Namespace = “System.Web.Security” %><%@ Import Namespace = “System.Security.Principal” %><%@ Import Namespace = “System.Runtime.InteropServices” %><script runat=server>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 externbool CloseHandle(IntPtr handle);public void Page_Load(Object s, EventArgs e){ if(impersonateValidUser(“username”, “domain”, “password”)) { //Insert your code that runs under the security context of a specific user here. undoImpersonation(); } else { //Your impersonation failed. Therefore, include a fail-safe mechanism here. }}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; } } } } if(token!= IntPtr.Zero) CloseHandle(token); if(tokenDuplicate!=IntPtr.Zero) CloseHandle(tokenDuplicate); return false;}private void undoImpersonation(){ impersonationContext.Undo();}</script>Visual J# .NET

<%@ Page language=”VJ#” %><%@ Import Namespace=”System.Web” %><%@ Import Namespace=”System.Web.Security” %><%@ Import Namespace=”System.Security.Principal” %><%@ Import Namespace=”System.Runtime.InteropServices” %><script runat=server>public static int LOGON32_LOGON_INTERACTIVE = 2;public static int LOGON32_PROVIDER_DEFAULT = 0;WindowsImpersonationContext impersonationContext; /** @attribute DllImport(“advapi32.dll”) */ public static native int LogonUserA(String lpszUserName, String lpszDomain, String lpszPassword, int dwLogonType, int dwLogonProvider, System.IntPtr[] phToken);/** @attribute DllImport(“advapi32.dll”, CharSet=CharSet.Auto, SetLastError=true) */ public static native int DuplicateToken(System.IntPtr hToken, int impersonationLevel, System.IntPtr[] hNewToken);/** @attribute DllImport(“kernel32.dll”,CharSet=CharSet.Auto) */ public static nativeboolean CloseHandle(System.IntPtr[] handle);/** @attribute DllImport(“advapi32.dll”, CharSet=CharSet.Auto,SetLastError=true) */ public static native boolean RevertToSelf();public void Page_Load(Object s, System.EventArgs e){ if(impersonateValidUser(“username”, “domain”, ” password”)) { //Insert your code that runs under the security context of a specific user here. undoImpersonation(); } else { //Your impersonation failed. Therefore, include a fail-safe mechanism here. }}private boolean impersonateValidUser(String userName, String domain, String password){ WindowsIdentity tempWindowsIdentity; System.IntPtr[] token = new System.IntPtr[1]; System.IntPtr[] tokenDuplicate = new System.IntPtr[1]; if(RevertToSelf()) { if(LogonUserA(userName, domain, password, LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_DEFAULT, token) != 0) { if(DuplicateToken(token[0], 2, tokenDuplicate) != 0) { tempWindowsIdentity = new WindowsIdentity(tokenDuplicate[0]); impersonationContext = tempWindowsIdentity.Impersonate(); if (impersonationContext != null) { CloseHandle(tokenDuplicate); CloseHandle(token); return true; } } } } if(!token[0].Equals(System.IntPtr.Zero)) CloseHandle(token); if(!tokenDuplicate[0].Equals(System.IntPtr.Zero)) CloseHandle(tokenDuplicate); return false;}private void undoImpersonation(){ impersonationContext.Undo();}</script> Note The identity of the process that impersonates a specific user on a thread must have the “Act as part of the operating system” privilege if the Aspnet_wp.exe process is running on a Microsoft Windows 2000-based computer. The “Act as part of the operating system” privilege is not required if the Aspnet_wp.exe process is running on a Windows XP-based computer or on a Windows Server 2003-based computer. By default, the Aspnet_wp.exe process runs under a computer account named ASPNET. However, this account does not have the required privileges to impersonate a specific user. You receive an error message if you try to impersonate a specific user. .
To work around this problem, use one of the following methods: Grant the “Act as part of the operating system” privilege to the ASPNET account.
Note We do not recommend this method to work around the problem.Change the account that the Aspnet_wp.exe process runs under to the System account in the <processModel> configuration section of the Machine.config file.

How to enable the “Option Strict” and “Option Explicit” statements in Visual Basic applications

Symptoms
This article describes how to enable the Option Strict and Option Explicit statements in Microsoft Visual Basic applications for code-behind files and files that are contained in the App_Code folder.
Resolution
To enable the Option Strict and Option Explicit statements in the code-behind files and files that are contained in the App_Code folder of a Visual Basic application, override the compiler configuration in the system.codedom section of the Web.config file for the application. To do this, add the following code to the Web.config file:

<system.codedom> <compilers> <compiler language=”c#;cs;csharp” extension=”.cs” type=”Microsoft.CSharp.CSharpCodeProvider, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089″ warningLevel=”0″/> <compiler language=”vb;vbs;visualbasic;vbscript” extension=”.vb” type=”Microsoft.VisualBasic.VBCodeProvider, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089″ warningLevel=”0″ /> </compilers> </system.codedom>

Error message when you use DataReader in Visual Basic .NET: “Invalid attempt to read from column ordinal”

Symptoms
When you use DataReader to read a row, if you try to access columns in that row, you receive an error message similar to the following:

System.InvalidOperationException: Invalid attempt to read from column ordinal ‘0′.With CommandBehavior.SequentialAccess, you may only read from column ordinal ‘2′ or greater.
Resolution
This problem occurs because you executed OleDbCommand or SqlCommand with the System.Data.CommandBehavior.SequentialAccess flag set but did not access the columns sequentially.

BUG: Error message when you set the Width property of a Boolean DataGrid column to 1 in Visual Basic .NET or in Visual C# .NET: “System.ArgumentException”

Symptoms
You bind a DataGrid control to a data source that has a column with Boolean data. If you set the Width property of the DataGrid Boolean column to 1 at run time, you receive the following exception:

An unhandled exception of type ‘System.ArgumentException’ occurred in system.windows.forms.dll
Additional information: Invalid parameter used.
Resolution
To work around this bug, do not set the Width property of the DataGrid Boolean column to 1.

Best practice installation methods for Visual Studio 6.0

Symptoms
Many setup and installation problems can be avoided by using best practice installation methods. While these steps may not address specific problems or error messages, they significantly reduce the possibility of problems that can occur while installing products made by Microsoft or other software suppliers. Past success without using these methods is no guarantee of future success.These best practice methods are recommended for all installations, including service packs as well as non-Microsoft products. While these best practice methods might not be necessary in all cases, they reduce the potential for problems.
Resolution
Windows 95, Windows 98, or Windows Millennium Edition (Me) operating system:For more information on best practices for installing a product on the Windows 95, Windows 98, or Windows Me operating system, review the Readme file(s) that are included with the product. Clear the system temporary folder (C:\Windows\Temp).Stop all applications, particularly monitoring programs such as anti-virus programs. To do this, follow these steps: Press CTRL+ALT+DEL to open the Close Program dialog box.Select each application in turn and click End Task (with the exception of Explorer, which is the shell for Windows).NOTE: Windows Me has a System Restore feature, which returns the system to its pre-installation state if you establish a restore point prior to an installation. To establish a restore point, follow these steps: On the Start menu, point to Programs, point to Accessories, point to System Tools, and then click System Restore.Select Create a restore point and follow the prompts to create a restore point.If you need to return the system to its pre-installation state, follow these steps: On the Start menu, point to Programs, point to Accessories, point to System Tools, and then click System Restore.Select Restore my computer to an earlier time and follow the prompts.For additional information, click the article number below to view the article in the Microsoft Knowledge Base:
267952?(http://support.microsoft.com/kb/267952/EN-US/) Description of PCHealth in Windows Millennium Edition (Me)
Windows NT 4.0 or Windows 2000 operating system:For more information on best practices for installing a product on the Windows NT 4.0 or Windows 2000 operating system, review the Readme file(s) that are included with the product.Log on using the Local Administrator account (the default is Administrator). The domain that is selected must match the computer name.
NOTE: The Local Administrator account is not the same as a user account that has administrator rights. The Local Administrator account is the system account that is used to set up the operating system. Clear the system temporary folder.
For Windows NT 4.0, this folder is <system partition letter>:\Temp.
For Windows 2000, this folder is usually <system partition letter>:\Documents and Settings\<%userprofile%>\Local Settings\Temp, but it may be <system partition letter>:\Winnt\Profiles\<%userprofile>\Local Settings\Temp.Stop all unneeded services, particularly monitoring and anti-virus programs.
Additional StepsMost installations will be successful if you use just the best practice methods; however, on rare instances, additional steps may be required. If you encounter problems that are due to previous program installations or system drivers that are interacting with the installation process, see the Microsoft Knowledge Base articles that are listed in the “References” section.

An “An unhandled exception of type ‘System.ArgumentException’ occurred” error occurs when you set the Sorted property to True

Symptoms
When you set the Sorted property of a ListBox control to True after you bind it to a datasource, you may receive the following exception:

An unhandled exception of type ‘System.ArgumentException’ occurred in system.windows.forms.dll.
Additional information: Can’t modify the Items collection when the DataSource property is set.
Resolution
To resolve this issue, set the Sorted property before you bind the ListBox to a datasource.