2012-09-10 83 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]; 

我希望這可以幫助有類似認證系統的人。