首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >NSURLCredentialStorage sharedCredentialStorage不返回有效的凭据

NSURLCredentialStorage sharedCredentialStorage不返回有效的凭据
EN

Stack Overflow用户
提问于 2012-12-23 22:31:31
回答 1查看 1.4K关注 0票数 0

我对sharedCredentialStorage有点问题。它不会返回预期的凭据,即使拥有保护空间的所有参数(主机、端口...)和我之前设置的一样。

设置defaultCredential的代码:

代码语言:javascript
复制
NSURLCredential* credential = [NSURLCredential credentialWithUser:[self.username text] password:[self.password text] persistence:NSURLCredentialPersistencePermanent];
NSURLProtectionSpace *protectionSpace = [[NSURLProtectionSpace alloc]initWithHost:@"localhost" port:0 protocol:NSURLProtectionSpaceHTTP realm:nil authenticationMethod:NSURLAuthenticationMethodDefault];
[[NSURLCredentialStorage sharedCredentialStorage] setDefaultCredential:credential forProtectionSpace:protectionSpace];

运行这段代码并转到gdb窗口后,allCredentials (位于sharedCredentialStorage)的结果如下:

代码语言:javascript
复制
(lldb) po [[NSURLCredentialStorage sharedCredentialStorage] allCredentials]
(id) $10 = 0x07546d60 {
    "<NSURLProtectionSpace: 0x7550530>" =     {
        lauro = "<NSURLCredential: 0x7573490>: lauro";
    };
}

这意味着凭据已正确添加到sharedCredentialStorage。

但当http请求操作发生时,弹出身份验证challange,并运行以下代码:

代码语言:javascript
复制
credential = [[NSURLCredentialStorage sharedCredentialStorage] defaultCredentialForProtectionSpace:[challenge protectionSpace]];

不会向凭据变量返回任何内容。你知道为什么会发生这种事吗?

gdb的输出结果如下:

代码语言:javascript
复制
(lldb) po [[[[NSURLCredentialStorage sharedCredentialStorage] allCredentials] allKeys] objectAtIndex:0]
(id) $13 = 0x0754c1b0 <NSURLProtectionSpace: 0x754c1b0>
(lldb) po [[[[[NSURLCredentialStorage sharedCredentialStorage] allCredentials] allKeys] objectAtIndex:0] host]
// Host for the credential @ sharedCredentialStorage
(id) $14 = 0x0a8b8000 localhost
(lldb) po [[[[[NSURLCredentialStorage sharedCredentialStorage] allCredentials] allKeys] objectAtIndex:0] port]
// Port for the credential @ sharedCredentialStorage (0 matches any)
(id) $15 = 0x00000000 <nil>
(lldb) po [[[[[NSURLCredentialStorage sharedCredentialStorage] allCredentials] allKeys] objectAtIndex:0] realm]
// Realm for the credential @ sharedCredentialStorage (nil matches any)
(id) $16 = 0x00000000 <nil>
(lldb) po [[[[[NSURLCredentialStorage sharedCredentialStorage] allCredentials] allKeys] objectAtIndex:0] authenticationMethod]
// Authentication method for the credential @ sharedCredentialStorage (this gets converted to HTTP basic)
(id) $17 = 0x0159c094 NSURLAuthenticationMethodDefault
(lldb) po [[[[[NSURLCredentialStorage sharedCredentialStorage] allCredentials] allKeys] objectAtIndex:0] protocol]
// Protocol for the credential @ sharedCredentialStorage
(id) $18 = 0x0159b504 http
(lldb) po [[challenge protectionSpace] host]
// Host for the credential @ challange
(id) $19 = 0x0754e6c0 localhost
(lldb) po [[challenge protectionSpace] port]
// Port for the credential @ challange
(id) $20 = 0x00000050 [no Objective-C description available]
// Realm for the credential @ challange
(lldb) po [[challenge protectionSpace] realm]
(id) $21 = 0x01bde844 <object returned empty description>
// Authentication method for the credential @ challange
(lldb) po [[challenge protectionSpace] authenticationMethod]
(id) $22 = 0x0159c094 NSURLAuthenticationMethodDefault

// And finally, the check again to see if we have credentials @ sharedCredentialStorage
(lldb) po [[NSURLCredentialStorage sharedCredentialStorage] allCredentials]
(id) $25 = 0x0756f2b0 {
    "<NSURLProtectionSpace: 0x7574a80>" =     {
        lauro = "<NSURLCredential: 0x7574b00>: lauro";
    };
}

还有一件事,即使我为realm使用了一些字符串(并在我的REST web服务中正确地设置了它),它也不能工作。

EN

回答 1

Stack Overflow用户

发布于 2013-01-23 15:41:19

当您收到质询时,保护空间中提供的端口可能是80 (或443)。当您存储凭据时,您指定的端口为0。将其设置为正确的端口( http为80,https为443,除非您知道您连接的服务器没有使用默认端口-您的lldv输出似乎显示端口50?)试一试。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14011778

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档