2012-09-10 84 views
-1

我需要:iOS的窗体身份验证

1)对验证表单验证服务

2)保存关闭饼干饼干与

3)调用Web服务,我有一段时间试图让这个工作。有没有人有这样的运气?这似乎是一个非常常见的操作。

这里是我的代码:

命中验证网址,并获得饼干:

- (IBAction)buttonGetCookieTap:(id)sender { 
    [[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookieAcceptPolicy:NSHTTPCookieAcceptPolicyAlways]; 
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:self.cookieURL]; 
    [request setHTTPShouldHandleCookies:YES]; 
    [request setHTTPMethod:@"POST"]; 
    self.urlConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self]; 
} 

- (void)connection: (NSURLConnection *)connection didReceiveResponse: (NSURLResponse *)response 
{ 
    NSHTTPURLResponse *HTTPResponse = (NSHTTPURLResponse *)response; 
    NSDictionary *fields = [HTTPResponse allHeaderFields]; 
    self.cookie = [fields valueForKey:@"Set-Cookie"]; 
} 

- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge 
{ 
    NSURLCredential *credential = [NSURLCredential credentialWithUser:@"blah" 
                  password:@"blah" 
                  persistence:NSURLCredentialPersistenceForSession]; 
    [[challenge sender] useCredential:credential forAuthenticationChallenge:challenge]; 
} 

现在调用验证服务与cookie:

- (IBAction)buttonCallServiceTap:(id)sender { 
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:self.dataURL]; 
    [request addValue:self.cookie forHTTPHeaderField:@"Cookie"]; 
    self.urlConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self]; 
} 

编辑:对不起,我有问题是,我得到一个纯粹的cookie,只有会话ID,但在服务器端cookie看起来很好。任何人都可以验证我的Cookie抓取代码没有问题吗?我已经尝试了很多变化,并且有同样的问题。谢谢!

回答

1

我写了一个堆栈溢出问题here的答案,它提供了用于检查应用程序Cookie的示例代码。该线程中的其他人提供了获取应用程序cookie详细信息的附加代码。您可以从那里开始,以帮助解决身份验证问题。

1

我想出了如何做到这一点,我不得不为HTTP身体建立一条肥皂消息。

NSString *soapFormat = [NSString stringWithFormat:@"<?xml version='1.0' encoding='UTF-8'?>\n" 
          "<soap:Envelope\n" 
           "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n" 
           "xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"\n" 
           "xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">\n" 
           "<soap:Body>\n" 
            "<Login xmlns=\"http://asp.net/ApplicationServices/v200\">\n" 
             "<username>myusername</username>\n" 
             "<password>mypassword</password>\n" 
            "</Login>\n" 
           "</soap:Body>\n" 
          "</soap:Envelope>\n"]; 
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc]initWithURL:self.authURL]; 
    [request addValue:@"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"]; 
    [request addValue:@"http://asp.net/ApplicationServices/v200/AuthenticationService/Login" forHTTPHeaderField:@"SOAPAction"]; 
    [request addValue:[NSString stringWithFormat:@"%d",[soapFormat length]] forHTTPHeaderField:@"Content-Length"]; 
    [request setHTTPMethod:@"POST"]; 
    [request setHTTPBody:[soapFormat dataUsingEncoding:NSUTF8StringEncoding]]; 
    self.authConnection = [[NSURLConnection alloc]initWithRequest:request delegate:self startImmediately:YES]; 

我可以从didReceiveResponse

- (void)connection: (NSURLConnection *)connection didReceiveResponse: (NSURLResponse *)response 
{ 
    if (self.currentCookies == nil) { 
     NSHTTPURLResponse *HTTPResponse = (NSHTTPURLResponse *)response; 
     self.currentCookies = [NSHTTPCookie cookiesWithResponseHeaderFields:[HTTPResponse allHeaderFields] forURL:[NSURL URLWithString:@""]]; 
     NSLog(@"COOKIE: %@",((NSHTTPCookie*)[self.currentCookies objectAtIndex:1]).value); 
    } 
} 

抢饼干然后我使用cookie从Web服务得到JSON数据。

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:self.dataURL]; 
    [request setHTTPMethod: @"GET"]; 
    NSString *cook = [NSString stringWithFormat:@".ASPXAUTH=%@",((NSHTTPCookie*)[self.currentCookies objectAtIndex:1]).value]; 
    [request addValue:cook forHTTPHeaderField:@"Cookie"]; 
    [request setHTTPShouldHandleCookies:NO]; 
    self.dataConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self]; 

我希望这可以帮助有类似认证系统的人。