简体   繁体   中英

Pass username and password in URL for authentication

I want ot pass username and password in URL(web service) for user authentication which will return true and false.I'm doing this as following:

NSString *userName = [NSString stringWithFormat:@"parameterUser=%@",txtUserName];
NSString *passWord = [NSString stringWithFormat:@"parameterPass=%@",txtPassword];
NSData *getUserData = [userName dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *getUserLength = [NSString stringWithFormat:@"%d",[getUserData length]];
NSData *getPassData = [passWord dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *getPassLength = [NSString stringWithFormat:@"%d",[getPassData length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc]init];
[request setURL:[NSURL URLWithString:@"http://URL/service1.asmx"]];
[request setHTTPMethod:@"GET"];

Now, I wanted to know How can I pass my username and password in this URL to make request. Could any one please suggest or give some sample code? Thanks.

Try this :-

NSString *userName = [NSString stringWithFormat:@"parameterUser=%@",txtUserName.text];
    NSString *passWord = [NSString stringWithFormat:@"parameterPass=%@",txtPassword.text];
    NSData *getUserData = [userName dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
    NSString *getUserLength = [NSString stringWithFormat:@"%d",[getUserData length]];
    NSData *getPassData = [passWord dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
    NSString *getPassLength = [NSString stringWithFormat:@"%d",[getPassData length]];
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc]init];
    [request setURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://URL/service1.asmx?%@&%@",userName,passWord]]];
    [request setHTTPMethod:@"GET"];

Hope it helps you..

NSString *urlStr = [NSString stringWithFormat:@"http://URL/service1.asmx?%@&%@",userName,passWord];
[request setURL:[NSURL URLWithString:urlStr]];

First off I would not pass a username and password across in a url. You should do this using post.

NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://URL/service1.asmx?"]];

NSString *userName = [NSString stringWithFormat:@"parameterUser=%@",txtUserName];
NSString *passWord = [NSString stringWithFormat:@"parameterPass=%@",txtPassword];
NSString *postString = [NSString stringWithFormat:@"username=%@&password=%@",userName, passWord];
NSData *postData = [NSData dataWithBytes: [postString UTF8String] length: [postString length]];

//URL Requst Object
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:TIMEOUT];
[request setHTTPMethod:@"POST"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody: postData];

This is more secure then passing sensitive data across in a url.

Edit

To get the response you can check this out. NSURLConnection and AppleDoc NSURLConnection

You can use a few different methods to handle the response from the server. You can use NSURLConnectionDelegate

NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
[self.connection start];

along with the delegate call backs:

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {

    NSLog(@"didReceiveData");
    if (!self.receivedData){
        self.receivedData = [NSMutableData data];
    }
    [self.receivedData appendData:data];
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {

    NSLog(@"connectionDidFinishLoading");
    NSString *receivedString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    NSLog(@"receivedString:%@",receivedString);

}

Or you can also use NSURLConnection sendAsynchronousRequest block

NSOperationQueue *queue = [[NSOperationQueue alloc] init];

[NSURLConnection sendAsynchronousRequest:urlRequest queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error)
{
    NSString *receivedString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
        NSLog(@"receivedString:%@",receivedString);

}];

To improve the secure , you may use the Http Basic Authentication . There are answer here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM