gpt4 book ai didi

iOS 使用凭据 : forAuthenticationChallenge is not using given credentials

转载 作者:太空宇宙 更新时间:2023-11-03 13:39:14 24 4
gpt4 key购买 nike

我有一些不寻常的用例,苹果的 useCredential:forAuthenticationChallenge 无法应对(或者可能是我?)。

问题是,我正在建立两个连续的连接(第二个连接在第一个连接完成后调用),每个连接都有不同的凭据(不同的客户端证书):

[[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:YES];

请求的 URL 是:

https://my.url.com/ws/service1
https://my.url.com/ws/service2

然后我实现委托(delegate)的方法:

- (void)connection:(NSURLConnection *)connection willSendRequestForAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge {
if ([challenge previousFailureCount] != 0) {
[[challenge sender] cancelAuthenticationChallenge:challenge];
}

NSURLCredential *newCredential = nil;
NSURLProtectionSpace *protectionSpace = [challenge protectionSpace];

if ([protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodClientCertificate]) {
if (isFirstRequest) {
[[challenge sender] useCredential:credentials1 forAuthenticationChallenge:challenge];
} else {
[[challenge sender] useCredential:credentials2 forAuthenticationChallenge:challenge];
}
}
}

凭据是根据身份创建的(持久性标志没有任何作用):

NSURLCredential* credential = [NSURLCredential credentialWithIdentity:identityRef
certificates:certificates
persistence:NSURLCredentialPersistenceNone];

我确定这两个凭证都是有效的,并且它们应该可以正常工作。但只有第一次调用成功。之后,当发出第二个请求时,将调用 willSendRequestForAuthenticationChallenge 方法并调用正确的 useCredential:forAuthenticationChallange 但它不会更改凭据,无论如何都会使用第一个!

来自documentation的 - useCredential:forAuthenticationChallenge:

Attempt to use a given credential for a given authentication challenge. (required)

This method has no effect if it is called with an authentication challenge that has already been handled.

有没有办法检查质询是否已被处理或重置质询以使系统不使用任何缓存的凭据?

我已经尝试删除缓存的凭据,但下面的代码(来自 SO)总是返回零凭据:

- (void)eraseCredentials {
NSString *urlString = @"my.url.com";
NSURLCredentialStorage *credentialsStorage = [NSURLCredentialStorage sharedCredentialStorage];
NSDictionary *allCredentials = [credentialsStorage allCredentials];

if ([allCredentials count] > 0)
{
for (NSURLProtectionSpace *protectionSpace in allCredentials)
{
if ([[protectionSpace host] isEqualToString:urlString])
{
NSDictionary *credentials = [credentialsStorage credentialsForProtectionSpace:protectionSpace];
for (NSString *credentialKey in credentials)
{
[credentialsStorage removeCredential:[credentials objectForKey:credentialKey] forProtectionSpace:protectionSpace];
}
}
}
}
}

注意:我已经尝试过将 .或 # 在 URL 之后,如 TLS Session Cache 中所述或者在 StackOverflow 上,但我认为这不是这里的问题,因为我的 willSendRequestForAuthenticationChallenge 按预期被正确调用了多次...开玩笑说给定的凭据不起作用。

更新请求本身正在构建如下:

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[self resourceURLWithName:name]
cachePolicy:NSURLRequestReloadIgnoringCacheData
timeoutInterval:30];
[request setHTTPShouldHandleCookies:NO];

以前它只是:

    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[self resourceURLWithName:name]];

但行为没有变化。

更新 2: 最后这不是 useCredential:forAuthenticationChallenge: 的问题,而是身份的问题。我用的那个好像还可以,但不是。在 this SO question 中查看我的回答

最佳答案

实现下面的委托(delegate)方法

- (BOOL)connectionShouldUseCredentialStorage:(NSURLConnection *)connection
{
return NO;
}

关于iOS 使用凭据 : forAuthenticationChallenge is not using given credentials,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30256178/

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