0

スタック オーバーフローに関する同様の質問があります。

とにかく信頼できないサーバー証明書を受け入れる私のコードは次のとおりです。

- (BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)space
{
    //We can always attempt to authenticate...
    return YES;
}

- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
{
    if ([[challenge protectionSpace] authenticationMethod] == NSURLAuthenticationMethodServerTrust) {
        [[challenge sender] useCredential:[NSURLCredential credentialForTrust:[[challenge protectionSpace] serverTrust]] forAuthenticationChallenge:challenge];
    } else {
      // Other situation
    }
}

ただし、サイトを信頼するかどうかをユーザーが選択できるように、変更ビューを提示したいと考えています。

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:
[[challenge protectionSpace]host] message:@"Do you trust this site?" 
delegate:self cancelButtonTitle:@"No" 
otherButtonTitles:@"Yes", @"Just once", nil];

[alert show];

どうやってやるの?

4

1 に答える 1

0

例として、チャレンジ オブジェクトをプロパティに保存してから、次のように alertView:clickedButtonAtIndex: デリゲート メソッドを実装できます (これは「一度だけ信頼する」ためのものです)。

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if(buttonIndex == alertView.cancelButtonIndex)
    {
        [[self.challenge sender] cancelAuthenticationChallenge:self.challenge];
    }
    else
    {
        [self.challenge.sender useCredential:[NSURLCredential credentialForTrust:self.challenge.protectionSpace.serverTrust] forAuthenticationChallenge:self.challenge];
        self.challenge = nil;
    }
}

常に信頼したい場合は、サーバー証明書データを保存して比較するために、より複雑なことを行う必要があります。または、サーバーの URL が常に信頼されている必要があることを保存することで、単純で安全でないものにします。これにより、中間者攻撃に対して脆弱になります。

于 2013-12-10T09:55:13.150 に答える