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

发布于 2024-11-06 01:12:11 字数 1494 浏览 3 评论 0原文

我有一个简单的应用程序分为三个程序集。一种是客户端表单,允许用户从注册表中读取密钥。第二个是用户登录的授权表单。第三个是 .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中。请指教。

I have a simple application split into three assemblies. One is a client form that allows user to read a key from the registry. Second is an authorization form through which the user logs in. The third is a .dll library with all the methods doing the actual work.

I followed MSDN tutorial on Performing Declarative Security Checks found here http://msdn.microsoft.com/en-us/library/dswfd229.aspx but something still doesn' work.

I create GenericPrincipal object like this:

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

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

        GenericPrincipal MyPrincipal =
            new GenericPrincipal(MyIdentity, MyString);

        Thread.CurrentPrincipal = MyPrincipal;
    }

This is located in the CustomPrincipal class in the .dll assembly.

In the same assembly I have a RegistryOperations class with the following method:

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

Nothing fancy. In my "Authorization" assembly I have the GUI which calls for .dll method for authorization:

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

Finally in the third, "Client" assembly I call for the .dll method to read registry keys:

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

This doesn't work. I login in through the Authorization assembly and when I try to read the registry I get the Request for principal permission failed. Visual Studio suggests adding the assemblies to some mystical Full Trust list but that is nowehere to be found in VS2010. Please advise.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夜巴黎 2024-11-13 01:12:11

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

When you log in, are you using the user name admin1? (If you weren't intending to check for a matching user name in the permission verification, you should remove it from the demand.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文