2016-04-20 12 views
1

は、私は次のような問題があります。クライアント証明書を使用してUIWebViewを認証する方法は?私が正しくウェブサイトをロードしている<code>UIWebView</code>を持っていますが、サーバーがあまりにもクライアント(<code>UIWebView</code>)から認証を望んでいる</p> <p>:

shouldStartLoadWithRequest:

-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType (UIWebViewNavigationType)navigationType; 
{ 
    if(![self authenticated]) 
    { 
     [self setAuthenticated:NO]; 
     [self setUrlConnection:[[NSURLConnection alloc] initWithRequest:[self requestObj] delegate:self]]; 
     [[self urlConnection] start]; 
     return NO; 
    } 
    return YES; 
} 

didReceiveAuthenticationChallenge:

-(void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge 
{ 
    if ([challenge previousFailureCount] == 0) 
    { 
     [self setAuthenticated:YES]; 
     NSURLCredential *credential = [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust]; 
     [challenge.sender useCredential:credential forAuthenticationChallenge:challenge]; 
    } 
    else [[challenge sender] cancelAuthenticationChallenge:challenge]; 
} 

didReceiveResponse:

私は、私は別のサイトからもらった次のコードで ssl certificateを追加しました
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response; 
{ 
    [self setAuthenticated:YES]; 
    [[self webView] loadRequest:[self requestObj]]; 
    [[self urlConnection] cancel]; 
} 

canAuthenticateAgainstProtectionSpace:

-(BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace 
{ 
    return [protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust]; 
} 

今、サーバが特定のDN名前のクライアント(証明書)から認証を必要としていました。私はiOS Client Certificates and Mobile Device Managementを見つけましたが、コードは私を助けなかったし、私の問題を解決しませんでした。

PKCS12ファイルをUIWebViewに追加することは可能ですか?サーバーがクライアントからの認証を希望する場合はUIWebViewにこのファイルを表示しますか?

私は常にエラー

2016-04-20 12:20:50.880 App [469:126255] NSURLSession/NSURLConnection HTTP load failed (kCFStreamErrorDomainSSL, -9813) 
2016-04-20 12:20:51.454 App [469:126252] CFNetwork SSLHandshake failed (-9824 -> -9829) 
2016-04-20 12:20:51.456 App [469:126252] NSURLSession/NSURLConnection HTTP load failed (kCFStreamErrorDomainSSL, -9829) 
+0

にこのクラスを追加しますが、シミュレータでテストしたのですか? – BHASKAR

+0

@BHASKARいいえ私はテストデバイスでテストしました。私はシミュレータでそれをテストする必要がありますか? – Premox

答えて

0

使用して、コード

-(void)connection:(NSURLConnection *)connection willSendRequestForAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge { 

if ([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust]) 
{ 
    NSURL* baseURL = [NSURL URLWithString:SERVER_IP]; 
    if ([challenge.protectionSpace.host isEqualToString:baseURL.host]) 
    { 
     NSLog(@"trusting connection to host %@", challenge.protectionSpace.host); 
     [challenge.sender useCredential:[NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust] forAuthenticationChallenge:challenge]; 
    } 
    else 
    { 
     NSLog(@"Not trusting connection to host %@", challenge.protectionSpace.host); 
    } 
} 

    [challenge.sender continueWithoutCredentialForAuthenticationChallenge:challenge]; 
} 

のこの部分を取得し、あなたの現在のクラスの上位

@interface NSURLRequest(AllowAllCerts) 

@end 


@implementation NSURLRequest(AllowAllCerts) 

+ (BOOL)allowsAnyHTTPSCertificateForHost:(NSString *)host 
{ 

return YES; 

} 
@end 
+0

あなたの最初のメソッドを 'ViewController'に追加しましたが、' UpperClass'はありません。私は 'AppDelegate'クラスともう1つしか持っていませんが、どれも上層クラスではありません。私は 'ViewController'のためにいくつかの' UpperClass'を作成すべきですか? – Premox

+0

は上位クラスではありません。実装の前にこれを追加してください。 – BHASKAR

+0

常に同じエラーが発生し、コードを追加した後に出力が得られます。2016-04-20 14:04:18.690 App [553:149129]がホストへの接続を信頼していますwww.example.de – Premox

関連する問題