域登录在ios应用程序中影响didReceiveAuthenticationChallenge



我正在开发一个应用程序,该应用程序需要从我们基于iis的网站下载网页。如果我在iPad上登录域进行无线连接,我连接的网站似乎会使用该登录作为我的凭据。但是,如果我没有连接到域,或者我是以无权访问该页面的用户身份连接的,它会触发didReceiveAuthenticationChallenge,否则不会。如果我使用Safari连接到同一个页面,它会要求进行身份验证。我希望每次都能让应用程序进行身份验证。如有任何帮助,我们将不胜感激。

请求页面的代码:

NSError *error = nil;
// assign the cmh url from user prefs
NSURL *url = [NSURL URLWithString:cmhUrl];
// Put that URL into an NSURLRequest
NSURLRequest *req = [NSURLRequest requestWithURL:url];
[req setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
// Create a connection that will exchange this request for data from the URL
connection = [[NSURLConnection alloc] initWithRequest:req
                                             delegate:self
                                     startImmediately:YES];

当您在Safari中进行身份验证时,身份验证令牌会保存在NSHTTPCookies存储中。当您从代码中发出请求时,存储中的所有cookie都会添加到标头中,因此无需再次请求令牌
尝试在提出请求之前清除存储:

NSHTTPCookieStorage *cookieStorage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
for (NSHTTPCookie *cookie in cookieStorage.cookies) {
    [cookieStorage deleteCookie:cookie];
}

希望这能有所帮助。

最新更新