MCAD/MCSD Training Guide (70-310): Developing XML Web Services and Server Components with Visual Basic(R) .NET and the .NET Framework
Configure security for a Windows service, a serviced component, a .NET Remoting object, and an XML Web service: Configure and control authorization. Authorization methods include file-based authorization and URL-based authorization.
After your application has authenticated users, you can proceed to authorize their access to resources. But there's a question to answer first: Just who is the user to whom you are granting access? It turns out that there are different answers to that question, depending on whether you implement impersonation. With impersonation, the ASP.NET process can actually take on the identity of the authenticated user .
Impersonation only applies to applications that use ASP.NET to communicate with the server. For other server applications (such as services and serviced components ), impersonation doesn't come into the picture.
Once a user has been authenticated and you've decided whether to use impersonation, you can proceed to grant access to resources. You can use the role-based authorization features of the .NET Framework for this purpose.
In this section, I'll discuss both impersonation and role-based authorization.
Implementing Impersonation
Configure security for a Windows service, a serviced component, a .NET Remoting object, and an XML Web service: Configure and implement Identity management.
ASP.NET impersonation is controlled by entries in the applicable web.config file. The default setting is no impersonation. You can also explicitly specify this setting by including this element in the file:
<identity impersonate="false"/>
With this setting, ASP.NET does not perform user impersonation. What does that mean? It means that ASP.NET will always run with its own privileges. By default, ASP.NET runs as an unprivileged account named ASPNET . You can change this by making a setting in the processModel section of the machine.config file. This setting can only be changed in machine.config , so any change automatically applies to every site on the server (and no site can have a setting different from that of the server). To use a high-privilege system account instead of a low-privilege account, set the userName attribute of the processModel element to SYSTEM .
WARNING
SYSTEM Is Dangerous Don't run ASP.NET under the SYSTEM account unless you absolutely need to. Because this is a high-privilege account, it opens your server to disastrous changes should a security hole in ASP.NET ever be discovered and exploited.
So when impersonation is disabled, all requests will run in the context of the account running ASP.NETeither the ASPNET account or the system account. This is true whether you're using anonymous access or authenticating users in some fashion.
The second possible setting is to turn on impersonation:
<identity impersonate="true"/>
In this case, ASP.NET takes on the identity passed to it by IIS. If you're allowing anonymous access in IIS, this means that ASP.NET will impersonate the IUSR_ ComputerName account that IIS itself uses. If you're not allowing anonymous access, ASP.NET will take on the credentials of the authenticated user and make requests for resources as if it were that user.
Finally, you can specify a particular identity to use for all authenticated requests:
<identity impersonate="true" name="DOMAIN\username" password="password"/>
With this setting, all requests are made as the specified user ( assuming that the password is correct in the configuration file).
Step by Step 11.6 will give you some practice in configuring impersonation.
STEP BY STEP
11.6 Using Impersonation
|
Identity and Principal Objects
Within a Visual Basic .NET application, authorization is handled by the role-based security system. Role-based security revolves around two interfaces: IIdentity and IPrincipal. For applications that use Windows accounts in role-based security, these interfaces are implemented by the WindowsIdentity and WindowsPrincipal objects, respectively.
NOTE
Custom Authorization If for some reason you want to develop a custom authorization scheme, you can implement IIdentity and IPrincipal in your own classes. In use, these classes will function very much like the Windows-based classes that I'll demonstrate in this chapter.
The WindowsIdentity object represents the Windows user who is running the current code. The properties of this object allow you to retrieve such information as the username and authentication method.
The WindowsPrincipal object adds functionality to the WindowsIdentity object. The WindowsPrincipal object represents the entire security context of the user who is running the current code, including any roles to which the user belongs. When the Common Language Runtime decides which role-based permissions to assign to your code, it inspects the WindowsPrincipal object.
Step by Step 11.7 demonstrates the use of the WindowsIdentity and WindowsPrincipal objects.
STEP BY STEP
11.7 WindowsIdentity and WindowsPrincipal
|
This code first tells the Common Language Runtime that you're using the standard Windows authentication method by calling the SetPrincipalPolicy method of the current AppDomain. It then retrieves the WindowsIdentity of the current user through the static GetCurrent method of the WindowsIdentity object. After displaying some of the properties of the WindowsIdentity object, it gets the corresponding WindowsPrincipal object by passing the WindowsIdentity object to the constructor of the WindowsPrincipal class.
WARNING
Modify the Domain Name This code contains a reference to a specific domain named LARKGROUP in its call to the IsInRole method. You should change that to the name of your own domain to test this code.
Note that the properties of the WindowsIdentity object are somewhat richer than those of the WindowsPrincipal object, but that the WindowsPrincipal object lets you evaluate role membership for the current user. If you only want to work with the WindowsPrincipal object, you can also retrieve it from the Thread.CurrentPrincipal static method.
Verifying Role Membership
One way to manage role-based security is to use the IsInRole method of the WindowsPrincipal object to determine whether the current user is in a specific Windows group (see Step By Step 11.8). The results of this method call can be used to modify your application's user interface or to perform other tasks .
STEP BY STEP
11.8 Verifying Role Membership
|
Three overloaded forms of the IsInRole method are available:
-
IsInRole(WindowsBuiltInRole) Uses one of the WindowsBuiltInRole constants to check for membership in the standard Windows groups.
-
IsInRole(String) Checks for membership in a group with the specified name.
-
IsInRole(Integer) Checks for membership in a group with the specified Role Identifier (RID). RIDs are assigned by the operating system and provide a language-independent way to identify groups.
Using the PrincipalPermission Class
An alternative way to manage identities is to perform imperative or declarative security checking with role-based security by using the PrincipalPermission class, as in Step By Step 11.9, or the PrincipalPermissionAttribute attribute.
STEP BY STEP
11.9 Using the PrincipalPermission Class
|
Checking permissions with role-based security is similar to checking permissions with code access security. The difference lies in what you are checking. The constructor for the PrincipalPermission class accepts both a name and a group name, so you can also use it to check whether a specific user is running the code.
REVIEW BREAK
|
Top |