简体   繁体   English

如何使用PrincipalPermissionAttribute类

[英]how to use PrincipalPermissionAttribute class

I have a webservice exposing certain methods. 我有一个公开某些方法的网络服务。 I want to restrict access of some of those methods to a particular group of users. 我想将某些方法的访问权限限制为特定的用户组。 On searching over the internet, I found that PrincipalPermissionAttribute class is what I need, but i did not get good enough example. 在互联网上搜索时,我发现PrincipalPermissionAttribute类是我所需要的,但是我没有得到足够好的示例。 Can anyone explain how this works? 谁能解释这是如何工作的?

This is how my web.config file looks like: 这是我的web.config文件的样子:

<System.Web>
 <authentication mode="windows">
 <authorization>
   <allow user="domain\group" />
 </authorization>
</System.Web>

Now the webservice method (C# Rest Webservice) that I want to restrict access is: 现在,我想限制访问的webservice方法(C#Rest Webservice)是:

public void DeleteAction(string resourceId)
{
}

I have done two things: 我做了两件事:

1) 1)

[PrincipalPermission(SecurityAction.Demand, role="domain\\group")]
   public void DeleteAction(string resourceId)
   {
      // delete action
   }

I get the following error message: "request for principal permission failed" and the second thing is how can I make the value of role to be dynamic ie selecting it from Web.config file rather than hardcoding it. 我收到以下错误消息:“请求主体权限失败”,第二件事是如何使角色的值具有动态性,即从Web.config文件中选择它而不是对其进行硬编码。

2) 2)

public void DeleteAction(string resourceId)
   {
     WindowsIdentity wi = new WindowsIdentity.GetCurrent();
     WindowsPrincipal wp = new WindowsPrincipal(wi);

     if(wp.IsInRole("domain\\group"))
     {
       // delete action
     }
   }

In this approach, it works fine when I use a console program to host the webservice. 通过这种方法,当我使用控制台程序托管Web服务时,它可以很好地工作。 When this service is hosted in IIS, I am not able to get this working. 当此服务托管在IIS中时,我无法正常工作。 The user is never identified ie wp.IsInRole("domain\\group") is returning false. 永远不会识别用户,即wp.IsInRole(“ domain \\ group”)返回false。

I know, I am missing something here. 我知道,我在这里错过了一些东西。 Any help is appreciated. 任何帮助表示赞赏。

Thanks, 谢谢,

It sounds like a user authentication issue. 听起来像是用户身份验证问题。 If you do not know who is the user you won't be able to know the permissions using the WindowsPrincipal. 如果您不知道谁是用户,则将无法使用WindowsPrincipal来了解权限。

Please make sure that your website is not using Asp.Net impersonation and that the forms authentication is enabled. 请确保您的网站没有使用Asp.Net模拟,并且已启用表单身份验证。

IIS -> WebSite -> Authentication IIS->网站->身份验证

Take a look to the IIS Authentication 看一下IIS身份验证

Regards, 问候,

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM