Login | Register   
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

ASP.NET Configuration and Group Policy, Part 2: Creating and Using Group Policy-Aware Providers  : Page 5

The second installment of this three-article series discusses how to set Windows Group Policy at both the machine and domain levels to control application configuration settings from a central location.


advertisement
A Group Policy-Aware Custom Configuration Provider
This final portion of the article describes how to make your applications Group Policy aware by integrating the techniques for creating and installing Group Policy templates and reading the Windows Registry. The downloadable sample application for this article contains a Group Policy-aware configuration provider for the Web.config file you saw in the first article in this series.

Creating a Group Policy Helper Class
You'll find that reading from the Windows Registry is a regular and repeated task when making code Group Policy aware, so it makes sense to create a helper class that abstracts the process.

The example application contains a class named GPAwareHelper that contains a static/shared method named GetGPOverride. This method takes three parameters: a Boolean value that indicates whether the target hive is HKEY_LOCAL_MACHINE or HKEY_CURRENT_USER, the key name, and the default value to return if there is no Group Policy-configured value in the Registry. The path to the key is stored as a constant within the class in this example, but could equally well be passed to the method.

The next listing shows the C# code for the GetGPOverride method (the downloadable code contains both C# and Visual Basic.NET versions). The code for the method is the same as that shown in the previous section of this article, except that it selects the appropriate registry hive (HKEY_LOCAL_MACHINE or HKEY_CURRENT_USER) depending on the value of the isHKLM parameter:

using System; using Microsoft.Win32; namespace GPUtilities { public static class GPAwareHelper { private const String REG_PATH = "Software\\Policies\\Examples\\GPConfigExample"; public static Object GetGPOverride(Boolean isHKLM, String keyName, Object configValue) { Object keyValue = null; RegistryKey demoKey = null; if (isHKLM) // open named key in HKEY_LOCAL_MACHINE section demoKey = Registry.LocalMachine.OpenSubKey(REG_PATH); else // open named key in HKEY_CURRENT_USER section demoKey = Registry.CurrentUser.OpenSubKey(REG_PATH); if (demoKey != null) { // get the specified value from this key keyValue = demoKey.GetValue(keyName); demoKey.Close(); // check that a value was found and, if not, return // the value provided in method parameters if (keyValue != null) return keyValue; else return configValue; } else { // key not found, so return value provided // in method parameters return configValue; } } ...

The GetGPOverride method returns the value of the specified key as an Object (if found in the Registry); otherwise it returns the default value passed to the method.

Converting CHECKBOX Part Values to Boolean Values
One problem when working with Group Policy is that the data types within the Registry and the settings generated by the PARTS you define in an administrative template are not directly compatible with common .NET value types such as Int32 and Boolean. In particular, the setting generated by a CHECKBOX part is either 0 (unchecked) or 1 (checked). Therefore, your code must convert this to a Boolean value if your configuration provider expects, and exposes, this setting as a Boolean type.

The helper class in the example application includes a method with the delightfully descriptive name ConvertInt32ObjectToBooleanObject that performs this conversion. It tests the underlying value type of a value passed as the single parameter, and returns an Object with an underlying Boolean type that has the appropriate value: true if the input is an Int32 type of value 1, or false if it is an Int32 type of value 0. If the value is not of underlying type Int32, the method simply returns the input Object:

... public static Object ConvertInt32ObjectToBooleanObject( Object inputValue) { // see if input value is of underlying type Int32 (int) if (inputValue.GetType().Name == "Int32") { // return Boolean equivalent return ((Int32)inputValue == 1); } else { // just return the input Object return inputValue; } }

Depending on the control types you use in your administrative templates and the way your custom configuration provider exposes settings, you may need other conversion methods that return values as the appropriate types.



Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap