4

我正在尝试使用. NSURLConnection此演示 Web 服务被记录为需要身份验证(登录名:管理员/密码:管理员)。

编辑:此 Web 服务现在发送身份验证质询,而不是在提出问题时。

此 Web 服务发送身份验证质询,因此我无法使用connection:didReceiveAuthenticationChallenge:委托方法。相反,我NSURLCredential在共享凭证存储中设置了默认值。不幸的是,此默认凭据未被NSURLConnection.

这是我的代码(使用 ARC,在 iOS 5 上测试):

@implementation ViewController
{
    NSMutableData *responseData;
}

- (IBAction) connect:(id)sender
{
    NSString *user = @"Administrator";
    NSString *password = @"Administrator";
    NSURL *nuxeoURL = [NSURL URLWithString:@"http://cmis.demo.nuxeo.org/nuxeo/atom/cmis/"];

    NSURLCredential *credential = [NSURLCredential credentialWithUser:user password:password persistence:NSURLCredentialPersistenceForSession];
    NSString *host = [nuxeoURL host];
    NSNumber *port = [nuxeoURL port];
    NSString *protocol = [nuxeoURL scheme];
    NSURLProtectionSpace *protectionSpace = [[NSURLProtectionSpace alloc] initWithHost:host port:[port integerValue] protocol:protocol realm:nil authenticationMethod:NSURLAuthenticationMethodHTTPBasic];
    [[NSURLCredentialStorage sharedCredentialStorage] setDefaultCredential:credential forProtectionSpace:protectionSpace];

    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:nuxeoURL];
    BOOL manuallyAddAuthorizationHeader = NO;
    if (manuallyAddAuthorizationHeader)
    {
        NSData *authoritazion = [[NSString stringWithFormat:@"%@:%@", user, password] dataUsingEncoding:NSUTF8StringEncoding];
        NSString *basic = [NSString stringWithFormat:@"Basic %@", [authoritazion performSelector:@selector(base64Encoding)]];
        [request setValue:basic forHTTPHeaderField:@"Authorization"];
    }
    NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
    [connection start];
}

- (void) connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    responseData = [NSMutableData data];
}

- (void) connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    [responseData appendData:data];
}

- (void) connectionDidFinishLoading:(NSURLConnection *)connection
{
    NSLog(@"connectionDidFinishLoading:%@", connection);
    NSLog(@"%@", [[NSString alloc] initWithData:responseData encoding:NSISOLatin1StringEncoding]);
}

- (void) connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
    NSLog(@"connection:%@ didFailWithError:%@", connection, error);
}

@end

由于未使用默认凭据,因此我收到的是 html(登录页面)而不是 xml。如果我将manuallyAddAuthorizationHeader变量设置为YES,则授权有效并且我收到 xml。

我的问题是:为什么不NSURLConnection自动使用默认值NSURLCredential

4

3 回答 3

8

在没有身份验证质询的情况下发送默认凭据被认为是不安全的,尤其是在您通过纯 HTTP 进行通信的情况下。HTTP 规范说您可以主动发送凭据,但您通常应该等待身份验证质询。这就是 Cocoa 默认提供的行为。

如果您需要主动发送凭据,则必须自己手动添加标头。您可以自己对其进行 base-64 编码,也可以使用CFHTTP函数为您完成编码,如下所示:

CFHTTPMessageRef dummyRequest =
    CFHTTPMessageCreateRequest(
        kCFAllocatorDefault,
        CFSTR("GET"),
        (CFURLRef)[urlRequest URL],
        kCFHTTPVersion1_1);
CFHTTPMessageAddAuthentication(
    dummyRequest,
    nil,
    (CFStringRef)username,
    (CFStringRef)password,
    kCFHTTPAuthenticationSchemeBasic,
    FALSE);
authorizationString =
    (NSString *)CFHTTPMessageCopyHeaderFieldValue(
        dummyRequest,
        CFSTR("Authorization"));
CFRelease(dummyRequest);

然后只需authorizationString将.AuthorizationNSURLRequest

(代码从https://stackoverflow.com/a/509480/100478公然复制,虽然我自己写过很多次类似的代码。)

于 2012-02-21T14:48:21.103 回答
3

由于 HTTP 支持许多不同的身份验证方法(Basic、Digest、Kerberos 等),NSURLConnection因此在收到来自服务器的身份验证质询之前无法发送凭据,因为它不知道如何发送它们。

于 2012-02-21T14:50:40.840 回答
0

虽然 BJ 的答案可能会在客户端解决您的问题,但真正的问题是服务器。在内部 NSURLConnection 将使用它自己的委托方法connection:didReceiveAuthenticationChallenge:和其他身份验证方法。这是它寻找可以应用的凭证的地方NSURLCredentialStorage。在您的情况下,这似乎没有发生。对于要调用的这些方法,服务器不仅必须提供 40x HTTP 状态代码,而且还必须包含 WWW-Authenticate 标头。这告诉 NSURLConnection 尝试回答身份验证质询。您的服务器可能不包含此内容,这就是客户端不发送凭据的原因。

于 2013-07-25T08:04:25.383 回答