SecurityManager.IsGranted () Behavior

Can anyone explain the following C # behavior? I wrote a small console application to learn about CAS, but I cannot understand why the following lines of code work:

string[] myRoles = new string[] { "role1", "role2", "role3" };
GenericIdentity myIdentity = new GenericIdentity("myUsername", "customAuthType");
GenericPrincipal myPrincipal = new GenericPrincipal(myIdentity, myRoles);

System.Threading.Thread.CurrentPrincipal = myPrincipal;

Console.WriteLine(SecurityManager.IsGranted(new PrincipalPermission(null, "role1")));
Console.WriteLine(SecurityManager.IsGranted(new PrincipalPermission(null, "roleX")));

The "true" output for calls to SecurityManager.IsGranted ().

If I then add the following lines:

 new PrincipalPermission(null, "role1").Demand();
 new PrincipalPermission(null, "roleX").Demand();

the first call request passes, and the second (as expected) raises a SecurityException.

Why SecurityManager.IsGranted () - does not return false for permission "roleX"?

+3
source share
3 answers

From the answers to a similar question here, it seems that IsGranted () only works with CAS permissions, and not with permissions other than CAS.

Quotes from the article:

SecurityManager.IsGranted() CAS, . WorkTimePermission CAS , , , . , [ CAS]. SecurityManager.IsGranted() false [ CAS].

, CAS -CAS-, , , " " "" CAS. , , , SecurityManager.IsGranted :

" ..."

- state - CAS, . .

+1

.NET 4.0 SecurityManager.IsGranted .

, , .NET 4.0, .

bool isGranted = SecurityManager.IsGranted(new SecurityPermission(SecurityPermissionFlag.Infrastructure))

:

var permissionSet = new PermissionSet(PermissionState.None);
permissionSet.AddPermission(new SecurityPermission(SecurityPermissionFlag.UnmanagedCode));
bool isGranted = permissionSet.IsSubsetOf(AppDomain.CurrentDomain.PermissionSet);

:
http://www.stringbuilder.net/post/2009/07/31/In-NET-40-SecurityManagerIsGranted-is-obsolete.aspx

+1

, SecurityManager.IsGranted ( ..) - , .

, :

    static bool HasAccess(string role)
    {
        IPrincipal principal = System.Threading.Thread.CurrentPrincipal;
        return principal == null ? false : principal.IsInRole(role);
    }
0

All Articles