开发者

How to limit the service access in WCF to a set of windows accounts?

开发者 https://www.devze.com 2022-12-11 17:16 出处:网络
I have a WCF service which uses netTcp binding and transport security. It uses Windows authentication (default).

I have a WCF service which uses netTcp binding and transport security. It uses Windows authentication (default).

Everything is 开发者_高级运维working smoothly but I want to ensure that only a select windows user account can use this service instead of everyone in the domain.

Is there any way to accomplish this such that select user accounts can be written in config file and service will use them (rules out using attributes)?


You cannot do this in config - but since you're using Windows authentication, you could easily use the ASP.NET role providers - either based on Active Directory / Windows domain role membership, or based on the ASP.NET build-in role/membership database.

With this, you could then use declarative syntax to limit callers to certain groups:

[ServiceContract]
interface IMyService
{
   [OperationContract]
   [PrincipalPermission(SecurityAction.Demand, Role="YourCustomRole")]
   public string MethodLimitedToGroup(string someInput);
}

Anyone who is not member of that group you specified, and tries to call this method, will receive a SecurityException - but nothing else.

You can also limit to a specific set of actual user names - not recommended, though - too complicated, too restrictive, in general:

[ServiceContract]
interface IMyService
{
   [OperationContract]
   [PrincipalPermission(SecurityAction.Demand, Name="User1")]
   [PrincipalPermission(SecurityAction.Demand, Name="User2")]
   public string MethodLimitedToGroup(string someInput);
}

You can define all of this in config:

<behaviors>
  <serviceBehavior>
     <behavior name="WinAuth">
        <serviceAuthorization principalPermissionMode="Windows" />
     </behavior>
  </serviceBehavior>
</behaviors>

and then simply assign that service behavior to your service in your config:

<service name="YourService" behaviorConfiguration="WinAuth"> ......

If you want to use the ASP.NET supplied membership/role database, specify

        <serviceAuthorization principalPermissionMode="UseAspNetRoles" />

instead.

Marc

0

精彩评论

暂无评论...
验证码 换一张
取 消