【问题标题】:Securing WCF REST service for use with iPhone application保护 WCF REST 服务以与 iPhone 应用程序一起使用
【发布时间】:2010-12-02 01:16:05
【问题描述】:

我创建了一个简单的 WCF REST 服务,我打算从 iPhone 应用程序中使用它。 该服务运行良好,但现在我想保护它。

在我的测试环境(Windows 7 上的 IIS)中,我已经使用 makecert.exe 设置了自签名证书。

我还覆盖了 validate() 方法,因此我可以使用自定义用户名和密码(因为 Windows 身份验证是不可能的)。

现在我被困了两天多,想知道如何配置所有东西才能正常工作。

我现在的目标是能够通过浏览器进行简单的 GET 请求,例如:

https://localhost/testservice/service1.svc/sayHello

当这可行时,我将继续处理所有与 iPhone 相关的内容。

任何帮助/示例将不胜感激!

这是我的 web.config:

 <system.serviceModel>
<services>
  <service name="IphoneWcf.Service1" behaviorConfiguration="IphoneWcf.Service1Behavior">
    <!-- Service Endpoints -->
    <endpoint address="" binding="basicHttpBinding" bindingConfiguration="webBinding" behaviorConfiguration="webBehavior" contract="IphoneWcf.IService1">
      <!-- 
          Upon deployment, the following identity element should be removed or replaced to reflect the 
          identity under which the deployed service runs.  If removed, WCF will infer an appropriate identity 
          automatically.
      -->
      <identity>
        <dns value="localhost" />
      </identity>
    </endpoint>
    <!--<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/> -->
    <host>
      <baseAddresses>
        <add baseAddress="https://localhost/iphonewcf" />
      </baseAddresses> 
     </host>
  </service>
</services>
<behaviors>
  <endpointBehaviors>
    <behavior name="webBehavior">

    </behavior>
     </endpointBehaviors>
  <serviceBehaviors>
    <behavior name="IphoneWcf.Service1Behavior">
      <!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->
      <serviceMetadata httpsGetEnabled="false" />
      <!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information -->
      <serviceDebug includeExceptionDetailInFaults="true" />
       <serviceCredentials>
        <serviceCertificate findValue="localhost" storeLocation="LocalMachine" storeName="My" x509FindType="FindBySubjectName" />
      </serviceCredentials>
    </behavior>
  </serviceBehaviors>
</behaviors>
<bindings>
  <basicHttpBinding>
    <binding name="webBinding">
      <security mode="Transport">
        <transport clientCredentialType="Basic" />
      </security>
   </binding>
  </basicHttpBinding>
</bindings>

提前致谢!

【问题讨论】:

    标签: iphone wcf .net-3.5 ssl rest


    【解决方案1】:

    另一种选择是为客户端身份验证创建另一个证书。我很确定 iPhone 支持 X509 证书。只需将您的配置更改为具有“证书”的客户端凭据类型。这适用于基本身份验证,但如果您想唯一标识每个客户端,您可能仍需要自定义用户/名称密码而不是 Basic。

    【讨论】:

      【解决方案2】:

      您不应该重写任何方法。只需在您的 basicHttpBinding 中声明 Digest 传输安全:

      <bindings>
        <basicHttpBinding>
          <binding name="SecurityByTransport">
            <security mode="Transport">
              <transport clientCredentialType="Digest" />
             </security>
           </binding>
        </basicHttpBinding>
      </bindings>
      

      在您的 iPhone 应用程序中,您处理 NSURLConnectionDelegate connection:didReceiveAuthenticationChallenge 消息,如 Handling Authentication Challenges 所示:

      -(void)connection:(NSURLConnection *)connection 
              didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge 
      { 
          if ([challenge previousFailureCount] == 0) { 
              NSURLCredential *newCredential; 
              newCredential=[NSURLCredential credentialWithUser:[self preferencesName] 
                                                       password:[self preferencesPassword] 
                                                    persistence:NSURLCredentialPersistenceNone]; 
              [[challenge sender] useCredential:newCredential 
                     forAuthenticationChallenge:challenge]; 
          } else { 
              [[challenge sender] cancelAuthenticationChallenge:challenge]; 
          } 
      } 
      

      【讨论】:

      • 差不多了。我不能使用摘要,因为我的测试机器不属于任何域。因此,首先我使用基本身份验证。输入用户名/密码后,出现错误“找不到资源”
      • 实际上我不知道 IIS 只能针对域目录对摘要用户进行身份验证。我想我是按照 ASP.Net 身份验证提供程序 http 模块的思路来思考的,例如 msdn.microsoft.com/en-us/library/bb386455.aspx。您从哪里得到“找不到资源错误”?
      • 当我在 Internet Explorer 中对以下 URL 执行 GET 请求时:localhost/testservice/service1.svc/sayHello 我收到询问用户名和密码的提示。提供与我的 Windows 帐户关联的用户名和密码后 - 在那里我得到页面找不到错误
      猜你喜欢
      • 1970-01-01
      • 2011-11-27
      • 1970-01-01
      • 2011-12-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-26
      相关资源
      最近更新 更多