【问题标题】:How to declaratively implement custom IAuthorizationPolicy in WCF?如何在 WCF 中以声明方式实现自定义 IAuthorizationPolicy?
【发布时间】:2009-08-24 14:13:34
【问题描述】:

我有一个托管在 IIS 中的 WCF 服务。我想使用我自己的 IAuthorizationPolicy,并在服务器上的 web.config 文件中对其进行配置。我有我的身份验证政策:

namespace MyLib.WCF
{
    public class CustomAuthorizationPolicy : IAuthorizationPolicy
    {
        public CustomAuthorizationPolicy()
        {
            this.Id = Guid.NewGuid().ToString();
        }

        public bool Evaluate(EvaluationContext evaluationContext, ref object state)
        {
            throw new ApplicationException("Testing custom auth");
        }
        ...
    }
}

在我的 web.config 中:

<service behaviorConfiguration="Behavior" name="MyService">
    <endpoint address="" binding="wsHttpBinding"  contract="IMyService"/>               
    <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
</service>
<serviceBehaviors>
    <behavior name="Behavior">
        <serviceAuthorization principalPermissionMode="Custom">
            <authorizationPolicies>
        <add policyType="MyLib.WCF.CustomAuthorizationPolicy, MyLib.WCF, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
            </authorizationPolicies>
        </serviceAuthorization>
    </behavior>
</serviceBehaviors>

但我的 CustomAuthorizationPolicy.Evaluate() 方法永远不会触发。我错过了什么?

【问题讨论】:

    标签: wcf


    【解决方案1】:

    嗯,显而易见的(愚蠢的)问题是:在您的&lt;service&gt; 中,您实际上引用了您的行为配置吗??

    即你有没有:

    <system.serviceModel>    
     ....
       <service name="YourService" behaviorConfiguration="Behavior">
           ....
       </service>
     ....
    </system.serviceModel>
    

    仅仅定义你所有的东西很好——但除非你真的引用了它,否则它对你没有任何好处(去过那里,我自己也做过!:-))

    第二个(几乎是愚蠢的)问题是:您使用什么绑定和安全配置?你甚至有没有打开安全性?如果你有&lt;security mode="None"&gt;,那么你的服务授权显然也不会被使用(因为根本没有凭据被传递给服务)。

    马克

    【讨论】:

    • 好问题,通常是显而易见的事情吸引我!我已将我的服务配置添加到上面的代码 sn-p 中,但要回答您的问题:1)是的,我正在引用我的行为配置,2)我正在使用 wsHttpBinding,但我没有设置安全模式 - 也许默认为无?
    • 不,实际上,wsHttp 绑定默认为使用 Windows 凭据的基于消息的安全性。这应该不是问题......
    猜你喜欢
    • 2018-06-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-15
    • 2011-04-13
    • 2021-11-02
    • 2010-10-08
    相关资源
    最近更新 更多