Log In to Another Account Programmatically

Problem

You want part of your code to execute under a different user account than that of the current user.

Solution

Log in to the new account with the unmanaged LogonUser function, and use the WindowsIdentity.Impersonate method.

Discussion

By default, your code executes under the Windows identity of the current user. That means that your code has the ability to perform any action the user can and it will be prevented if it attempts any action that’s forbidden to the user. In some cases, however, you might want to allow your code to execute with different permissions than the current user account. For example, you might create an application that runs permanently on a shared kiosk in an organization. Before ...

Get Microsoft® Visual Basic® .NET Programmer's Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.