【问题标题】:WCF- adding authentication layerWCF-添加身份验证层
【发布时间】:2015-08-19 09:06:30
【问题描述】:

过去几天我阅读了很多关于 WCF 中的身份验证和授权的内容,但我仍然无法确定在我的场景中什么是最佳选择:

在我们公司,我们有一个权限系统,每个需要访问新服务中任何方法的用户都需要获得该方法的特定权限。

我从活动目录中获取用户身份,然后向权限系统发送请求以检查用户是否具有该方法的特定权限。

我的问题如下: 应该在哪里实施检查?在每种方法中都进行检查似乎是错误的。我想在方法本身之前有一个层来通知我用户是否具有所需的权限或不访问该方法。

我们将非常感谢您的帮助。

【问题讨论】:

    标签: c# wcf security authentication authorization


    【解决方案1】:

    如果您想要一个用于检查授权的集中入口点,您可以创建 ServiceAuthorizationManager 的扩展自定义实现并将其引用到您的 WCF 配置文件。

    public class MyServiceAuthorizationManager : ServiceAuthorizationManager
    {
        protected override bool CheckAccessCore(OperationContext operationContext)
        {                
            // Extract the action URI from the OperationContext. Match this against the claims
            // in the AuthorizationContext.
            string action = operationContext.RequestContext.RequestMessage.Headers.Action;
    
            // Iterate through the various claim sets in the AuthorizationContext.
            foreach(ClaimSet cs in operationContext.ServiceSecurityContext.AuthorizationContext.ClaimSets)
            {
                // Examine only those claim sets issued by System.
                if (cs.Issuer == ClaimSet.System)
                {
                    // Iterate through claims of type "http://www.contoso.com/claims/allowedoperation".
                    foreach (Claim c in cs.FindClaims("http://www.contoso.com/claims/allowedoperation", Rights.PossessProperty))
                    {
                        // If the Claim resource matches the action URI then return true to allow access.
                        if (action == c.Resource.ToString())
                            return true;
                    }
                }
            }
    
          // If this point is reached, return false to deny access.
          return false;                 
        }
    }
    

    在您的服务配置中添加引用:

    <behaviors>
      <serviceBehaviors>
        <behavior name="CalculatorServiceBehavior">
          <serviceAuthorization serviceAuthorizationManagerType="Samples.MyServiceAuthorizationManager,MyAssembly" />
         </behavior>
     </serviceBehaviors>
    

    每次用户访问您服务器中的任何服务时,都会触发上述实现。

    如果你想要额外的Authorization Policy,你也可以扩展 IAuthorizationPolicy。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-08-18
      • 1970-01-01
      • 2011-04-07
      • 2015-04-14
      • 2011-05-27
      相关资源
      最近更新 更多