WCF-添加身份验证层
WCF- adding authentication layer
过去几天我阅读了很多有关 WCF 中的身份验证和授权的内容,但我仍然无法决定在我的场景中什么是最佳选择:
我们公司有一个权限系统,每个需要访问新服务中任何方法的用户都需要获得该方法的特定权限。
我从活动目录中获取用户身份,然后向权限系统发送请求以检查用户是否具有该方法的特定权限。
我的问题如下:
应该在哪里实施该检查? 在每种方法中进行检查似乎都是错误的。我想在方法本身之前有一个层来通知我用户是否具有访问该方法所需的权限。
帮助将不胜感激。
如果您想要一个用于检查授权的集中入口点,您可以创建 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。
过去几天我阅读了很多有关 WCF 中的身份验证和授权的内容,但我仍然无法决定在我的场景中什么是最佳选择:
我们公司有一个权限系统,每个需要访问新服务中任何方法的用户都需要获得该方法的特定权限。
我从活动目录中获取用户身份,然后向权限系统发送请求以检查用户是否具有该方法的特定权限。
我的问题如下: 应该在哪里实施该检查? 在每种方法中进行检查似乎都是错误的。我想在方法本身之前有一个层来通知我用户是否具有访问该方法所需的权限。
帮助将不胜感激。
如果您想要一个用于检查授权的集中入口点,您可以创建 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。