我在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];
}
}
謝謝。我現在使用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
@Jasmine你可以把整個代碼用於willSendRequestForAuthenticationChallenge,所以我對它有了更多的瞭解。爲你+1。 –
@DhavalBhadania我現在正在使用neilco的建議。它適用於任何自簽名證書的開發。我只是忽略了錯誤。在簽發證書時的生產中,我擺脫了NSURLRequest(IgnoreSSL)文件。 – Jasmine