我的服务器提供了几种身份验证方法:NTLM和摘要
我的iOS客户端无法处理NTLM身份验证,因此我实施connection:willSendRequestForAuthenticationChallenge:
委托拒绝NTLM,然后仅使用正确的凭据进行摘要身份验证质询。
到目前为止,iOS 7上的一切正常。
但是在iOS 8上,我发现了一种奇怪的行为:
connection:willSendRequestForAuthenticationChallenge:
代表在大多数时间(95%)不会被呼叫!!
我收到了这个错误:
Error: Error Domain=NSPOSIXErrorDomain Code=54 "The operation couldn’t be completed. Connection reset by peer"
UserInfo=0x16520fb0 {_kCFStreamErrorCodeKey=54,
NSErrorPeerAddressKey=<CFData 0x16682e40 [0x2f752440]>{length = 16, capacity = 16, bytes = 0x10020d7eac12780b0000000000000000},
NSErrorFailingURLKey=http://SERVER_IP:SERVER_PORT/Tunnel/Message.aspx,
NSErrorFailingURLStringKey=http://SERVER_IP:SERVER_PORT/Tunnel/Message.aspx,
_kCFStreamErrorDomainKey=1}
只有5%的时间正确调用代理并正常工作。
下面显示了我如何将请求发送到服务器并处理身份验证质询:
- (void)postRequest
{
NSString *IP = SERVER_IP;
int port = SERVER_PORT;
NSString *url = [NSString stringWithFormat:@"http://%@:%d/Tunnel/Message.aspx", IP, port];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:url]];
[request setHTTPMethod:@"POST"];
[request setValue:@"application/xml" forHTTPHeaderField:@"Content-Type"];
NSString *xml = [NSString stringWithFormat:@"<?xml version=\"1.0\" encoding=\"UTF-8\"?><GetServerInfo></GetServerInfo>"];
[request setHTTPBody: [xml dataUsingEncoding:NSUTF8StringEncoding]];
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
}
- (void)connection:(NSURLConnection *)connection willSendRequestForAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
{
NSLog(@"%@", challenge.protectionSpace);
if ([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodHTTPDigest])
{
if ([challenge previousFailureCount] == 0)
{
[[challenge sender] useCredential:[NSURLCredential credentialWithUser:USERNAME
password:PASSWORD
persistence:NSURLCredentialPersistenceNone]
forAuthenticationChallenge:challenge];
}
else
{
[[challenge sender] continueWithoutCredentialForAuthenticationChallenge:challenge];
}
}
else
{
[[challenge sender] rejectProtectionSpaceAndContinueWithChallenge:challenge];
}
}
这些代码在iOS 7上运行,willSendRequestForAuthenticationChallenge
在身份验证挑战期间多次被调用,但在iOS 8上甚至没有被调用过一次!
这可能是iOS 8的错误还是自iOS 8以来发生了什么变化?
答案 0 :(得分:2)
当我将iOS 7应用程序更新到iOS 8时,发生了这种情况。我们使用Oracle SOA作为中间件,并且它已经停止调用委托方法。以下适用于iOS8和iOS7。 (使用Xcode 6.1)
- (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"];
}