ios https和基本身份验证和发布请求

标签 ios post https basic-authentication

我正在努力让上述三个正常工作,但有些东西没有点击。具体来说,身份验证请求在应该触发时并未触发。根据我在这里阅读的内容,相关文章是:

- (BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace
{
    NSLog(@"protection space");
    return [protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust];
}

- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
{
    NSLog(@"auth challenge");
    NSInteger count = [challenge previousFailureCount];
    if (count > 0)
    {
        NSLog(@"count > 0");
        NSObject<ServiceDelegate> *delegate = [currentRequest delegate];
        [[challenge sender] cancelAuthenticationChallenge:challenge];
        if ([delegate respondsToSelector:@selector(wrapperHasBadCredentials:)])
        {
            [delegate rbService:self wrapperHasBadCredentials:self];
        }
        return;
    }

    NSArray *trustedHosts = [[NSArray alloc] initWithObjects:@"myserver", nil];    
    if ([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust])
    {
        NSLog(@"server trust");
        if ([trustedHosts containsObject:challenge.protectionSpace.host])
        {
            [challenge.sender useCredential:[NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust] forAuthenticationChallenge:challenge];
        }
        [challenge.sender continueWithoutCredentialForAuthenticationChallenge:challenge];
    }
    else
    {
        NSLog(@"credentials");
        NSURLCredential* credential = [[[NSURLCredential alloc] initWithUser:@"xxx" password:@"xxx" persistence:NSURLCredentialPersistenceForSession] autorelease];
        [[challenge sender] useCredential:credential forAuthenticationChallenge:challenge];
    }

}

目标服务器设置了两个 URLS,一个 HTTPS 和一个 HTTP,两者都使用基本身份验证提示输入用户名/密码。我已经使用 firefox 检查了目标服务器,一切似乎都像宣传的那样工作。目标服务器使用不受信任的证书,但我认为我已经在代码中解决了这个问题。也许不是。

应用中的具体行为:

当使用 HTTP 时,日志显示为:
日志-保护空间
(然后返回 401 代码)

使用 HTTPS 时:
日志-保护空间
日志 - 身份验证挑战
日志 - 服务器信任
日志-保护空间
(然后返回一个 401 代码)

在第一个实例中,它到达 canAuthenticate... 部分,返回,但随后不挑战身份验证,并返回 401 响应。

在第二个实例中,它执行了所有这些操作,实际上进行了质询,然后再次转到 canAuthenticate... 部分,返回并返回 401 响应。

请记住,请求是一个 POST 请求,包含 header 和 HTTPBody。身份验证不包含在 header 中(我宁愿不这样做),但如果没有其他解决方案,我会尝试那样做。

一如既往,非常感谢您的帮助。这是无价的。

最佳答案

看起来这里的答案必须是将身份验证与 POST 请求一起发送。我在想挑战/响应过程会以某种方式自行将这些 header 添加到请求中,但显然不会。

关于ios https和基本身份验证和发布请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7369056/

相关文章:

ios - 切换相机权限会导致崩溃

ios - 临时打开新ViewController时保存值

不允许使用Django方法(POST)

ios - Cordova:我可以在 div 中显示 native 应用程序组件吗?

android - 线性渐变在移动网络浏览器中不起作用

c# - 将各种数据一起发布的最佳方式(Web API)

PHP POST 通过 AJAX 循环通过 javascript 发送对象文字

python - 如何仅在 Heroku https 上制作 python?

java - 将自签名证书导入 Docker 的 JRE cacert 服务无法识别

java - 如何更新我的 Android 应用程序中的 SSL 证书?