0

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 要求操作が発生し、認証チャレンジが発生し、次のコードが実行されると:

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

もう1つ、レルムに文字列を使用しても(そしてREST Webサービスでも適切に設定しても)、機能しません。

4

1 に答える 1

0

問題が発生した場合、保護スペースで提供されるポートはおそらく 80 (または 443) です。認証情報を保存するときは、ポート 0 を指定します。正しいポートに設定します (http の場合は 80、https の場合は 443、接続先のサーバーがデフォルトのポートを使用していないことがわかっている場合を除きます。lldv の出力は次のようになります)。 show port 50?) 試してみてください。

于 2013-01-23T07:41:18.647 に答える