且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

能够从客户端进行主要目的是在WCF服务端

更新时间:2023-11-12 23:49:04

的下面的文章和下载的代码在回答我的问题非常有帮助。



的http:// msdn.microsoft.com/en-us/magazine/cc948343.aspx


In WCF, on the client side, user would be authenticated and his roles/permissions would be stored in Principal/Identity objects on the client side. Once authenticated, user should only be able to invoke service method if he is in a certain role. For that to happen, I need to transmit client side Principal/Identity objects to service side. But once I get to the service side, the principal object is Windows Principal and Identity is Windows Identity. This does not allow me to check if service method should be invoked based on client side credentials.

Is it possible to transfer my principal and identity object from client side to server side? I want to transmit my principal object (Generic Principal) to server side. Is it possible? Please help.

Earlier I posted similar question as follows:

Carry over client side customized Principal object to the WCF service side

I tried to follow through the answers but I was not able to carry over my principal object.

Here are the details.

On the client side my Principal object and identity object looks as follows in Immediate window during debugging:

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

On the server side, my principal object and identity looks as follows:

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

My WCF client looks as follows

Client code:

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

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


            Console.ReadLine();
        }
    }
}

Client Config looks as follows:

<?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>

Service code looks as follows:

[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;
    }
}

The following article and its downloaded code was very helpful in answering my question.

http://msdn.microsoft.com/en-us/magazine/cc948343.aspx