【问题标题】:How to enable basic authentication on WCF 4.0 REST Service?如何在 WCF 4.0 REST 服务上启用基本身份验证?
【发布时间】:2011-09-24 06:42:23
【问题描述】:

首先,网上有很多关于这方面的文章,但其中大部分是针对 WCF 3.5 的。我正在使用 WCF 4.0 的一些特性,这使得场景有点不同。

以下是我的合同:

[ServiceContract]
    public interface IService1
    {

        [OperationContract]
        [WebGet(UriTemplate="x?v={value}")]
        string GetData(int value);
    }

以下是我的服务类:

[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class Service1 : IService1
    {
        public string GetData(int value)
        {
            return string.Format("You entered: {0}", value);
        }
    }

以下是我的 web.config

<system.serviceModel>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" />
    <standardEndpoints>
      <webHttpEndpoint>
        <standardEndpoint automaticFormatSelectionEnabled="true" />
      </webHttpEndpoint>
    </standardEndpoints>
  </system.serviceModel>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true">
      <add name="UrlRoutingModule" type="System.Web.Routing.UrlRoutingModule, System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
    </modules>
    <handlers>
      <add name="UrlRoutingHandler" preCondition="integratedMode" verb="*" path="UrlRouting.axd"/>
    </handlers>
  </system.webServer>

我没有使用任何 svc 文件,所以在 Global.asax 中我编写了以下代码行来在路径“blah”处公开我的服务

protected void Application_Start(object sender, EventArgs e)
        {
            RouteTable.Routes.Add(new ServiceRoute("blah", new WebServiceHostFactory(), typeof(Service1)));
        }

如果我在 web.config 中更改标准端点,现在可以启用基本身份验证,如下所示:

<standardEndpoints>
      <webHttpEndpoint>
        <standardEndpoint automaticFormatSelectionEnabled="true">
          <security mode="Transport">
            <transport clientCredentialType="Basic" />
          </security>
        </standardEndpoint>
      </webHttpEndpoint>
    </standardEndpoints>

我收到以下错误:

找不到一个基地址 匹配端点的方案 https 与绑定 WebHttpBinding。 注册的基地址方案是 [http]。

从现在开始我应该怎么做才能让它发挥作用?我将在哪里检查每个请求提供的用户名/密码?

【问题讨论】:

    标签: wcf wcf-security wcf-rest


    【解决方案1】:

    这无法在 ASP.NET 开发服务器中进行测试,并且要在 IIS 7.5 中运行,我们必须安装基本身份验证,还需要创建证书并将 ssl 绑定与该应用程序相关联。

    所有这些都导致 web 服务方法需要基本身份验证,但它对 AD 起作用。

    没有办法改变它来自己验证它。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-01-13
      • 2011-08-17
      • 2011-06-10
      • 1970-01-01
      • 1970-01-01
      • 2011-02-04
      • 2011-11-03
      • 1970-01-01
      相关资源
      最近更新 更多