Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cocoa/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone NSURLConnection、NSURLRequest、不可信证书和用户身份验证_Iphone_Cocoa_Nsurlconnection_Nsurlrequest_Nsurlcredential - Fatal编程技术网

Iphone NSURLConnection、NSURLRequest、不可信证书和用户身份验证

Iphone NSURLConnection、NSURLRequest、不可信证书和用户身份验证,iphone,cocoa,nsurlconnection,nsurlrequest,nsurlcredential,Iphone,Cocoa,Nsurlconnection,Nsurlrequest,Nsurlcredential,大家早上好 我一直在尝试编写一个应用程序,该应用程序从需要身份验证的远程Web服务获取一些数据。我的主要问题是这些远程服务器中的大多数(还有很多)没有有效的证书。我已经有了正确的uname和pass(下面)来响应挑战的代码。我遇到的问题是让两个人一起玩。我似乎找不到一种方法可以同时发送质询NSURLCredentials或正确链接回调。当我尝试链接它们时,我无法让我的NSURLRequest调用direceiveauthenticationchallenge两次 任何想法都将不胜感激 身份验证代

大家早上好

我一直在尝试编写一个应用程序,该应用程序从需要身份验证的远程Web服务获取一些数据。我的主要问题是这些远程服务器中的大多数(还有很多)没有有效的证书。我已经有了正确的uname和pass(下面)来响应挑战的代码。我遇到的问题是让两个人一起玩。我似乎找不到一种方法可以同时发送质询
NSURLCredential
s或正确链接回调。当我尝试链接它们时,我无法让我的
NSURLRequest
调用
direceiveauthenticationchallenge
两次

任何想法都将不胜感激

身份验证代码

-(void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
{   
    if(!hasCanceled){
        if ([challenge previousFailureCount] == 0) {
            NSURLCredential *newCredential;
            newCredential=[NSURLCredential credentialWithUser:_username password:_password persistence:NSURLCredentialPersistenceNone];
            [[challenge sender] useCredential:newCredential forAuthenticationChallenge:challenge];
        } 
        else {
            [[challenge sender] cancelAuthenticationChallenge:challenge];
            NSLog(@"Bad Username Or Password");
            badUsernameAndPassword = YES;
            finished = YES;
        }
    }
}

您只能使用该质询的凭据答复
nsurauthenticationchallenge
。您可以通过以下方式确定收到的挑战类型:

[[challenge protectionSpace] authenticationMethod]
可能的值为。如果服务器证书无效,身份验证方法将是
nsurauthenticationmethodservertrust
。在代码中,您应该检查身份验证方法并做出适当的响应

if ([challenge previousFailureCount] > 0) {
    // handle bad credentials here
    [[challenge sender] cancelAuthenticationChallenge:challenge];
    return;
}

if ([[challenge protectionSpace] authenticationMethod] == NSURLAuthenticationMethodServerTrust) {
    // check if the user has previously accepted the certificate, otherwise prompt
} else if ([[challenge protectionSpace] authenticationMethod] == /* your supported authentication method here */) {
    [[challenge sender] useCredential:/* your user's credential */ forAuthenticationChallenge:challenge];
}

如果您没有每次都获得两个身份验证挑战,那么这不是一个错误。您可以在创建凭据时缓存凭据。如果你这样做了,你就不一定会再次得到提示。

工作起来很有魅力。需要正确实现“CanAuthenticationAgainstProtectionSpace”方法才能让我的应用程序调用此方法。但在那之后一切都很好。谢谢