【问题标题】:Bypass self signed certificate with NSURLCredential while also passing in a username and password使用 NSURLCredential 绕过自签名证书,同时传入用户名和密码
【发布时间】:2012-10-18 18:11:07
【问题描述】:
    NSURLCredential *credential2 = [NSURLCredential
                  credentialForTrust:challenge.protectionSpace.serverTrust
                  credentialWithUser: @"username"
                                            password: @"password"
                                         persistence:NSURLCredentialPersistenceNone];


    [challenge.sender useCredential:credential2 forAuthenticationChallenge:challenge];

我需要绕过带有 UIWebView 的自签名证书,同时还能够传入用户名和密码进行身份验证。

 NSURLCredential *credential = [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust];

NSURLCredential *credential = [NSURLCredential credentialWithUser: @“用户名”密码:@“密码” 持久性:NSURLCredentialPersistenceNone];

都是单独有效的,当我尝试将它们结合起来时,它们都会大惊小怪......有什么想法吗?

【问题讨论】:

    标签: objective-c ipad uiwebview nsurlconnection nsurlcredential


    【解决方案1】:

    使用 RestKit 和下面的代码以及用于登录凭据的 alertView 来解决此问题。

    - (id)initWithURL:(NSURL*)pageURL {
    
        _retainedURL = pageURL;
        [_retainedURL retain];
    
        RKClient *client;
        NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
        client = [RKClient clientWithBaseURL: pageURL];
        client.username=[userDefaults objectForKey:@"UserName"];
        client.password=[userDefaults objectForKey:@"Password"];
        client.authenticationType=RKRequestAuthenticationTypeHTTPBasic;
        client.disableCertificateValidation= TRUE;
        RKRequest *request=[RKRequest requestWithURL:pageURL delegate:self];
        [client configureRequest:request];
        request.authenticationType = RKRequestAuthenticationTypeHTTPBasic;
        request.disableCertificateValidation = TRUE;
        self.authenticatedRequest = request;
        [request sendAsynchronously];
    
        if(self = [super init]) {
            self.URL = pageURL;
            self.availableActions = SVWebViewControllerAvailableActionsOpenInSafari | SVWebViewControllerAvailableActionsMailLink | SVWebViewControllerAvailableActionsCopyLink;
        }
        return self;
    }
    

    登录 alertView 将用户名和密码文本添加到 userdefaults 并将保留的 URL 传递回 initWithURL 函数。当页面未通过身份验证(使用登录凭据)时,将调用登录警报视图。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-03-13
      • 1970-01-01
      • 1970-01-01
      • 2013-11-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多