c# - 能够在 WCF 中将主体对象从客户端传送到服务端

标签 c# .net wcf

在 WCF 中,在客户端,用户将经过身份验证,他的角色/权限将存储在客户端的主体/身份对象中。一旦通过身份验证,用户只有在担任特定角色时才能调用服务方法。为此,我需要将客户端主体/身份对象传输到服务端。但是一旦我到达服务端,主体对象就是 Windows Principal,Identity 就是 Windows Identity。这不允许我检查是否应根据客户端凭据调用服务方法。

是否可以将我的主体和身份对象从客户端传输到服务器端?我想将我的主体对象(通用主体)传输到服务器端。是否可以?请帮忙。

之前我发过类似的问题如下:

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

我试图按照答案进行操作,但我无法继承我的主要目标。

这里是详细信息。

在调试期间,在客户端,我的 Principal 对象和身份对象在立即窗口中如下所示:

System.Threading.Thread.CurrentPrincipal {System.Security.Principal.GenericPrincipal} [System.Security.Principal.GenericPrincipal]:{System.Security.Principal.GenericPrincipal} 身份:{System.Security.Principal.GenericIdentity} 系统.Threading.Thread.CurrentPrincipal.Identity {System.Security.Principal.GenericIdentity} [System.Security.Principal.GenericIdentity]:{System.Security.Principal.GenericIdentity} 身份验证类型:“” 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} 身份验证类型:“NTLM” 已认证:真 名称:“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;
    }
}

最佳答案

看看WCF Behaviors特别是 WCF Security Behaviors .行为允许您连接到 WCF 管道以在操作实际执行之前设置您喜欢的内容。

关于c# - 能够在 WCF 中将主体对象从客户端传送到服务端,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12469116/

相关文章:

.net - 使用 Nancy + TinyIoC 通过 Quartz JobFactory 注入(inject)依赖项

javascript - 呈现后的母版页控件 Id

c# - VS2019 Roslyn编译器通用约束方法解析

c# - 卡住多个工作表中的 Pane C#

.net - MongoDB 索引从开发到生产

无论我做什么,WCF 服务都只处理 10 个并发调用

c# - 如何解析从文件加载的 soap 消息?

wcf - IBM DataPower 3.7.1.x 与 WCF 客户端相关的问题

c# - 将 C# 列表中的数据从一种类型转换为另一种类型。

c# - 在 TCP 期间发送断点会影响结果