HTTPS基本身份验证 [英] HTTPS Basic authentication

查看:109
本文介绍了HTTPS基本身份验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用用户名和密码对用户进行身份验证,但是在didReceiveAuthenticationChallenge方法中,即使我输入了错误的用户名或密码,它也不会进入else.这是我的代码:

I try to authenticate user with username and password But in the didReceiveAuthenticationChallenge method it doesn't go to an else even if i put a wrong username or password. Here is my code:

- (IBAction)clickOK:(id)sender {

if ([[usernameField text] length] < 1 || [[passwordField text] length] < 1) {
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Login" message:@"Neither the username nor password may be empty!" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [alert show];
    [alert release];
    return;
}
NSURL *myURL = [NSURL URLWithString:@"https://www.freelancer.com/users/login.php"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:myURL
                                                            cachePolicy:NSURLRequestReloadIgnoringLocalCacheData
                                            timeoutInterval:10];    
NSLog(@"ispost");
[request setHTTPMethod:@"POST"];
NSString* str = [NSString stringWithFormat:@"%@:%@", usernameField.text, passwordField.text];
[request setHTTPBody:[str dataUsingEncoding:NSUTF8StringEncoding]];

NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
if (!theConnection) {
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Network Error" message:@"Could not login!" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [alert show];
    [alert release];
    return;
}

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

return YES; 
}
- (void) connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge{

if ([challenge previousFailureCount] == 0) {
    NSLog(@"received authentication challenge");
    NSURLCredential *newCredential;
    newCredential = [NSURLCredential credentialWithUser:usernameField.text
                                               password:passwordField.text
                                            persistence:NSURLCredentialPersistenceNone];
    NSLog(@"credential created");
    [[challenge sender] useCredential:newCredential
           forAuthenticationChallenge:challenge];
     NSLog(@"responded to authentication challenge"); 
} else {
    [[challenge sender] cancelAuthenticationChallenge:challenge];
    NSLog(@"Invalid Username or Password");

    UIAlertView *userNameAlert = [[UIAlertView alloc]initWithTitle:@"Alert" message:@"Invalid credentials. Please check your login details  !" delegate:self cancelButtonTitle:nil otherButtonTitles:@"OK",nil];
    [userNameAlert show];

}
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
NSLog(@"Got response: %@", response);

responseData = [[NSMutableData alloc] init];
 }

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

return;
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
[responseData release];
[connection release];
// Show error message
NSLog(@"Connection failed! Error - %@ %@",
      [error localizedDescription],
      [[error userInfo] objectForKey:NSURLErrorFailingURLStringErrorKey]);
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
// Use responseData
NSLog(@"Succeeded! Received %d bytes of data",[responseData length]);
[responseData release];
[connection release];
}

推荐答案

像下面这样更新didReceiveAuthenticationChallenge方法:

Update the didReceiveAuthenticationChallenge method like this :

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

if ([challenge previousFailureCount] == 0) {
    NSLog(@"received authentication challenge");
        NSURLCredential *newCredential;
    newCredential = [NSURLCredential credentialWithUser:usernameField.text
                                               password:passwordField.text
                                            persistence:NSURLCredentialPersistenceNone];
    NSLog(@"credential created");
    [[challenge sender] useCredential:newCredential forAuthenticationChallenge:challenge];
    NSLog(@"responded to authentication challenge");    
} else {
    NSLog(@"previous authentication failure");
    UIAlertView *userNameAlert = [[UIAlertView alloc]initWithTitle:@"Alert" message:@"Invalid credentials. Please check your login details  !" delegate:self cancelButtonTitle:nil otherButtonTitles:@"OK",nil];
    [userNameAlert show];

}

以及对此的clickOk方法:

and the clickOk method to this :

- (IBAction)clickOK:(id)sender {

        if ([[usernameField text] length] < 1 || [[passwordField text] length] < 1) {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Login" message:@"Neither the username nor password may be empty!" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
            [alert show];
            return;
        }
        NSURL *myURL = [NSURL URLWithString:@"http://test.webdav.org/auth-basic/"];
        NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:myURL
                                                               cachePolicy:NSURLRequestReloadIgnoringLocalCacheData
                                                    timeoutInterval:10];    
        NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
        [theConnection start];
    }
 }

这篇关于HTTPS基本身份验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆