1

I found this code:

AspNetHostingPermissionLevel GetCurrentTrustLevel() {
    foreach (AspNetHostingPermissionLevel trustLevel in
            new AspNetHostingPermissionLevel [] {
                AspNetHostingPermissionLevel.Unrestricted,
                AspNetHostingPermissionLevel.High,
                AspNetHostingPermissionLevel.Medium,
                AspNetHostingPermissionLevel.Low,
                AspNetHostingPermissionLevel.Minimal 
            } )
    {
        try {
            new AspNetHostingPermission(trustLevel).Demand();
        }
        catch (System.Security.SecurityException ) {
            continue;
        }

        return trustLevel;
    }

    return AspNetHostingPermissionLevel.None;
}

from Get current ASP.NET Trust Level programmatically, but this is C#, and I would like to have it for VB.NET. Any chance of someone expert in both VB.NET and C# that can translate this to VB code?

I tried myself and got the following VB.NET code but it generates an error inside my VWD:

Private Function GetCurrentTrustLevel() As AspNetHostingPermissionLevel

    For Each trustLevel As AspNetHostingPermissionLevel In New _
        AspNetHostingPermissionLevel() { _
            AspNetHostingPermissionLevel.Unrestricted, _
            AspNetHostingPermissionLevel.High, _
            AspNetHostingPermissionLevel.Medium, _
            AspNetHostingPermissionLevel.Low, _
            AspNetHostingPermissionLevel.Minimal _
        }

            Try
                New AspNetHostingPermission(trustLevel).Demand()
            Catch generatedExceptionName As System.Security.SecurityException
                Continue Try
            End Try

            Return trustLevel
        Next

        Return AspNetHostingPermissionLevel.None
    End Function

These parts seems to be wrong:

New AspNetHostingPermission(trustLevel).Demand()

and

Continue Try

Obviously, need to be handled by someone fluent in both C# and VB.NET and can spot the errors in VB.NET

Thanks

Lars

Machavity
  • 30,841
  • 27
  • 92
  • 100
lars
  • 17
  • 1
  • 9
  • 1
    Try to load it up in Reflector, and choose to display it in VB.Net. Usually works. http://reflector.red-gate.com/download.aspx?TreatAsUpdate=1 – Mikael Svenson Jan 23 '11 at 18:34
  • Thanks for the tip! I will look into that next time I come across a similar C# vs VB.NET problem. Lars – lars Jan 23 '11 at 19:42

1 Answers1

1

The following should work

Private Function GetCurrentTrustLevel() As AspNetHostingPermissionLevel

    For Each trustLevel As AspNetHostingPermissionLevel In New _
        AspNetHostingPermissionLevel() { _
            AspNetHostingPermissionLevel.Unrestricted, _
            AspNetHostingPermissionLevel.High, _
            AspNetHostingPermissionLevel.Medium, _
            AspNetHostingPermissionLevel.Low, _
            AspNetHostingPermissionLevel.Minimal _
        }

            Try
                Dim HostedPermission As AspNetHostingPermission = _
                    New AspNetHostingPermission(trustLevel)
                HostedPermission.Demand()
            Catch generatedExceptionName As System.Security.SecurityException
                Continue For
            End Try

            Return trustLevel
        Next

        Return AspNetHostingPermissionLevel.None
    End Function

I am no VB.Net expert, but the following is what I did to get it to work:

  1. Continue For rather than Continue Try
  2. Needed to declare a variable of type AspNetHostingPermission and initialize it, then call the Demand method.
Owen Blacker
  • 4,117
  • 2
  • 33
  • 70
Waleed Al-Balooshi
  • 6,318
  • 23
  • 21