身份验证挑战iOS正在提供错误401

时间:2014-02-25 09:00:40

标签: ios sharepoint nsurlconnection windows-authentication

我的应用正在与sharepoint网站进行通信,因此我使用身份验证质询来传递凭据以使用sharepoint进行身份验证。

所有的网络服务都得到了正确的响应,但是当我尝试下载图片时,它会给我401(身份验证失败)错误大约2到5次,然后它会获得成功响应200.

以下是代码:

-(void)getResponseArray:(NSString *)urlstring SoapMessage:(NSString *)soapMessage SoapAction:(NSString *)soapAction1
{
    self.urlString = urlstring;
    NSString *fullsoapMessage = [NSString stringWithFormat:@"<?xml version='1.0' encoding='utf-8'?><soap:Envelope xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'><soap:Body>%@</soap:Body></soap:Envelope>", soapMessage];

    if(IS_DEBUG)
        NSLog(@"The request: %@", fullsoapMessage);

    NSURL *webserviceurl = [NSURL URLWithString:urlstring];
    if (request ==  nil) {
        request = [[NSMutableURLRequest alloc]initWithURL:webserviceurl];
    }else{
        [request setURL:webserviceurl];
    }

    NSString *msglength = [NSString stringWithFormat:@"%d", fullsoapMessage.length];
    [request addValue:@"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
    [request addValue:soapAction1 forHTTPHeaderField:@"SOAPAction"];
    [request addValue:msglength forHTTPHeaderField:@"Content-Length"];
    [request setHTTPMethod:@"POST"];
    [request setHTTPBody:[fullsoapMessage dataUsingEncoding:NSUTF8StringEncoding]];
    [request setTimeoutInterval:10000];

    NSURLConnection *conn = [[NSURLConnection alloc]initWithRequest:request delegate:self];
    if (conn) {
        webData = [[NSMutableData alloc] init];
    }else{
        if(IS_DEBUG)
            NSLog(@"Connection is NULL");
    }
}

-(BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace {

    return YES;

}

-(void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge {

    if ([challenge previousFailureCount] == 0)
    {
        NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];

        NSString *username = [userDefaults valueForKey:KEY_ADMIN_USERNAME];

        NSString *password = [userDefaults valueForKey:KEY_ADMIN_PASSWORD];

        NSURLCredential *newCredential;

        newCredential = [NSURLCredential credentialWithUser:username password:password persistence:NSURLCredentialPersistenceNone];

        [[challenge sender] useCredential:newCredential forAuthenticationChallenge:challenge];

    } else {

        [[challenge sender] cancelAuthenticationChallenge:challenge];

    }
}

请建议我一些解决方案:

  1. 如何使用 NSURLAuthenticationChallenge ,以便sharepoint网站在第一次调用请求时接受凭据,并且在没有因身份验证失败而请求失败的情况下获得响应200。

1 个答案:

答案 0 :(得分:0)

NSMutableURLRequest添加此授权标头。试一试。

NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
NSString *username           = [userDefaults valueForKey:KEY_ADMIN_USERNAME];
NSString *password           = [userDefaults valueForKey:KEY_ADMIN_PASSWORD];
NSString *authStr            = [NSString stringWithFormat:@"%@:%@",strUserName,strAPIKey];
NSData *authData             = [authStr dataUsingEncoding:NSUTF8StringEncoding];
NSString *authValue          = [NSString stringWithFormat:@"Basic %@", [authData base64EncodedStringWithOptions:0]];
[request setValue:authValue forHTTPHeaderField:@"Authorization"];