【问题标题】:NSURLConnectionDelegate For Authentication Challenge won't get called on iOS?NSURLConnectionDelegate For Authentication Challenge 不会在 iOS 上被调用?
【发布时间】:2015-10-09 10:44:53
【问题描述】:

我正在使用NSURLConnection 连接https api 并且服务器正在使用

"Go Daddy Certificate Authority - G2" and "is encrypted using a modern cipher suite.

The connection uses TLS 1.2.

The connection is encrypted and authenticated using AES_128_GCM and uses ECDHE_RSA as the key exchange mechanism."

我已经实现了以下代表

- (BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace {
    return [protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust];
}

- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge {
    [challenge.sender continueWithoutCredentialForAuthenticationChallenge:challenge];
}

- (void)connection:(NSURLConnection *)connection willSendRequestForAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
{
 [[challenge sender] useCredential:[NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust] forAuthenticationChallenge:challenge];
}

但问题是“没有一个委托方法正在命中”。为什么身份验证方法没有命中以及如何解决?

在实现文件中使用如下@interface classname () < NSURLConnectionDelegate, UIAlertViewDelegate >

而 NSURLConnection Delegate 直接点击下面的方法- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response

NSURLConnection代码如下

    NSURL *url = [NSURL URLWithString:@"urlString"];
     NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
    [request setURL:url];
    [request setHTTPMethod:@"POST"];
    [request addValue:postLength forHTTPHeaderField:@"Content-Length"];
    [request addValue:@"text/xml; charset=utf-8" forHTTPHeaderField:@"Accept"];
    [request addValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
    [request setValue:@"v1.1" forHTTPHeaderField:@"Version"];
    [request setHTTPBody:postData];

    DLog(@"%@\n%@\n%@", request, request.allHTTPHeaderFields, [[NSString alloc] initWithData:request.HTTPBody encoding:NSUTF8StringEncoding]);

    self.responseData = [[NSMutableData alloc] init];
    // send the login request
    self.loginConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:NO];

    [self.loginConnection scheduleInRunLoop:[NSRunLoop mainRunLoop]
                                    forMode:NSDefaultRunLoopMode];
    [self.loginConnection start];

【问题讨论】:

  • 您的 xcode 版本是 Xcode 7 吗?
  • 感谢回复,xcode版本是6.4

标签: ios security https aes rsa


【解决方案1】:

这发生在将 iOS 7 应用程序更新到 iOS 8 时。我们使用 Oracle SOA 作为中间件,结果它停止调用委托方法。以下适用于 iOS8 和 iOS7。

 - (void) addBasicHTTPAuthenticationHeaders
        {
            NSString * wsUserName = @"userNameForWebService";
            NSString * wsPassword = @"passwordForWebService";

            NSString *authStr = [NSString stringWithFormat:@"%@:%@", wsUserName, wsPassword];
            NSData *authData = [authStr dataUsingEncoding:NSASCIIStringEncoding];
            NSString *authValue = [NSString stringWithFormat:@"Basic %@", [authData base64EncodedStringWithOptions:NSDataBase64EncodingEndLineWithCarriageReturn]];
            [urlRequest setValue:authValue forHTTPHeaderField:@"Authorization"];
        }

对于 iOS 8 及更高版本,您必须实现 willSendRequestForAuthenticationChallenge:connection:didReceiveAuthenticationChallenge: 在 iOS8 中不被调用,仅在旧操作系统中调用。

因此,要在 iOS8 及更高版本中提供身份验证,请实现上述方法,您必须在其中调用挑战响应方法之一(NSURLAuthenticationChallengeSender` 协议):

useCredential:forAuthenticationChallenge:

continueWithoutCredentialForAuthenticationChallenge: 

cancelAuthenticationChallenge:

performDefaultHandlingForAuthenticationChalleng: 

rejectProtectionSpaceAndContinueWithChallenge: didReceiveAuthenticationChallengeNSURLConnectionDelegate 上的一个方法,而您的其余方法(didFailWithError 除外)都是 NSURLConnectionDataDelegate 方法。您是否在控制器中实现了这两种协议?如果您发布所有班级的代码可能会有所帮助。

【讨论】:

  • 我已经编辑了“NSURLConnection”,如上所示,但它仍然没有命中 Authentication 方法,而是直接命中了 didReceiveResponse 方法。我已经编辑了这个问题。
  • 由于应用程序也支持 ios7,所以使用了已弃用的方法
  • 是的,你是对的,所以你目前在 iOS 7 模拟器中运行它?
  • 当前运行在 iPhone 4S - 操作系统版本 8.1.2
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-08-29
  • 2023-03-25
  • 1970-01-01
  • 2020-10-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多