我在IIS上托管了一个带有Windows身份验证的网站。我试图在我的一个iPhone Web应用程序中访问它。目前我正在使用此代码,但它无法正常工作。

NSString * authString = [[[NSString stringWithFormat:@"%@:%@",@" myusername",@" mypassword"] dataUsingEncoding:NSUTF8StringEncoding] base64Encoding];

authString = [NSString stringWithFormat: @"Basic %@", authString];

**[requestObj setValue:authString forHTTPHeaderField:@"Authorization"];**

我的网络应用程序托管了Windows身份验证。但在这里我使用的是基本的。任何人都可以发布正确的http标头。

谢谢..

有帮助吗?

解决方案

我认为主要区别在于您需要指定要进行身份验证的域以及用户名和密码。这样的事情应该有效。为了简洁起见,我使用了同步请求,理想情况下,您应该使用ASINetworkQueue或NSOperationQueue来执行请求。

NSString *username = @"test";
NSString *password = @"test";
NSString *domain = @"test";
NSURL *url = [NSURL URLWithString:@"http://myurl"];
ASIHTTPRequest *request = [[[ASIHTTPRequest alloc] initWithURL:url] autorelease];
[request setUseSessionPersistence:YES];
[request setUsername:username];
[request setPassword:password];
[request setDomain:domain];
[request start];
if ([request error]) {
    if ([[request error] code] == ASIAuthenticationErrorType) {
        //Authentication failed
    }
} else {
    NSLog([request responseString]);
}

我没有访问Windows服务器来测试这个,但我过去测试过NTLM所以应该可以工作......:)

其他提示

Windows身份验证(NTLM)并不像基本身份验证那么简单。 NTLM需要多个webrequest来协商安全性,因此您不能发送静态HTTP标头来登录。

您可以使用第三方 ASIHTTPRequest库来执行NTLM over HTTP身份验证。

我不是100%肯定它支持NTLM身份验证,但您是否调查了NSUrlConnection上的 connection:didReceiveAuthenticationChallenge 方法?

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top