Test Group Membership of the Current User
The .NET Framework has always provided security classes
that let you retrieve basic information about the account of the current
user. The new My.User object provided
by Visual Basic 2005 makes it easier than ever to access this
information.
Note
Find out who's using your application, and the groups a mystery user belongs to.
How do I do that?
Applications often need to test who is running the application.
For example, you might want to restrict some features to certain
groups, such as Windows administrators. You can accomplish this with
the My.User object.
The My.User object provides
two key properties that return information about the current user.
These are:
IsAuthenticatedReturns
Trueif the current user account information is available in theMy.Userobject. The only reason this information wouldn't be present is if you've created a web application that allows anonymous access, or if the current Windows account isn't associated with the application domain.UsernameReturns the current username. Assuming you're using a Windows security policy, this is the Windows account name for the user, in the form
ComputerName\UserNameorDomainName\UserName.
The My.User object also
provides a single method, IsInRole( ). This
method accepts the name of a group (as a string) and then returns
True if the user belongs to that group. For example, you could use this technique to verify that the current user is a Windows administrator before performing a certain ...
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.
Read now
Unlock full access