能够从客户端进行主要目的是在WCF服务端 [英] Ability to carry principal object from client to service side in WCF

查看:80
本文介绍了能够从客户端进行主要目的是在WCF服务端的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在WCF,在客户端,用户将被验证和他的角色/权限将被存储在客户端上的主/标识对象。一旦通过验证,用户应该只能够调用服务的方法,如果他是有一定的作用。要做到这一点,我需要客户端的主/身份对象发送到服务端。但是,一旦我得到的服务端,主要目的是Windows主体和身份的Windows标识。这并不让我检查服务方法应该基于客户端的凭据进行调用。



是否有可能我的本金和标识对象从客户端转移到服务器端?我想我的主要对象(通用校长)传送到服务器端。是否可以?请帮忙。



早些时候我张贴类​​似的问题如下:



结转定制的Principal对象的WCF服务端客户端



我试图通过答案跟随,但我没能扛过我的主要对象。



下面是详细内容。



客户端我的主要对象和标识对象看起来调试过程中立即窗口如下:



System.Threading.Thread.CurrentPrincipal
{System.Security.Principal.GenericPrincipal}
〔System.Security.Principal.GenericPrincipal]:{} System.Security.Principal.GenericPrincipal
身份:{} System.Security.Principal.GenericIdentity
System.Threading.Thread.CurrentPrincipal.Identity
{} System.Security.Principal.GenericIdentity
[System.Security.Principal.GenericIdentity]:{} System.Security.Principal.GenericIdentity
AuthenticationType:
IsAuthenticated:假
名称:



服务器端,我的主要目标并标识如下所示:



System.Threading.Thread.CurrentPrincipal
{} System.Security.Principal.WindowsPrincipal
[System.Security.Principal.WindowsPrincipal] :{} System.Security.Principal.WindowsPrincipal
身份:{} System.Security.Principal.WindowsIdentity
{} System.Security.Principal.WindowsIdentity
[System.Security.Principal.WindowsIdentity ]:{} System.Security.Principal.WindowsIdentity
AuthenticationType:NTLM
IsAuthenticated:真正的
名称:MyDomain\MyLoginID



我的WCF客户端看起来如下:



客户端代码:

 命名空间的ConsoleApplication1 
{
类节目
{
静态无效的主要(字串[] args)
{
ServiceReference1.Service1Client客户端=新Service1Client(NetTcpBinding_IService1);

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


到Console.ReadLine();
}
}
}



客户端配置如下所示:

 <?XML版本=1.0编码=UTF-8>?; 
<结构>
< system.serviceModel>
<&绑定GT;
<&NetTcpBinding的GT;
<绑定名称=NetTcpBinding_IService1closeTimeout =10:10:00
openTimeout =10:10:00receiveTimeout =10:10:00的SendTimeout =10:10: 00
transactionFlow =假transferMode =缓冲transactionProtocol =OleTransactions
hostNameComparisonMode =StrongWildcard将ListenBackLog =10
maxBufferPoolSize =524288MAXBUFFERSIZE =65536MAXCONNECTIONS =10
maxReceivedMessageSize =65536>
< readerQuotas MAXDEPTH =32maxStringContentLength =8192maxArrayLength =16384
maxBytesPerRead =4096maxNameTableCharCount =16384/>
<有序的ReliableSession =真正的inactivityTimeout =10:10:00$ B $ =使假/> B:
<安全模式=运输>
<运输clientCredentialType =窗口的ProtectionLevel =EncryptAndSign/>
<消息clientCredentialType =的Windows/>
< /安全>
< /&结合GT;
< / NetTcpBinding的>
< /绑定>
<客户端>
<端点地址=的net.tcp://本地主机:8888 / Service1的绑定=NetTcpBinding的
bindingConfiguration =NetTcpBinding_IService1合同=ServiceReference1.IService1
NAME = NetTcpBinding_IService1>

< /端点>
< /客户>
< /system.serviceModel>
< /结构>



服务代码如下:

  [的ServiceContract] 
公共接口IService1
{
[OperationContract的]
字符串的GetData(int值);

[OperationContract的]
的CompositeType GetDataUsingDataContract(CompositeType中复合材料);

// TODO:添加您的业务运营在这里
}

//使用数据合同下面的示例中所示复合类型添加到服务操作
〔DataContract]
公共类的CompositeType
{
布尔boolValue = TRUE;
串stringValue的=你好;

[数据成员]
公共BOOL BoolValue
{
{返回boolValue; }
集合{boolValue =价值; }
}

[数据成员]
公共字符串的StringValue
{
{返回stringValue的; }
集合{stringValue的=价值; }
}
}


公共类服务1:IService1
{
公共字符串的GetData(int值)
{
返回的String.Format(您输入:{0},值);
}

公众的CompositeType GetDataUsingDataContract(CompositeType中复合材料)
{
如果(复合== NULL)
{
抛出新的ArgumentNullException(综合);
}
如果(composite.BoolValue)
{
composite.StringValue + =后缀;
}
返回复合材料;
}
}


解决方案

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



的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

这篇关于能够从客户端进行主要目的是在WCF服务端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆