Attention: We are retiring the ASP.NET Community Blogs. Learn more >

Configuring Code Groups with code

I have been configuring code groups with the Configuration Wizard.  Today, I finally got around to coding this up to run with our MSI.   In our code, we needed two code groups on each client workstation, one for Intranet and one of Internet access.  The below code demonstrates how simple this can be.  So, no I do not have to do this manually on all client workstations.

DimmachinePolicyLevel As PolicyLevel = Nothing

Dim ph As IEnumerator = SecurityManager.PolicyHierarchy

Do While ph.MoveNext

    Dim p1 As PolicyLevel = CType(ph.Current, PolicyLevel)

    If p1.Label = "Machine" Then

        machinePolicyLevel = p1

        Exit Do

    End If

Loop

If machinePolicyLevel Is Nothing Then Return

 

' Intranet

Dim permissionSet As PermissionSet = New NamedPermissionSet("FullTrust")

Dim membership As IMembershipCondition = New UrlMembershipCondition("http://<some ip address>/<some directory>/*")

Dim policy As PolicyStatement = New PolicyStatement(permissionSet)

Dim codeGroup As CodeGroup = New UnionCodeGroup(membership, policy)

codeGroup.Description = "FullTrust permissions for http://<some ip address>/<some directory>/"

codeGroup.Name = "AM.NET Intranet FullTrust"

' Internet

Dim permissionSet2 As PermissionSet = New NamedPermissionSet("FullTrust")

Dim membership2 As IMembershipCondition = New UrlMembershipCondition("http://<some ip address>/<some directory>/*")

Dim policy2 As PolicyStatement = New PolicyStatement(permissionSet2)

Dim codeGroup2 As CodeGroup = New UnionCodeGroup(membership2, policy2)

codeGroup2.Description = "FullTrust permissions for http://<some ip address>/<some directory>/"

codeGroup2.Name = "AM.NET Internet FullTrust"

machinePolicyLevel.RootCodeGroup.AddChild(codeGroup)

machinePolicyLevel.RootCodeGroup.AddChild(codeGroup2)

SecurityManager.SavePolicy()

No Comments