【问题标题】:Secure WCF not working with non windows Clients iPhone安全 WCF 不适用于非 Windows 客户端 iPhone
【发布时间】:2012-03-12 12:24:28
【问题描述】:

我创建了 WCF SOAP 服务来与我的 iPhone 应用程序一起使用。它可以与 VS 中的服务客户端“编码客户端”和 WCF 测试客户端工具一起正常工作,但 iPhone、Java 或 PHP 无法正常工作。 WCF 它在 https 配置上工作

像这样绑定

<bindings>
  <wsHttpBinding>
    <binding name="wsHttpEndpointBinding" maxBufferPoolSize="2147483647"
      maxReceivedMessageSize="2147483647">
      <readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647" />
      <security mode="TransportWithMessageCredential">
        <transport clientCredentialType="None" />
        <message clientCredentialType="UserName" negotiateServiceCredential="false"
          establishSecurityContext="false" />
      </security>
    </binding>
  </wsHttpBinding>
</bindings>

以及类似的行为:

<services>
      <service behaviorConfiguration="ServiceBehavior" name="HR_Service.Service">
        <endpoint address="mex" binding="mexHttpsBinding" bindingConfiguration=""
          name="MexHttpsBindingEndpoint" contract="HR_Service.IService">
          <identity>
            <dns value="localhost" />
          </identity>
        </endpoint>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="ServiceBehavior">
          <serviceMetadata httpGetEnabled="false" httpsGetEnabled="true"
            httpsGetBinding="" />
          <serviceDebug includeExceptionDetailInFaults="false" />
          <dataContractSerializer maxItemsInObjectGraph="6553600" />
        </behavior>
      </serviceBehaviors>
    </behaviors>

和 ASP.net 会话兼容性

<serviceHostingEnvironment aspNetCompatibilityEnabled="true"
      multipleSiteBindingsEnabled="true" /> 

我的 iPhone 是这样的

+ (BOOL)allowsAnyHTTPSCertificateForHost:(NSString *)host
{
    return YES; // Or whatever logic
}

+ (void)setAllowsAnyHTTPSCertificate:(BOOL)allow forHost:(NSString*)host 
{ 
}

从服务器接受证书,然后 SOAP 信封

NSString *soapMessage = [NSString stringWithFormat:@"<?xml version=\"1.0\" encoding=\"utf-8\"?><soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns=\"http://tempuri.org/\"><soap:Body><Authenticate><userid>username</userid><password>pass</password></Authenticate></soap:Body></soap:Envelope>"];

然后是信封

NSURL *url = [NSURL URLWithString:@"https://mywebsite.com/Service/Service1.svc"];                           
    NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];                         
    NSString *msgLength = [NSString stringWithFormat:@"%d", [soapMessage length]];              
    [theRequest addValue: @"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];       
    [theRequest addValue: @"http://tempuri.org/IService1/Authenticate" forHTTPHeaderField:@"soapAction"];
    [theRequest addValue: msgLength forHTTPHeaderField:@"Content-Length"];
    [NSURLRequest setAllowsAnyHTTPSCertificate:YES forHost:[url host]]; 
    [theRequest setHTTPMethod:@"POST"];     
    [theRequest setHTTPBody: [soapMessage dataUsingEncoding:NSUTF8StringEncoding]];
    NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];

    if(theConnection) {
        webData = [NSMutableData data] ;
    }
    else {
        NSLog(@"theConnection is NULL");
    }

NSURLConnectionDelegate

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
    [webData setLength:0];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
    [webData appendData:data];
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
    NSLog(@"%@",[NSString stringWithFormat:@"Connection failed: %@", [error description]]);
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {

    NSLog(@"Data has been loaded");

    NSString *responseString = [[NSString alloc] initWithData:webData encoding:NSUTF8StringEncoding];


    NSLog(@"Respose Data :%@",responseString) ;


}

连接总是成功,但响应数据总是空的

【问题讨论】:

    标签: iphone ios wcf soap https


    【解决方案1】:

    你的soapMessage错了

    你使用了错误的标签

    在 soamMessage 字符串中使用“Authenticate”标签而不是“test”标签

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-08-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多