c# - 无法让 WCF 休息服务通过 HTTPS 工作

标签 c# .net wcf

我们有一个正在运行的 ASP.NET 应用程序,我已经向它添加了一个 WCF Rest 服务。在本地和部署在测试环境中时,这工作正常。问题是当我们部署到只有 HTTPS 的生产环境时。

我已经在网上搜索和阅读了大部分答案,并尝试了很多东西。一切都没有运气。

这是我们的简单代码

[ServiceContract]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class ReportingService
{
    public ReportingService()
    {
        Thread.CurrentPrincipal = HttpContext.Current.User;
    }

    [OperationContract]
    [WebGet(UriTemplate = "get/{id}", ResponseFormat = WebMessageFormat.Json)]
    [PrincipalPermission(SecurityAction.Demand)]
    public List<RawReportTable> GetReport(string id)
    {
        ...
    }
}

在 Global.asax.cs 中我们有

RouteTable.Routes.Add(new ServiceRoute("api/reporting", new WebServiceHostFactory(), typeof(ReportingService)));

在我们的 web.config 中,我们为 system.serviceModel 定义了以下内容

<standardEndpoints>
  <webHttpEndpoint>
    <standardEndpoint name="api" helpEnabled="true" automaticFormatSelectionEnabled="true" maxBufferSize="500000" maxReceivedMessageSize="500000">          
      <readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />
      <security mode="Transport" />
    </standardEndpoint>
  </webHttpEndpoint>
</standardEndpoints>

<behaviors>
  <endpointBehaviors>
    <behavior name="api">
      <webHttp />
    </behavior>
  </endpointBehaviors>

  <serviceBehaviors>
    <behavior name="">
      <serviceCredentials>
          <serviceCertificate storeLocation="LocalMachine" storeName="My" x509FindType="FindByThumbprint" findValue="d5 85 5b 37 89 47 6f 89 71 5b b7 5d 87 6f 2e e5 24 aa 57 b6" />
      </serviceCredentials>
    </behavior>
  </serviceBehaviors>
</behaviors>

<services>
  <service name="ReportingService">
    <endpoint address="api/reporting" behaviorConfiguration="api" binding="webHttpBinding" bindingConfiguration="webBinding" contract="WayfinderFM.Service.api.ReportingService" />
  </service>
</services>

<bindings>
  <webHttpBinding>
    <binding name="webBinding">
      <security mode="Transport" />
    </binding>
  </webHttpBinding>
</bindings>

通过此设置,我收到以下错误: 请求错误:服务器在处理请求时遇到错误。有关详细信息,请参阅服务器日志。

我认为(和一些示例显示)我不再需要配置中的服务/端点内容,因为它已在路由中注册。

删除那部分我们仍然得到同样的错误。我尝试了很多不同的配置,但都没有运气。

奇怪的是/api/reporting/help 实际显示。只是无法使用任何服务。

有人知道吗?我希望这是我错过的简单事情。

谢谢大家

编辑

我认为这与 [PrincipalPermission(SecurityAction.Demand)] 我们使用它来确保用户已通过身份验证并且我们可以访问 token 。我找到了 PrincipalPermission.Demand() failing once WCF Service was moved to SSL遗憾的是没有答案。

最佳答案

不想回答我自己的问题,但在阅读 Rajesh 的消息后,我更深入地研究了错误。正如我的编辑所说,它与 PrincipalPermission 属性有关。如果我删除它,它正在运行我的代码并且我的身份验证失败。

回到网上搜索,我找到了 http://forums.silverlight.net/t/34954.aspx/1

我刚刚添加到 serviceBehaviours。所以看起来像

<serviceBehaviors>
    <behavior name="">
        <serviceDebug httpHelpPageEnabled="true" includeExceptionDetailInFaults="true" />
        <serviceAuthorization principalPermissionMode="None"/>
    </behavior>
</serviceBehaviors>

现在它可以工作了,我可以调用我的服务并被拒绝,除非我经过身份验证等。耶

希望这可以帮助其他人解决这个问题

关于c# - 无法让 WCF 休息服务通过 HTTPS 工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11205295/

相关文章:

c# - 接口(interface)可以需要一个属性,但不指定所需的类型吗?

c# - nHibernate - 类中的对象或 ObjectID

C# .net 对象引用未设置为静态类上的对象实例

.net - 奇怪的 LINQ to SQL .Union() 错误

.net - 当参数相互冲突时抛出什么最合适的异常?

c# - Array.Reverse算法?

c# - 在 ObjectDataSource 上使用选择属性而不是 'SelectMethod'?

.net - 我想要一些调试 WCF Web 服务异常的技巧

c# - 忽略从 WCF 请求返回的数据中的关系

.net - 如何以编程方式配置 WCF 已知类型?