服务器端的WCF客户端身份验证

服务器端的WCF客户端身份验证,wcf,authentication,impersonation,Wcf,Authentication,Impersonation,我对我的客户有这样的结构 WindowsIdentity wi = WindowsIdentity.GetCurrent(); IntPtr token = wi.Token; 下一步是通过WCF向服务器发送身份验证令牌,并在那个里模拟用户 api.SendToken(token); ... ... ... 但是,当我在服务器端收到令牌并尝试构建WindowsIdentity时,它会向我抛出一个错误: WindowsIdentity newId = new WindowsIdentity(

我对我的客户有这样的结构

WindowsIdentity wi = WindowsIdentity.GetCurrent();
IntPtr token = wi.Token;
下一步是通过WCF向服务器发送身份验证令牌,并在那个里模拟用户

api.SendToken(token);

...
...
...
但是,当我在服务器端收到令牌并尝试构建WindowsIdentity时,它会向我抛出一个错误:

WindowsIdentity newId = new WindowsIdentity(token);

Invalid token for impersonation - it cannot be duplicated.
请你们帮我找出我做错了什么,并分享你们的想法,我如何将令牌从客户端传递到服务器


谢谢

WCF已经有了内置的管道系统来支持,你为什么要自己动手

更新以避免仅链接的答案(啊,我年轻时的错误…)

以下是配置内置WCF模拟所需的基本步骤

  • 只有一些绑定支持Windows身份验证。WSHttpBinding是最常见的支持它的绑定,但其他人也可能支持它

  • 在服务协定上,对需要模拟的方法使用OperationBehavior属性:

    [OperationBehavior(Impersonation=ImpersonationOption.Required)]
    public string SomeMethod(string aParameter) { ... }
    
  • 对于客户端,最简单的方法是创建一个从ClientBase类继承的自定义类。所有服务引用类型都继承自此类。以下是客户端代码的示例:

    var client = new SomeClientBaseDerivedType("TheServiceEndpoint");
    client.ClientCredentials.Windows.AllowedImpersonationLevel =
        System.Security.Principal.TokenImpersonationLevel.Impersonation;
    

WCF已经有内置的管道来支持,你为什么要尝试自己的管道

更新以避免仅链接的答案(啊,我年轻时的错误…)

以下是配置内置WCF模拟所需的基本步骤

  • 只有一些绑定支持Windows身份验证。WSHttpBinding是最常见的支持它的绑定,但其他人也可能支持它

  • 在服务协定上,对需要模拟的方法使用OperationBehavior属性:

    [OperationBehavior(Impersonation=ImpersonationOption.Required)]
    public string SomeMethod(string aParameter) { ... }
    
  • 对于客户端,最简单的方法是创建一个从ClientBase类继承的自定义类。所有服务引用类型都继承自此类。以下是客户端代码的示例:

    var client = new SomeClientBaseDerivedType("TheServiceEndpoint");
    client.ClientCredentials.Windows.AllowedImpersonationLevel =
        System.Security.Principal.TokenImpersonationLevel.Impersonation;
    
有用吗?有用吗?