首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用NSURLCredential绕过自签名证书,同时传递用户名和密码

使用NSURLCredential绕过自签名证书,同时传递用户名和密码
EN

Stack Overflow用户
提问于 2012-10-18 18:11:07
回答 1查看 511关注 0票数 0
代码语言:javascript
复制
    NSURLCredential *credential2 = [NSURLCredential
                  credentialForTrust:challenge.protectionSpace.serverTrust
                  credentialWithUser: @"username"
                                            password: @"password"
                                         persistence:NSURLCredentialPersistenceNone];


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

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

代码语言:javascript
复制
 NSURLCredential *credential = [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust];

NSURLCredential *凭证= NSURLCredential credentialWithUser:@“用户名”密码:@“密码”持久性:NSURLCredentialPersistenceNone;

都是有效的。当我试图把它们结合起来时,它们都会大惊小怪……有什么想法吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-11-20 19:30:53

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

代码语言:javascript
复制
- (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将用户名和密码的文本添加到用户默认值,并将保留的URL传递回initWithURL函数。当页面未经过身份验证(带有登录凭据)时,将调用登录警报视图。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12961001

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档