2

WCF では、クライアント側でユーザーが認証され、ユーザーの役割/権限がクライアント側のプリンシパル/アイデンティティ オブジェクトに格納されます。認証されると、ユーザーは特定のロールにいる場合にのみサービス メソッドを呼び出すことができるようになります。そのためには、クライアント側のプリンシパル/アイデンティティ オブジェクトをサービス側に送信する必要があります。しかし、サービス側に到達すると、プリンシパル オブジェクトは Windows プリンシパルであり、ID は Windows ID です。これでは、クライアント側の資格情報に基づいてサービス メソッドを呼び出す必要があるかどうかを確認できません。

プリンシパルと ID オブジェクトをクライアント側からサーバー側に転送することはできますか? プリンシパル オブジェクト (ジェネリック プリンシパル) をサーバー側に送信したいと考えています。出来ますか?助けてください。

以前、次のような同様の質問を投稿しました。

クライアント側でカスタマイズされたプリンシパル オブジェクトを WCF サービス側に引き継ぐ

答えをたどろうとしましたが、主要な目的を引き継ぐことができませんでした。

詳細はこちら。

クライアント側では、プリンシパル オブジェクトと ID オブジェクトは、デバッグ中にイミディエイト ウィンドウで次のように表示されます。

System.Threading.Thread.CurrentPrincipal {System.Security.Principal.GenericPrincipal} [System.Security.Principal.GenericPrincipal]: {System.Security.Principal.GenericPrincipal} ID: {System.Security.Principal.GenericIdentity} System.Threading. Thread.CurrentPrincipal.Identity {System.Security.Principal.GenericIdentity} [System.Security.Principal.GenericIdentity]: {System.Security.Principal.GenericIdentity} AuthenticationType: "" IsAuthenticated: false 名前: ""

サーバー側では、プリンシパル オブジェクトと ID は次のようになります。

System.Threading.Thread.CurrentPrincipal {System.Security.Principal.WindowsPrincipal} [System.Security.Principal.WindowsPrincipal]: {System.Security.Principal.WindowsPrincipal} ID: {System.Security.Principal.WindowsIdentity} {System.Security .Principal.WindowsIdentity} [System.Security.Principal.WindowsIdentity]: {System.Security.Principal.WindowsIdentity} AuthenticationType: "NTLM" IsAuthenticated: true Name: "MyDomain\MyLoginID"

私のWCFクライアントは次のようになります

クライアントコード:

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            ServiceReference1.Service1Client client = new Service1Client("NetTcpBinding_IService1");

            Console.WriteLine(client.GetData(6548));


            Console.ReadLine();
        }
    }
}

クライアント構成は次のようになります。

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <system.serviceModel>
        <bindings>
            <netTcpBinding>
                <binding name="NetTcpBinding_IService1" closeTimeout="10:10:00"
                    openTimeout="10:10:00" receiveTimeout="10:10:00" sendTimeout="10:10:00"
                    transactionFlow="false" transferMode="Buffered" transactionProtocol="OleTransactions"
                    hostNameComparisonMode="StrongWildcard" listenBacklog="10"
                    maxBufferPoolSize="524288" maxBufferSize="65536" maxConnections="10"
                    maxReceivedMessageSize="65536">
                    <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
                        maxBytesPerRead="4096" maxNameTableCharCount="16384" />
                    <reliableSession ordered="true" inactivityTimeout="10:10:00"
                        enabled="false" />
                    <security mode="Transport">
                        <transport clientCredentialType="Windows" protectionLevel="EncryptAndSign" />
                        <message clientCredentialType="Windows" />
                    </security>
                </binding>
            </netTcpBinding>
        </bindings>
        <client>
            <endpoint address="net.tcp://localhost:8888/Service1" binding="netTcpBinding"
                bindingConfiguration="NetTcpBinding_IService1" contract="ServiceReference1.IService1"
                name="NetTcpBinding_IService1">

            </endpoint>
        </client>
    </system.serviceModel>
</configuration>

サービス コードは次のようになります。

[ServiceContract]
public interface IService1
{
    [OperationContract]
    string GetData(int value);

    [OperationContract]
    CompositeType GetDataUsingDataContract(CompositeType composite);

    // TODO: Add your service operations here
}

// Use a data contract as illustrated in the sample below to add composite types to service operations
[DataContract]
public class CompositeType
{
    bool boolValue = true;
    string stringValue = "Hello ";

    [DataMember]
    public bool BoolValue
    {
        get { return boolValue; }
        set { boolValue = value; }
    }

    [DataMember]
    public string StringValue
    {
        get { return stringValue; }
        set { stringValue = value; }
    }
}


public class Service1 : IService1
{
    public string GetData(int value)
    {
        return string.Format("You entered: {0}", value);
    }

    public CompositeType GetDataUsingDataContract(CompositeType composite)
    {
        if (composite == null)
        {
            throw new ArgumentNullException("composite");
        }
        if (composite.BoolValue)
        {
            composite.StringValue += "Suffix";
        }
        return composite;
    }
}
4

1 に答える 1

0

WCF Behaviors、特にWCF Security Behaviorsをご覧ください。ビヘイビアーを使用すると、WCF パイプラインにフックして、操作が実際に実行される前に好きなものをセットアップできます。

于 2012-09-18T01:26:49.437 に答える