我在冒充用户时遇到了麻烦。我有一个声明如下的方法:

[PrincipalPermission(SecurityAction.Demand, Name=@"DJPITER-PC\Test", Role="LocalTestGroup")]
static void LocalTestGroupOnly()
{
    Console.WriteLine("Inside LocalTestGroupOnly() - {0}", 
        WindowsIdentity.GetCurrent().Name);
}

主叫代码是:

WindowsImpersonationContext context = 
        WindowsIdentity.Impersonate(token);

    Console.WriteLine("Calling LocalTestGroupOnly() as {0}", 
        WindowsIdentity.GetCurrent().Name);
    LocalTestGroupOnly();

    context.Undo();

    try
    {
        // Reverted user is displayed properly 
        Console.WriteLine("Calling LocalTestGroupOnly() as {0}", 
            WindowsIdentity.GetCurrent().Name);

        // This method should fail but if succeeds
        LocalTestGroupOnly();
    }
    catch (SecurityException ex)
    {
        Console.WriteLine("Your account lacks permission to that function.");
    }

默认用户不是LocalTestGroup的成员。 用户由LocalTestGroup的令牌IS成员指示。

问题:

对LocalTestGroupOnly()的第一次调用成功,因为用户由LocalTestGroup的标记IS成员指示。 LocalTestGroupOnly()的第二次调用(作为默认用户)应该失败,因为默认用户不是“Test”且它不属于LocalTestGroup。问题是这种方法也成功了。

如果我单独运行程序 - 无论是否有模拟,我们都会纠正这种行为:当模仿为“测试”时它会成功,而在调用默认用户时则会失败。

这里有什么问题?

有帮助吗?

解决方案

你能检查 Thread.CurrentPrincipal.Identity 而不是 WindowsIdentity.GetCurrent()吗? PrincipalPermission.Demand()使用第一个。

要更改 Thread.CurrentPrincipal (或 HttpContext.User ),您似乎必须在模拟后或撤消后明确设置它们。查看此处以查找类似问题

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top