需要有关 NSURLCredential initWithTrust 的帮助:

发布于 2024-11-18 02:59:55 字数 2549 浏览 2 评论 0原文

在过去的几个小时里,我一直在用头撞墙,却找不到任何解决方案。 我正在开发一个应用程序来学习如何使用 JSON,因此我只是构建一个简单的应用程序,它首先只返回我在 GitHub 上的所有存储库的列表。 我能够毫无问题地从 NSURLConnection 返回的数据中提取 JSON 响应,不幸的是,当我尝试访问任何需要身份验证的内容时,响应始终为 { 消息=“未找到”; } 如果我发送请求只是获取有关我的用户名的公共信息,我会收到包含所有正确信息的正确响应,这样我就知道我正在与 api.github.com 成功通信。 首先开始我使用的请求:

    -(IBAction)retriveRepos:(id)sender
{
    responseData = [[NSMutableData data] retain];
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"https://api.github.com/user/repos"]];
    [request setHTTPMethod:@"GET"];
    [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
    [[NSURLConnection alloc] initWithRequest:request delegate:self];
}

- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge 被调用时,身份验证方法返回为 NSURLAuthenticationMethodServerTrust (我觉得很奇怪,因为我觉得我应该提供用户名和密码来登录)。根据Apple的文档,我需要使用initWithTrust:创建我的NSURLCredential。我在 Google 上搜索答案,最后找到了一些地方,说只需从传入的 NSURLAuthenticationChallengeprotectionSpace 中获取 serverTrust 即可。这根本没有任何影响的迹象,事实上,我的 NSURLCredential 看起来几乎什么也没有。

if ([[challenge protectionSpace] authenticationMethod] == NSURLAuthenticationMethodServerTrust) {
        NSLog(@"challenge.protectionSpace.serverTrust: %@", challenge.protectionSpace.serverTrust); // This logs "challenge.protectionSpace.serverTrust: <SecTrust 0x10340b2e0 [0x7fff7cc12ea0]>"
        NSURLCredential *credential = [[NSURLCredential alloc] initWithTrust:challenge.protectionSpace.serverTrust];
        NSLog(@"credential: %@", credential); // Always logs something like "credential: <NSURLCredential: 0x1002d2f20>: (null)"
        NSURLCredential *otherCredential = [[NSURLCredential alloc] initWithUser:@"user" password:@"password" persistence:NSURLCredentialPersistencePermanent];
        NSLog(@"otherCredential: %@", otherCredential); // This logs otherCredential: <NSURLCredential: 0x1018d0840>: user"
        [[challenge sender] useCredential:credential forAuthenticationChallenge:challenge];
    }

我还使用了更广泛的日志记录,表明凭证确实存在(起初我认为它根本没有被创建),但它没有任何属性设置证书: (无效的) 有密码: 0 身份:(空) 密码:(空) 。我只能假设问题出在我的身份验证中,但我严格遵循了我发现的示例。我正在使用 Lion 的 GM 预览版,所以我想我可能发现了一个错误,或者 GitHub API 中存在一些错误,但考虑到我的技能水平,我发现我更有可能是一个正在寻找的白痴答案就在我面前。 如果有人知道一个好的指南,它实际上可以引导您完成身份验证的所有步骤(最好是所有样式),而不是严重缺乏 Apple URL 加载指南,该指南只说使用一种方法,但没有说明 在哪里SecTrustRef 来自它,我们将不胜感激。

I have been beating my head against the wall for the last few hours and cannot find even a glimmer of a solution anywhere.
I am working on an app to learn how to work with JSON so I am just building a simple app that at first just returns a list of all of my repos on GitHub.
I am able to extract the JSON response from the data returned by NSURLConnection with no problem, unfortunately when I try to access anything that requires authentication the response is always {
message = "Not Found";
}

If I send a request to just get public information about my username I get back the correct response with all the correct info so I know that I am successfully communicating with api.github.com.
First off to start the request I use:

    -(IBAction)retriveRepos:(id)sender
{
    responseData = [[NSMutableData data] retain];
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"https://api.github.com/user/repos"]];
    [request setHTTPMethod:@"GET"];
    [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
    [[NSURLConnection alloc] initWithRequest:request delegate:self];
}

When - (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge gets called the authentication method comes back as NSURLAuthenticationMethodServerTrust (which I find strange since I feel like I should be supplying a username and password to login). According to Apple's docs I need to create my NSURLCredential with initWithTrust:. I Googled for answers and finally found a few places that said to just take the serverTrust from the protectionSpace of the NSURLAuthenticationChallenge that was passed in. Doing this shows no signs of having any effect at all, in fact it looks like my NSURLCredential has pretty much nothing in it.

if ([[challenge protectionSpace] authenticationMethod] == NSURLAuthenticationMethodServerTrust) {
        NSLog(@"challenge.protectionSpace.serverTrust: %@", challenge.protectionSpace.serverTrust); // This logs "challenge.protectionSpace.serverTrust: <SecTrust 0x10340b2e0 [0x7fff7cc12ea0]>"
        NSURLCredential *credential = [[NSURLCredential alloc] initWithTrust:challenge.protectionSpace.serverTrust];
        NSLog(@"credential: %@", credential); // Always logs something like "credential: <NSURLCredential: 0x1002d2f20>: (null)"
        NSURLCredential *otherCredential = [[NSURLCredential alloc] initWithUser:@"user" password:@"password" persistence:NSURLCredentialPersistencePermanent];
        NSLog(@"otherCredential: %@", otherCredential); // This logs otherCredential: <NSURLCredential: 0x1018d0840>: user"
        [[challenge sender] useCredential:credential forAuthenticationChallenge:challenge];
    }

I used more extensive logging as well that showed that credential did in fact exist (at first I thought it hadn't been created at all) but that it had none of it's attributes set certificates: (null)
hasPassword: 0
identity: (null)
password: (null)
. I can only assume that the problem is in my authentication but I followed the examples I found to the letter. I am using the GM preview of Lion, so I guess it is possible I found a bug, or that there is some bug in the GitHub API, but given my skill level I find it much more likely that I am an idiot that is looking over the answer sitting right in front of me.
If anyone knows of a good guide that actually walks you through all the steps of authenticating (preferably in all the styles) rather than the severely lacking Apple URL loading guide that just says to use a method but doesn't say where the SecTrustRef comes from it would be greatly appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

春夜浅 2024-11-25 02:59:55

NSURLAuthenticationMethodServerTrust 是您在协商 SSL 连接时将得到的信息。它使您有机会评估服务器的信任度(反之亦然)。 技术说明 2232 对此进行了详细描述。您的代表需要处理它,技术说明链接到示例代码高级 URL 连接的源,它应该向您展示如何应对该挑战。

不要绝望,如果有来自您的服务器的 HTTP 身份验证步骤,您将看到代理稍后收到该消息。

NSURLAuthenticationMethodServerTrust is what you will get back as it negotiates an SSL connection. It's giving you the opportunity to evaluate the trust of the server (or vice versa). Technote 2232 describes this in a bit of detail. Your delegate needs to handle it, the technote links to the source for the sample code Advanced URL Connections which should show you how to respond to that challenge.

Don't despair, if there is an HTTP authentication step coming from your server, you will see the delegate get that later.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文