-1

我需要:

1)针对表单身份验证服务进行身份验证

2) 保存 cookie

3) 使用 cookie 调用 web 服务

我正在努力让它发挥作用。有没有人有这方面的运气?这似乎是一个非常常见的操作。

这是我的代码:

点击 auth URL 并获取 cookie:

- (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 调用 auth 服务:

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

编辑:对不起,我遇到的问题是我只用会话 id 得到了一个准系统 cookie,但在服务器端,cookie 看起来很好。谁能验证我的 cookie 抓取代码没有问题?我已经尝试了很多变体并且遇到了同样的问题。谢谢!

4

2 回答 2

1

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

于 2012-09-10T23:40:31.283 回答
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 中获取 cookie

- ( 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];

我希望这对拥有类似身份验证系统的人有所帮助。

于 2012-09-19T18:53:33.303 回答