能够在WCF中将主要对象从客户端传送到服务端

在WCF中,在客户端,用户将被认证,他的角色/权限将存储在客户端的Principal / Identity对象中。 经过身份validation后,用户应该只能在某个角色中调用服务方法。 为此,我需要将客户端Principal / Identity对象传输到服务端。 但是一旦我到达服务端,主要对象是Windows Principal,Identity是Windows Identity。 这不允许我检查是否应该根据客户端凭据调用服务方法。

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

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

将客户端定制的Principal对象转移到WCF服务端

我试图通过答案,但我无法inheritance我的主要对象。

这是细节。

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

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名称:“”

服务器端,我的主要对象和标识如下所示:

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”

我的WCF客户端如下所示

客户代码:

namespace ConsoleApplication1 { class Program { static void Main(string[] args) { ServiceReference1.Service1Client client = new Service1Client("NetTcpBinding_IService1"); Console.WriteLine(client.GetData(6548)); Console.ReadLine(); } } } 

客户端配置如下所示:

                      

服务代码如下:

 [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行为 ,特别是WCF安全行为 。 行为允许您在操作实际执行之前挂钩到WCF管道以设置您喜欢的内容。