【问题标题】:In iOS, how would you programmatically pass a username / password to a secure site在 iOS 中,您如何以编程方式将用户名/密码传递给安全站点
【发布时间】:2010-10-10 00:23:51
【问题描述】:

如何在 iOS 中以编程方式访问受 .htaccess 或访问控制保护的受密码保护的网站。

使用 NSURLRequest,您可以从 URL 发出 GET 请求。但是,如果 URL 受到保护,您将如何发送用户名密码对。另外,如果它使用标准 MD5 加密方式进行加密呢?

【问题讨论】:

    标签: ios


    【解决方案1】:

    将这样的内容添加到您的 HTTPConnectionDelegate:

    -(void)connection:(NSURLConnection *)aConnection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge {
            if ([challenge previousFailureCount] == 0) {
                    NSURLCredential *newCredential;
                    newCredential = [NSURLCredential credentialWithUser:username password:password persistence:NSURLCredentialPersistenceNone];
                    [[challenge sender] useCredential:newCredential forAuthenticationChallenge:challenge];
            } else {
                    [[challenge sender] cancelAuthenticationChallenge:challenge];
            }
    }
    

    【讨论】:

    • 谢谢..这是作为明文传递的吗?一些网络服务器拒绝明文密码。
    • 文档不清楚,但我不得不猜测它使用了服务器请求的身份验证方法。如果那是基本身份验证,那么它不会被加密(除非连接是 SSL)。如果是digest auth,则使用md5单向哈希方法。
    猜你喜欢
    • 1970-01-01
    • 2019-01-23
    • 2016-06-25
    • 2013-02-07
    • 2015-07-13
    • 2018-12-05
    • 2020-02-15
    • 1970-01-01
    • 2022-11-19
    相关资源
    最近更新 更多