【问题标题】:Pass message headers on WCF via netTcpBinding通过 netTcpBinding 在 WCF 上传递消息头
【发布时间】:2015-02-04 06:09:06
【问题描述】:

在敲了一天的头之后,我正在寻求帮助。我有一个 Web 窗体应用程序,WCF 服务托管在同一台机器上。 WCF 服务具有具有某种自定义权限的方法(例如,只有“admin”可以为用户设置密码)。

Web 表单应用程序使用表单身份验证,因此我拥有 HttpContext.Current.User 以及此使用的所有角色。所以我使用AuthCookie 来通过HttpRequestMessageProperty 传递对象并像这样读取它:

 HttpRequestMessageProperty property =
OperationContext.Current.IncomingMessageProperties[HttpRequestMessageProperty.Name] as HttpRequestMessageProperty;    

basicHttpBinding 工作正常。但是后来我需要实现 net.tcp 协议,所以 HttpRequestMessageProperty 不再起作用了。我尝试通过MessageHeader 添加它,但这也不起作用。

OperationContext.Current.IncomingMessageProperties没有我的标题...

那么如何将用户角色列表传递给我的 WCF?我正在使用这样的 Windows 安全性,但我需要从 HttpContext.Current.User 传递角色:

 <netTcpBinding>
    <binding transactionFlow="True">
      <security mode="Transport">
        <transport clientCredentialType="Windows" />
      </security>
    </binding>
  </netTcpBinding> 

【问题讨论】:

    标签: asp.net wcf soap nettcpbinding


    【解决方案1】:

    好的,所以我找到了传输角色信息的工作方式。虽然目前还不清楚为什么标准方法对我不起作用,但这是正确的方法(感谢Guy Burstein):

    客户:

                    MessageHeader<string> header = new MessageHeader<string>(roleData);
                    MessageHeader untypedHeader = header.GetUntypedHeader(ProjectParam.Role, "justASampleNamespace");
                    OperationContext.Current.OutgoingMessageHeaders.Add(untypedHeader);
    

    WCF:

    string roles = OperationContext.Current.IncomingMessageHeaders.GetHeader<string>(ProjectParam.Role, "justASampleNamespace");
    if(roles.Contains(ProjectParam.AdminLoginID))
    {
    //Now business logic comes
    }
    

    ProjectParam.Role 只是用于键的内部常量,第二个参数是示例字符串(MSDN 说“命名空间”)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-02-05
      • 1970-01-01
      • 2023-03-04
      • 2012-08-01
      • 1970-01-01
      • 2016-11-18
      • 2019-03-24
      • 2016-06-09
      相关资源
      最近更新 更多