gpt4 book ai didi

iphone - NSURLConnection、NSURLRequest、不受信任的证书和用户身份验证

转载 作者:行者123 更新时间:2023-12-03 16:06:26 34 4
gpt4 key购买 nike

大家早上好,

我一直在尝试编写一个应用程序,从需要身份验证的远程 Web 服务执行一些 GET 操作。我的主要问题是大多数远程服务器(而且有很多)没有有效的证书。我有code to accept the invalid certificate以及使用正确的 uname 和 pass 响应挑战的代码(如下)。我遇到的问题是让两个人一起玩。我似乎找不到一种方法来发送质询 NSURLCredential 或正确链接回调的方法。当我尝试链接它们时,我无法让我的 NSURLRequest 调用 didReceiveAuthenticationChallenge 两次。

如有任何想法,我们将不胜感激!

身份验证代码...

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

最佳答案

您只能使用该质询的凭据回复 NSURLAuthenticationChallenge。您可以使用以下方法确定您收到的挑战类型:

[[challenge protectionSpace] authenticationMethod]

可能的值为 documented here 。如果服务器证书无效,身份验证方法将为 NSURLAuthenticationMethodServerTrust。在您的代码中,您应该检查身份验证方法并做出适当的响应。

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

如果您每次都没有收到两个身份验证质询,这并不是错误。您可以在创建凭据时缓存它们。如果这样做,系统不一定会再次提示您。

关于iphone - NSURLConnection、NSURLRequest、不受信任的证书和用户身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2949640/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com