在WCF中,在客户端,用户将被认证,他的角色/权限将存储在客户端的Principal / Identity对象中。经过身份验证后,用户应该只能在某个角色中调用服务方法。为此,我需要将客户端Principal / Identity对象传输到服务端。但是一旦我到达服务端,主要对象是Windows Principal,Identity是Windows Identity。这不允许我检查是否应该根据客户端凭证调用服务方法。
是否可以将我的主体和身份对象从客户端传输到服务器端?我想将我的主要对象(Generic Principal)传输到服务器端。可能吗?请帮忙。
之前我发布了类似的问题如下:
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} 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} 身份:{System.Security.Principal.WindowsIdentity} {} System.Security.Principal.WindowsIdentity [System.Security.Principal.WindowsIdentity]:{System.Security.Principal.WindowsIdentity} AuthenticationType:“NTLM” IsAuthenticated:是的 名称:“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;
}
}