Home
Manage Your Code
Snippet: SetSecurity Installer class (C#)
Title: SetSecurity Installer class Language: C#
Description: SetSecurity Installer class. this is to grant full trust to the installed assembly Views: 820
Author: Abison Mathew Jose Date Added: 11/4/2008
Copy Code  
1//-----------------------------------------------------------------------
2// 
3//  Copyright (C) Microsoft Corporation.  All rights reserved.
4// 
5// THIS CODE AND INFORMATION ARE PROVIDED AS IS WITHOUT WARRANTY OF ANY
6// KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
7// IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
8// PARTICULAR PURPOSE.
9//-----------------------------------------------------------------------
10
11using System;
12using System.Collections.Generic;
13using System.ComponentModel;
14using System.Configuration.Install;
15using System.Diagnostics;
16using System.IO;
17
18namespace CustomActions
19{
20    [RunInstaller(true)]
21    [System.Security.Permissions.PermissionSetAttribute(System.Security.Permissions.SecurityAction.Demand, Name = "FullTrust")]
22    public sealed partial class SetSecurity : Installer
23    {
24        public SetSecurity()
25        {
26            InitializeComponent();
27        }
28
29        public override void Install(System.Collections.IDictionary stateSaver)
30        {
31            // Call the base implementation.
32            base.Install(stateSaver);
33
34            string allUsersString = this.Context.Parameters["allUsers"];
35            string solutionCodeGroupName = this.Context.Parameters["solutionCodeGroupName"];
36            string solutionCodeGroupDescription = this.Context.Parameters["solutionCodeGroupDescription"];
37            string targetDir = this.Context.Parameters["targetDir"];
38            string assemblyName = this.Context.Parameters["assemblyName"];
39            string assemblyCodeGroupName = this.Context.Parameters["assemblyCodeGroupName"];
40            string assemblyCodeGroupDescription = this.Context.Parameters["assemblyCodeGroupDescription"];
41
42            // Note that a code group with solutionCodeGroupName name is created in the 
43            // Install method and removed in the Rollback and Uninstall methods.
44            // The solutionCodeGroupName must be a unique name to ensure that the 
45            // correct code group is removed during Rollback and Uninstall.
46
47            if (String.IsNullOrEmpty(solutionCodeGroupName))
48                throw new InstallException("Cannot set the security policy. The specified solution code group name is not valid.");
49            if (String.IsNullOrEmpty(solutionCodeGroupDescription))
50                throw new InstallException("Cannot set the security policy. The specified solution code group description is not valid.");
51            if (String.IsNullOrEmpty(targetDir))
52                throw new InstallException("Cannot set the security policy. The specified target directory is not valid.");
53            if (String.IsNullOrEmpty(assemblyName))
54                throw new InstallException("Cannot set the security policy. The specified assembly name is not valid.");
55            if (String.IsNullOrEmpty(assemblyCodeGroupName))
56                throw new InstallException("Cannot set the security policy. The specified assembly code group name is not valid.");
57            if (String.IsNullOrEmpty(assemblyCodeGroupDescription))
58                throw new InstallException("Cannot set the security policy. The specified assembly code group description is not valid.");
59            if (stateSaver == null)
60                throw new ArgumentNullException("stateSaver");
61
62            try
63            {
64                bool allUsers = String.Equals(allUsersString, "1");
65                string assemblyPath = Path.Combine(targetDir, assemblyName);
66
67                // Note that Install method may be invoked during Repair mode and the code group 
68                // may already exist.
69                // To prevent adding of another code group, remove the code group if it exists.
70                try
71                {
72                    // The solutionCodeGroupName must be a unique name; otherwise, the method might delete wrong code group.
73                    CaspolSecurityPolicyCreator.RemoveSecurityPolicy(allUsers, solutionCodeGroupName);
74                }
75                catch {}
76
77                CaspolSecurityPolicyCreator.AddSecurityPolicy(
78                    allUsers,
79                    solutionCodeGroupName,
80                    solutionCodeGroupDescription,
81                    assemblyPath,
82                    assemblyCodeGroupName,
83                    assemblyCodeGroupDescription);
84                stateSaver.Add("allUsers", allUsers);
85
86            }
87            catch (Exception ex)
88            {
89                throw new InstallException("Cannot set the security policy.", ex);
90            }
91        }
92
93        public override void Rollback(System.Collections.IDictionary savedState)
94        {
95            // Call the base implementation.
96            base.Rollback(savedState);
97
98            // Check whether the "allUsers" property is saved.
99            // If it is not set, the Install method did not set the security policy.
100            if ((savedState == null) || (savedState["allUsers"] == null))
101                return;
102
103            // The solutionCodeGroupName must be a unique name; otherwise, the method might delete wrong code group.
104            string solutionCodeGroupName = this.Context.Parameters["solutionCodeGroupName"];
105            if (String.IsNullOrEmpty(solutionCodeGroupName))
106                throw new InstallException("Cannot remove the security policy. The specified solution code group name is not valid.");
107
108            try
109            {
110                bool allUsers = (bool) savedState["allUsers"];
111                CaspolSecurityPolicyCreator.RemoveSecurityPolicy(allUsers, solutionCodeGroupName);
112            }
113            catch (Exception ex)
114            {
115                throw new InstallException("Cannot remove the security policy.", ex);
116            }
117        }
118
119
120        public override void Uninstall(System.Collections.IDictionary savedState)
121        {
122            // Call the base implementation.
123            base.Uninstall(savedState);
124
125            // Check whether the "allUsers" property is saved.
126            // If it is not set, the Install method did not set the security policy.
127            if ((savedState == null) || (savedState["allUsers"] == null))
128                return;
129
130            // The solutionCodeGroupName must be a unique name; otherwise, the method might delete wrong code group.
131            string solutionCodeGroupName = this.Context.Parameters["solutionCodeGroupName"];
132            if (String.IsNullOrEmpty(solutionCodeGroupName))
133                throw new InstallException("Cannot remove the security policy. The specified solution code group name is not valid.");
134
135            try
136            {
137                bool allUsers = (bool)savedState["allUsers"];
138                CaspolSecurityPolicyCreator.RemoveSecurityPolicy(allUsers, solutionCodeGroupName);
139            }
140            catch (Exception ex)
141            {
142                // Note that throwing an exception might stop the uninstall process.
143                // To inform the user and stop the uninstall process, throw an exception.
144                // To continue the uninstall, do not throw the exception.
145                throw new InstallException("Cannot remove the security policy.", ex);
146            }
147        }
148    }
149}