【问题标题】:How to authenticate an UIWebView with a client certificate?如何使用客户端证书对 UIWebView 进行身份验证?
【发布时间】:2016-04-20 10:19:14
【问题描述】:

我有以下问题:

我有一个UIWebView,它正在正确加载网站,但服务器也需要来自客户端 (UIWebView) 的身份验证。我添加了ssl certificate 以及我从另一个站点获得的以下代码:

应该StartLoadWithRequest:

-(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:

-(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)

【问题讨论】:

  • 你在模拟器中测试了吗?
  • @BHASKAR 不,我已经在我的测试设备上进行了测试。我应该在模拟器上测试它吗?

标签: ios objective-c iphone uiwebview client-certificates


【解决方案1】:

使用这段代码

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

【讨论】:

  • 我在ViewController 中添加了您的第一个方法,但我没有任何UpperClass。我只有AppDelegate 类和另一个类,但它们都不是UpperClass。我应该为我的ViewController 创建一些UpperClass 吗?
  • 不是上流社会。在您的实施之前添加它。
  • 总是遇到同样的错误,添加代码后我得到输出 2016-04-20 14:04:18.690 App[553:149129] trusting connection to host www.example.de
  • 解决了。我已经从帖子中的链接 URL 修改了方法 =)。感谢支持,帮助我理解逻辑
  • 你如何修改方法,@Premox?我没有在问题中看到编辑?
猜你喜欢
  • 2012-01-10
  • 2014-08-23
  • 2015-10-29
  • 2018-07-05
  • 2019-01-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-04-24
相关资源
最近更新 更多