-1

する必要がある:

1) フォーム認証サービスに対して認証する

2) クッキーを保存する

3) Cookie を使用して Web サービスを呼び出す

そして、私はこれを機能させるために非常に苦労しています。これで運が良かった人はいますか?非常に一般的な操作のようです。

これが私のコードです:

認証 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 を使用して認証サービスを呼び出します。

- (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 は問題ないように見えます。私のクッキー取得コードに問題がないことを誰かが確認できますか? 私はこれの多くのバリエーションを試しましたが、同じ問題があります。ありがとう!

4

2 に答える 2

1

ここにStackOverflowの質問に対する回答を書きました。これは、アプリケーションのCookieを調べるためのサンプルコードを提供します。そのスレッドの他の誰かが、アプリ側のCookieの詳細を取得するための追加のコードを提供しました。認証のトラブルシューティングに役立てるために、そこから始めることができます。

于 2012-09-10T23:40:31.283 に答える
1

これを行う方法を理解したので、HTTP 本文の SOAP メッセージを作成する必要がありました。

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 に答える