Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
671 views
in Technique[技术] by (71.8m points)

c# - Ability to carry principal object from client to service side in WCF

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: "MyDomainMyLoginID"

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;
    }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Take a look at WCF Behaviors and in particular WCF Security Behaviors. Behaviors allow you to hook into the WCF pipeline to set up what you like before the operation actually executes.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...