Swift 3 NTLM身份验证

编程入门 行业动态 更新时间:2024-10-23 03:32:10
本文介绍了Swift 3 NTLM身份验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

对于一个最近的项目,我试图分别从服务器中以SOAP和oData格式提取一些数据,这些数据已通过Microsoft NTLM身份验证进行保护,这真是一场噩梦,想方设法做到这一点,而这一切都不在线例子确实有效.

For a recent project I tried to pull some data from a server in the SOAP and oData format respectively, that is protected with a Microsoft NTLM authentication, and it has been a nightmare figuring out how to do it, none of the online examples really worked.

推荐答案

所以这是我的解决方案;我必须适应,扩展和结合一些不同的资源.我希望这对以后的人有所帮助.

So here is my solution; I had to adapt, expand and combine a few different sources. I hope this helps someone in the future.

// // ViewController.swift // ntlm // // Created by Kamik423 on 21.3.17. // Copyright © 2017 Kamik423 All rights reserved. // // You might have to allow arbitrary loads!! // // Adapted from: // gist.github/stevenschobert/f374c999e5cba6ccf09653b846967c83 // blogs.msdn.microsoft/chiranth/2013/09/20/ntlm-want-to-know-how-it-works/ import UIKit class ViewController: UIViewController { var username: String? = nil var password: String? = nil lazy var conn: URLSession = { let config = URLSessionConfiguration.ephemeral let session = URLSession(configuration: config, delegate: self, delegateQueue: nil) return session }() override func viewDidLoad() { super.viewDidLoad() username = "<username>" password = "<password>" ntlm() } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } func ntlm() { let urlString = "<url>" let url = URL(string: urlString) let request = NSMutableURLRequest(url: url!, cachePolicy: .reloadIgnoringLocalCacheData, timeoutInterval: 60000) request.httpMethod = "GET" let task = conn.dataTask(with: request as URLRequest, completionHandler: { (data, response, error) -> Void in print(response) print(error) print(String(data: data!, encoding: .utf8)) }) task.resume() } func doesHaveCredentials() -> Bool { guard let _ = self.username else { return false } guard let _ = self.password else { return false } return true } } extension ViewController: URLSessionDelegate { func urlSession(_ session: URLSession, didReceive challenge: URLAuthenticationChallenge, completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void) { print("got challenge") guard challenge.previousFailureCount == 0 else { print("too many failures") challenge.sender?.cancel(challenge) completionHandler(.cancelAuthenticationChallenge, nil) return } guard challenge.protectionSpace.authenticationMethod == NSURLAuthenticationMethodNTLM else { print("unknown authentication method \(challenge.protectionSpace.authenticationMethod)") challenge.sender?.cancel(challenge) completionHandler(.cancelAuthenticationChallenge, nil) return } guard self.doesHaveCredentials() else { challenge.sender?.cancel(challenge) completionHandler(.cancelAuthenticationChallenge, nil) DispatchQueue.main.async { print("Userdata not set") }; return } let credentials = URLCredential(user: self.username!, password: self.password!, persistence: .forSession) challenge.sender?.use(credentials, for: challenge) completionHandler(.useCredential, credentials) } }

更多推荐

Swift 3 NTLM身份验证

本文发布于:2023-11-26 13:03:38,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1633978.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:身份验证   Swift   NTLM

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!