MSTest Request for principal permission failed

393 Views Asked by At

I've looked at the very good content at this thread and it hasn't solved my problem. MSTEST PrincipalPermission

My class:

public class SecurityUsingAttributes
{
    [PrincipalPermission(SecurityAction.Demand, Role = "SomeRole")]
    public int MyMethod1()
    {
        return 5;
    }
}

My test:

[TestClass]
public class SecurityUsingAttributesTests
{
    [TestMethod]
    public void TestMethod1()
    {
        IIdentity identity = new GenericIdentity(@"MyDomain\MyName");
        string[] roles = new string[] { "SomeRole"};

        IPrincipal genericPrincipal = new GenericPrincipal(identity, roles);


        Thread.CurrentPrincipal = genericPrincipal;

        SecurityUsingAttributes target = new SecurityUsingAttributes();

        Assert.IsTrue(5 == target.MyMethod1());
    }
}

This works now.

1

There are 1 best solutions below

0
RT. On

I was missing the assignment for CurrentPrincipal. The above code works now.