簡體   English   中英

NTLM身份驗證Xcode

[英]NTLM Authentication Xcode

我正在嘗試使用UIWebview進行NTLM身份驗證。 我已經研究過,並且這樣寫。 但是在我的NSLog中,我僅看到“獲得身份驗證挑戰”,但沒有看到“通過nsurlconnection收到的響應” 請幫我。 我堅持了超過5天。 我正在使用ios 6.1進行測試。 感謝您的幫助:)

-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request   navigationType:(UIWebViewNavigationType)navigationType{


if([request.URL.host isEqualToString:@"42.61.46.2"])
{
    NSURLConnection* connection = [[NSURLConnection alloc] initWithRequest:request   delegate:self];
    [connection start];

    NSLog(@"%@",request);
    return NO;
}


self.lbl_error.hidden = YES; //hide error message label
return YES;
}

- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
{
    NSLog(@"got auth challange");

if ([challenge previousFailureCount] == 0)
{
    NSURLProtectionSpace *protectionSpace = [[NSURLProtectionSpace alloc]
                                             initWithHost: @"42.61.46.2"
                                             port: 80
                                             protocol: @"http"
                                             realm: nil authenticationMethod : NSURLAuthenticationMethodNTLM];                                                   

    [[NSURLCredentialStorage sharedCredentialStorage] setDefaultCredential:    [NSURLCredential credentialWithUser:@"example" password:@"example"     persistence:NSURLCredentialPersistenceForSession] forProtectionSpace:protectionSpace];



}
else
{
    [[challenge sender] cancelAuthenticationChallenge:challenge];
}

}


- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
NSLog(@"received response via nsurlconnection");



NSURL* url = [NSURL URLWithString:@"http://42.61.46.2"]; //create a NSURL object

if(activeWebView == nil)
{
    //If there is no activeWebview i.e. no webview in the collection, create a new one and     show it

    [self addWebViewWithURL:url];
}
else
    {
           NSString *url = @"http://42.61.46.2";
    NSURLRequest *urlRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:url]];

    [activeWebView loadRequest:urlRequest];
    }
}



- (BOOL)connectionShouldUseCredentialStorage:(NSURLConnection *)connection
{
    return NO;
}

通過為您的didReceiveAuthenticationChallange使用此方法,它對我有用:

- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:    (NSURLAuthenticationChallenge *)challenge
{
    NSLog(@"got auth challange");

    if ([challenge previousFailureCount] ==0)
    {
        [[challenge sender]  useCredential:[NSURLCredential credentialWithUser:@"username" password:@"password" persistence:NSURLCredentialPersistenceForSession] forAuthenticationChallenge:challenge];


    }
    else
{
    NSLog(@"Did cancelAuthenticationChallenge");
    [[challenge sender] cancelAuthenticationChallenge:challenge];
}

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM