Difficulty: Medium
Correct Answer: Implement declarative security and use a minimum permission request so that the assembly will not load unless it has file system and registry permissions.
Explanation:
Introduction / Context:
In .NET, Code Access Security (CAS) is used to control what managed code is allowed to do based on evidence such as where the assembly is loaded from. Assemblies distributed over an intranet may run with partial trust, which can limit access to sensitive resources like the file system and Windows registry. This question focuses on choosing the correct technique so that your assembly explicitly states the critical permissions it requires and refuses to load if the security policy does not grant those permissions.
Given Data / Assumptions:
Concept / Approach:
Declarative security in .NET allows you to apply attributes at the assembly or type level to specify security requirements. A minimum permission request (RequestMinimum) states that the assembly cannot run without the specified permissions. If the security policy does not grant those permissions, the common language runtime will refuse to load the assembly. In contrast, a demand is typically used at run time to ensure the caller has certain permissions, but it does not by itself prevent the assembly from loading. For an intranet-deployed assembly that must have file system and registry access, the appropriate approach is to declaratively request minimum permissions that match these needs.
Step-by-Step Solution:
1. Identify the exact permissions your assembly needs, such as FileIOPermission and RegistryPermission.
2. At the assembly level, apply declarative security attributes that use SecurityAction.RequestMinimum for these permissions.
3. When the assembly is loaded, the runtime compares the requested minimum permissions against the permissions granted by the current security policy.
4. If the permissions are granted, the assembly loads and can safely perform file and registry operations.
5. If the permissions are not granted, a security exception occurs at load time, preventing the assembly from running with insufficient rights.
Verification / Alternative check:
You can verify this configuration by adjusting the LocalIntranet code group permissions or by testing the assembly in a more restricted environment. If you deliberately remove either file or registry permission from the policy, the assembly should fail to load, showing that the minimum permission request is being enforced. If permissions are granted, the assembly should load and function normally when reading and writing XML files and registry keys.
Why Other Options Are Wrong:
Option A uses declarative security with a demand, but a demand is typically enforced at execution time on the call stack and does not guarantee that the assembly will fail at load time if permissions are missing. Option C uses imperative demands in code, which can add flexibility but again does not meet the requirement of failing to load when permissions are not granted, and it increases the complexity of the code. Option D is incorrect because minimum permission requests are a declarative mechanism; imperative code cannot declare minimum permissions at load time in the same way as assembly attributes.
Common Pitfalls:
Developers sometimes rely only on run time demands, leading to late failures when code paths are executed rather than clear failures at load time. Another pitfall is over-requesting permissions, which reduces security by granting more rights than the assembly truly needs. It is also easy to forget to test the assembly under partial trust to see how it behaves when permissions are restricted. Using declarative minimum permission requests encourages a least-privilege approach and provides early, predictable failure when security requirements are not satisfied.
Final Answer:
You should implement declarative security and specify a minimum permission request for the file system and Windows registry so that the assembly will not load unless these permissions are granted.
Discussion & Comments