2012-12-23 37 views
0

我遇到sharedCredentialStorage問題。它不會返回預期的憑證,即使保護空間的所有參數(主機,端口,...)與我之前設置的相同。NSURLCredentialStorage sharedCredentialStorage不返回有效憑證

代碼來設置defaultCredential:

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)的結果給出:

(lldb) po [[NSURLCredentialStorage sharedCredentialStorage] allCredentials] 
(id) $10 = 0x07546d60 { 
    "<NSURLProtectionSpace: 0x7550530>" =  { 
     lauro = "<NSURLCredential: 0x7573490>: lauro"; 
    }; 
} 

這意味着憑證已正確添加到sharedCredentialStorage。

但是當http請求的操作情況和認證challange彈出這個代碼是跑:

credential = [[NSURLCredentialStorage sharedCredentialStorage] defaultCredentialForProtectionSpace:[challenge protectionSpace]]; 

沒有返回憑據變量。 你有什麼線索爲什麼發生這種情況?

的GDB的輸出提供:

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

一兩件事,就算我用一些字符串境界(和設置正確,也是在我的REST Web服務),這是行不通的。

回答

0

當您收到挑戰時,保護空間中提供的端口大概是80(或443)。當您存儲憑證時,您將端口設置爲0.將其設置爲正確的端口(80代表http,443代表https,除非您知道要連接的服務器未使用默認端口 - 您的lldv輸出似乎顯示端口50?)並嘗試。