Skip to Content
The .NET Developer's Guide to Windows Security
book

The .NET Developer's Guide to Windows Security

by Keith Brown
September 2004
Intermediate to advanced
408 pages
7h 25m
English
Addison-Wesley Professional
Content preview from The .NET Developer's Guide to Windows Security

Chapter 26. How to Get a Token for a User

Getting a token (Item 16) for a user is tremendously easy if you happen to be running on a Windows Server 2003 machine in a native Windows Server 2003 domain. You can simply construct a new Windows Identity, passing in the user principal name (UPN) for the account, which for ACME\Alice is typically something like .[1] Here's an example:

using System;
using System.Security.Principal;

class IsUserAnAdmin {
  static void Main(string[] args) {
    if (1 != args.Length) {
      Console.WriteLine("Usage: IsUserAnAdmin userPrincipalName");
      return;
    }
    string upn = args[0];
    // here's the magic constructor
    WindowsIdentity id = new WindowsIdentity(upn);
    WindowsPrincipal p = new WindowsPrincipal(id);
 if (p.IsInRole(WindowsBuiltInRole.Administrator)) ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

.NET Framework Security

.NET Framework Security

Brian A. LaMacchia, Sebastian Lange, Matthew Lyons, Rudi Martin, Kevin T. Price
Programming .NET Security

Programming .NET Security

Adam Freeman, Allen Jones

Publisher Resources

ISBN: 0321228359Purchase book