2013-10-10 41 views
2

我在iOS應用中使用HTTPS POST連接到內部公司REST服務。 此服務需要基本的HTTP身份驗證。NSURLConnection將發送請求認證挑戰安全

當我嘗試在HTTPHeader中傳遞身份驗證參數時,服務器迴應並返回錯誤消息,說「錯誤域= NSURLErrorDomain代碼= -1202」此服務器的證書無效。您可能會連接到假裝爲「oiam.XXXX.com」的服務器,這可能會使您的機密信息處於危險之中。「UserInfo = 0x8d1de60 {NSErrorFailingURLStringKey = https://oiam.xxxx.com/NSLocalizedRecoverySuggestion=Would您想要連接到服務器嗎?」,

我讀了一些問題,看起來像我需要安裝證書。由於我在iOS模擬器上,因此無法安裝證書。

我試圖使用NSURLConnectionDelegate協議和連接工作

唯一的問題是我有這種方法,我通過用戶名密碼。它安全嗎?爲什麼我不需要對值進行編碼?我在進行Header中的HTTPS基本身份驗證時傳遞值。

-(void)connection:(NSURLConnection *)connection  willSendRequestForAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge { 
if ([challenge previousFailureCount]) { 
    [[challenge sender] cancelAuthenticationChallenge:challenge]; 
} else { 
    NSURLCredential *credential = [NSURLCredential credentialWithUser:@"username1" 
            password:@"password1"              
            persistence:NSURLCredentialPersistenceForSession]; 
    [[challenge sender] useCredential:credential forAuthenticationChallenge:challenge]; 
} 
} 

回答

3

供內部使用和測試,你可以使用類擴展在NSURLRequest覆蓋私有API,使得無效的證書被接受:

#if DEBUG 

@interface NSURLRequest (IgnoreSSL) 

+ (BOOL)allowsAnyHTTPSCertificateForHost:(NSString *)host; 

@end 

@implementation NSURLRequest (IgnoreSSL) 

+ (BOOL)allowsAnyHTTPSCertificateForHost:(NSString *)host 
{ 
    // ignore certificate errors only for this domain 
    if ([host hasSuffix:@"oiam.XXXX.com"]) { 
     return YES; 
    } 
    else { 
     return NO; 
    } 
} 

@end 

#endif 

至於有關憑據的安全性問題,它們將在通過電線傳輸之前根據需要進行編碼。

+2

謝謝。我現在使用HTTPS Basic Auth(在Header中傳遞證書)並從Stack Overflow添加了這個代碼以刪除證書錯誤: - (void)connection:(NSURLConnection *)連接willSendRequestForAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge if [[[challenge認證方法] == NSURLAuthenticationMethodServerTrust){[challenge sender] useCredential:[NSURLCredential credentialForTrust:[[challenge protectionSpace] serverTrust]] for challenge authentication:challenge]; }}它的工作。我試圖弄清楚這是什麼。 – Jasmine

+0

@Jasmine你可以把整個代碼用於willSendRequestForAuthenticationChallenge,所以我對它有了更多的瞭解。爲你+1。 –

+0

@DhavalBhadania我現在正在使用neilco的建議。它適用於任何自簽名證書的開發。我只是忽略了錯誤。在簽發證書時的生產中,我擺脫了NSURLRequest(IgnoreSSL)文件。 – Jasmine

相關問題