【问题标题】:Is NSURLConnection willSendRequestForAuthenticationChallenge secureNSURLConnection willSendRequestForAuthenticationChallenge 是否安全
【发布时间】:2013-10-10 19:35:39
【问题描述】:

我正在我的 iOS 应用程序中使用 HTTPS POST 连接到内部公司 REST 服务。 此服务需要基本的 HTTP 身份验证。

当我尝试在 HTTPHeader 中传递身份验证参数时,服务器返回错误消息 “Error Domain=NSURLErrorDomain Code=-1202”此服务器的证书无效。您可能正在连接到伪装成“oiam.XXXX.com”的服务器,这可能会使您的机密信息面临风险。" UserInfo=0x8d1de60 {NSErrorFailingURLStringKey=https://oiam.xxxx.com/NSLocalizedRecoverySuggestion=Would 你还是想连接到服务器?, "

我阅读了一些问题,看起来我需要安装证书。由于我在 iOS 模拟器上,因此无法安装证书。

我尝试使用 NSURLConnectionDelegate 协议并且连接工作

我唯一的问题是我传递用户名密码的这种方法。它安全吗?为什么我不需要对值进行编码?在 Header 中进行 HTTPS Basic 身份验证时,我正在为传递值进行编码。

-(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];
}
}

【问题讨论】:

    标签: ios ssl nsurlconnection nsurlconnectiondelegate


    【解决方案1】:

    对于内部使用和测试,您可以使用类扩展覆盖 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 
    

    关于凭证的安全性问题,在通过网络传输之前,它们将根据需要进行编码。

    【讨论】:

    • 谢谢。我现在正在使用 HTTPS 基本身份验证(在 Header 中传递凭据)并从 Stack Overflow 添加此代码以删除证书错误:-(void)connection:(NSURLConnection *)connection willSendRequestForAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge { if ([[challenge protectionSpace ] authenticationMethod] == NSURLAuthenticationMethodServerTrust) { [[challenge sender] useCredential:[NSURLCredential credentialForTrust:[[challenge protectionSpace] serverTrust]] forAuthenticationChallenge:challenge]; } } 它的工作。我试图弄清楚这是做什么的。
    • @Jasmine 你能把整个代码用于 willSendRequestForAuthenticationChallenge,这样我就有更多的想法了。为你 +1。
    • @DhavalBhadania 我现在正在使用 neilco 的建议。它适用于任何自签名证书的开发。我只是忽略了错误。在证书签署时的生产中,我摆脱了 NSURLRequest (IgnoreSSL) 文件。
    • @Jasmine,感谢您的评论。这真的很有帮助。
    猜你喜欢
    • 1970-01-01
    • 2016-04-08
    • 1970-01-01
    • 2012-04-04
    • 1970-01-01
    • 1970-01-01
    • 2020-09-10
    • 2020-02-22
    • 2012-02-18
    相关资源
    最近更新 更多