2

次のことを可能にする正しいWCFセキュリティの実装/構成は何ですか。

  • 既存のWindowsアカウントを使用してサービスで認証する
  • 資格情報を提供せずに、別のプロジェクトからのサービス参照の追加を許可する
  • サービスを呼び出すことができるユーザーを制限する
4

1 に答える 1

2

既存のWindowsアカウントを使用してサービスで認証する

これを行うtransport clientCredentialTypeには、バインディング構成の属性をに設定する必要がありますWindows

<bindings>
   <wsHttpBinding>
      <binding>
         <security mode="Message">
            <transport clientCredentialType="Windows" />
         </security>
      </binding>
   </wsHttpBinding>
</bindings>

資格情報を提供せずに、別のプロジェクトからのサービス参照の追加を許可する

これを行うにはmex、サービスエンドポイントのエンドポイントを作成します。

<services>
   <service name="Services.SampleService" behaviorConfiguration="wsDefaultBehavior">
      <endpoint address="mex"  binding="mexHttpBinding" contract="IMetadataExchange" />
   </service>
</services>

サービスを呼び出すことができるユーザーを制限する

これはもう少し複雑です。ユーザーごとにサービスを保護するために私が見つけた方法には、カスタム承認ポリシーが必要です。承認を実行するクラスは、IAuthorizationPolicyインターフェースを実装する必要があります。これは私の承認クラスの完全なコードです:

namespace Services.SampleService.Authorization
{
    /// <summary>
    /// Handles the default authorization for access to the service
    /// <para>Works in conjunction with the AuthorizedUsersDefault setting</para>
    /// </summary>
    public class DefaultAuthorization: IAuthorizationPolicy
    {

        string _Id;

        public DefaultAuthorization()
        {
            this._Id = Guid.NewGuid().ToString();
        }

        public bool Evaluate(EvaluationContext evaluationContext, ref object state)
        {
            bool isAuthorized = false;
            try
            {
                //get the identity of the authenticated user
                IIdentity userIdentity = ((IIdentity)((System.Collections.Generic.List<System.Security.Principal.IIdentity>)evaluationContext.Properties["Identities"])[0]);
                //verify that the user is authorized to access the service
                isAuthorized = Properties.Settings.Default.AuthorizedUsersDefault.Contains(userIdentity.Name, StringComparison.OrdinalIgnoreCase);
                if (isAuthorized)
                {
                    //add the authorized identity to the current context
                    GenericPrincipal principal = new GenericPrincipal(userIdentity, null);
                    evaluationContext.Properties["Principal"] = principal;
                }
            }
            catch (Exception e)
            {
                Logging.Log(Severity.Error, "There was an error authorizing a user", e);
                isAuthorized = false;
            }
            return isAuthorized;
        }

        public ClaimSet Issuer
        {
            get { return ClaimSet.System; }
        }

        public string Id
        {
            get { return this._Id; }
        }
    }
}

「魔法」はEvaluateメソッドで発生します。私の場合、許可されたユーザーのリストは、という名前のProperties.Settings変数(タイプArrayOfString)に保持されますAuthorizedUsersDefault。このようにして、プロジェクト全体を再デプロイすることなく、ユーザーリストを維持できます。

次に、この承認ポリシーをサービスごとに使用するには、ServiceBehaviorsノードで次のように設定します。

<behaviors>
   <serviceBehaviors>
      <behavior name="wsDefaultBehavior">
         <serviceAuthorization principalPermissionMode="Custom">
        <authorizationPolicies>
           <add policyType="Services.SampleService.Authorization.DefaultAuthorization, MyAssemblyName" />
        </authorizationPolicies>
     </serviceAuthorization>
      </behavior>
   </serviceBehaviors>
</behaviors>
于 2013-03-25T17:17:50.163 に答える