使用多个程序集时,请求主体权限失败

时间:2011-05-10 12:58:28

标签: c# .net security principal

我将一个简单的应用程序拆分为三个程序集。一个是允许用户从注册表中读取密钥的客户端表单。第二个是用户登录的授权表单。第三个是.dll库,所有方法都在进行实际工作。

我按照MSDN教程执行了http://msdn.microsoft.com/en-us/library/dswfd229.aspx中执行的声明式安全检查,但仍然无法正常工作。

我像这样创建GenericPrincipal对象:

    public static void CreatePrincipal(string user)
    {
        GenericIdentity MyIdentity = new GenericIdentity(user);

        String[] MyString = { "Administrator", "User" };

        GenericPrincipal MyPrincipal =
            new GenericPrincipal(MyIdentity, MyString);

        Thread.CurrentPrincipal = MyPrincipal;
    }

它位于.dll程序集中的CustomPrincipal类中。

在同一个程序集中,我有一个RegistryOperations类,其中包含以下方法:

    [PrincipalPermissionAttribute(SecurityAction.Demand, Name = "admin1", Role = "User")]
    public static string ReadDeclarative()
    {
      ...
    }

没什么特别的。在我的“授权”程序集中,我有GUI,它需要.dll方法进行授权:

    private void btnLogin_Click(object sender, EventArgs e)
    {
        CustomPrincipal.CreatePrincipal(txtUsername.Text);
    }

最后在第三个“客户端”程序集中,我调用.dll方法来读取注册表项:

    private void btnReadRegistry_Click(object sender, EventArgs e)
    {
        txtContents.Text = RegistryOperations.ReadDeclarative();
    }

这不起作用。我通过授权程序集登录,当我尝试读取注册表时,我获得了委托人权限请求失败。 Visual Studio建议将程序集添加到一些神秘的完整信任列表中,但现在无处可寻在VS2010。请指教。

1 个答案:

答案 0 :(得分:0)

登录时,您使用的是用户名admin1吗? (如果您不打算在权限验证中检查匹配的用户名,则应将其从需求中删除。)

相关问题