当代理质询提供具有Proxy-Authenticate标头的407响应时,弹出“代理验证”对话框

时间:2016-12-08 09:57:52

标签: ios objective-c proxy proxy-authentication

在我的应用程序中,我为NSURLSessionConfiguration对象配置了代理,以便App可以使用代理。当代理需要身份验证时,应用程序将通过委托方法请求用户名和密码" URLSession:task:didReceiveChallenge:completionHandler:"并确保请求可以继续。

HTTP请求中的正常,但HTTPS请求会弹出一个对话框,说明需要进行代理身份验证,并允许用户选择执行此操作"稍后"或直接转到系统设置。

此外,甚至在委托方法" didReceiveChallenge"之前,对话框就会弹出。在上面的NSUrlSession中,应用程序在iOS显示之前没有机会提供凭据。

有没有其他人看到这个并知道如何解决这个问题?

代理服务器响应标题

需要HTTP / 1.1 407代理身份验证

内容类型:text / html

X-Squid-Error:ERR_CACHE_ACCESS_DENIED 0

代理 - 身份验证:基本领域="基本"

代理 - 身份验证:摘要领域="摘要", nonce =" xxxxxxxxxxxxxxxxxxxxxxxxxxx",qop =" auth",stale = false

X-Cache:来自proxy.xxxx.com的MISS

Via:1.1 proxy.xxxx.com

我的演示代码

    NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration];
config.requestCachePolicy = NSURLRequestReloadIgnoringLocalCacheData;
config.connectionProxyDictionary = @{
                                     @"HTTPEnable" : @(1),
                                     (NSString *)kCFStreamPropertyHTTPProxyHost  : @"proxy.xxxx.com",
                                     (NSString *)kCFStreamPropertyHTTPProxyPort  : @(1234),
                                     @"HTTPSEnable": @(1),
                                     (NSString *)kCFStreamPropertyHTTPSProxyHost :@"proxy.xxxx.com",
                                     (NSString *)kCFStreamPropertyHTTPSProxyPort : @(4321)
                                    };

self.session = [NSURLSession sessionWithConfiguration:config delegate:self delegateQueue:[NSOperationQueue mainQueue]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"https://www.xxxxx.com"] cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:60.0];
NSURLSessionTask  *task =  [self.session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
    NSLog(@"result:%@  error: %@",data, error.description);
}];
[task resume];

#pragma mark - NSURLSessionTaskDelegate

- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)taskdidReceiveChallenge:(NSURLAuthenticationChallenge *)challengecompletionHandler:(void (^)(NSURLSessionAuthChallengeDisposition disposition, NSURLCredential * _Nullable credential))completionHandler{
NSLog(@"task:%@",challenge.protectionSpace.authenticationMethod);
if (challenge.protectionSpace.authenticationMethod == NSURLAuthenticationMethodHTTPDigest) {
    NSURLCredential *cren = [NSURLCredential credentialWithUser:username password:password persistence:NSURLCredentialPersistenceNone];
    if (completionHandler) {
        completionHandler(NSURLSessionAuthChallengeUseCredential,cren);
    }
}

}

1 个答案:

答案 0 :(得分:1)

我找到了解决方案here。似乎这些问题都是错误。而不是使用URLSession:task:didReceiveChallenge:completionHandler:delegate,一个好的解决方法是添加"代理授权&# 34; NSURLSessionConfiguration.Here是基于here的代码:

// 1 - define credentials as a string with format:
//    "username:password"
//
NSString *username = @"USERID";
NSString *password = @"SECRET";
NSString *authString = [NSString stringWithFormat:@"%@:%@",
                        username,
                        password];

// 2 - convert authString to an NSData instance
NSData *authData = [authString dataUsingEncoding:NSUTF8StringEncoding];

// 3 - build the header string with base64 encoded data
NSString *authHeader = [NSString stringWithFormat: @"Basic %@",
                        [authData base64EncodedStringWithOptions:0]];

// 4 - create an NSURLSessionConfiguration instance
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration ephemeralSessionConfiguration];

// 5 - add custom headers, including the Authorization header
[configuration setHTTPAdditionalHeaders:@{
                                          @"Proxy-Authorization": authHeader
                                          }
 ];

// 6 - set proxy dictionary
NSDictionary *proxyDict = @{
                            @"HTTPEnable"  : @1,
                            (NSString *)kCFStreamPropertyHTTPProxyHost  : @"ip",
                            (NSString *)kCFStreamPropertyHTTPProxyPort  : proxyPortNumber,

                            @"HTTPSEnable" : @1,
                            (NSString *)kCFStreamPropertyHTTPSProxyHost : @"ip",
                            (NSString *)kCFStreamPropertyHTTPSProxyPort : proxyPortNumber,

                            };
configuration.connectionProxyDictionary = proxyDict;

// 7 - create an NSURLSession instance
NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration delegate:nil delegateQueue:operationQueue];