2017-02-12 77 views
0

我是iOS開發人員的初學者。我想通過身份驗證從IBM Domino服務器訪問一些數據。該代碼只能返回服務器的登錄頁面。任何人都知道什麼是錯的? (和抱歉,我的英語水平)IBM Domino服務器上的Swift 3 URLSession身份驗證

這裏是我的代碼來獲取數據:

class URLSessionTest: NSObject, URLSessionDelegate { 

let user = "myUser" 
let password = "myPwd" 
let url = URL.init(string: "https://www.example.com/Test.nsf/0/91182C6C9EEE0414C12580A300312D1A?Opendocument") 


func getData() { 
    var request = URLRequest.init(url: url!) 
    request.httpMethod = "POST" 
    request.timeoutInterval = 30.0 
    let parameters = ["Username": user, "Password": password] as Dictionary<String, String> 
    do { 
     request.httpBody = try JSONSerialization.data(withJSONObject: parameters, options: .prettyPrinted) 
    } catch let error { 
     print("request serialization error: \(error.localizedDescription)") 
    } 
    let configuration = URLSessionConfiguration.default 
    let session = URLSession(configuration: configuration, delegate: self, delegateQueue: nil) 
    let task = session.dataTask(with: request as URLRequest, completionHandler: { (data, response, error) in 
     if error != nil { 
      print ("dataTask error: \(error!.localizedDescription)") 
     } 
     if let myresponse = response as? HTTPURLResponse { 
      print ("dataTask response: \(myresponse)") 
      myresponse.statusCode 
     } 
     let myval = NSString(data: data!, encoding: String.Encoding.utf8.rawValue)! 
     print("dataTask data: \(myval)") 
    }) 
    task.resume() 
} 

和代表:

open func urlSession(_ session: URLSession, didReceive challenge: URLAuthenticationChallenge, completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Swift.Void){ 
    print ("challenge \(challenge.protectionSpace.authenticationMethod)") 
    var disposition: URLSession.AuthChallengeDisposition = .useCredential 
    var credential:URLCredential? 
    let defaultCredential = URLCredential(user: user, password: password, persistence: URLCredential.Persistence.none) 
    if challenge.previousFailureCount > 0 { 
     print ("cancel authentication challenge") 
     disposition = .cancelAuthenticationChallenge 
     credential = nil 
    } else if challenge.protectionSpace.authenticationMethod == NSURLAuthenticationMethodServerTrust { 
     print ("Server Trust") 
     credential = URLCredential(trust: challenge.protectionSpace.serverTrust!) 

     if (credential != nil) { 
      print ("Use credential") 
      disposition = .useCredential 
     } 
     else{ 
      print ("perform default handling") 
      disposition = .performDefaultHandling 
      credential = defaultCredential 
     } 
    } 
    else if challenge.protectionSpace.authenticationMethod == NSURLAuthenticationMethodClientCertificate { 
     print ("client certificate") 
    } 
    else if challenge.protectionSpace.authenticationMethod == NSURLAuthenticationMethodHTTPBasic { 
     print ("Basic authentication") 
    } 
    else{ 
     disposition = .cancelAuthenticationChallenge 
     credential = nil 
    } 
    if credential != nil { challenge.sender!.use(credential!, for: challenge)} 
    completionHandler(disposition, credential); 
} 

func urlSession(_ session: URLSession, task: URLSessionTask, didReceive challenge: URLAuthenticationChallenge, completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void) { 
    print ("URLSessionTask didReceive") 
    let credential = URLCredential(user: user, password: password, persistence: URLCredential.Persistence.forSession) 
    challenge.sender?.use(credential, for: challenge) 
    completionHandler(URLSession.AuthChallengeDisposition.useCredential,credential) 
} 

下面是代碼的輸出:

challenge NSURLAuthenticationMethodServerTrust 
Server Trust 
Use credential 
dataTask response: <NSHTTPURLResponse: 0x610000031780> { URL: https://www.example.com/Test.nsf/0/91182C6C9EEE0414C12580A300312D1A?Opendocument } { status code: 200, headers { 
"Cache-Control" = "no-cache"; 
"Content-Length" = 5949; 
"Content-Type" = "text/html; charset=UTF-8"; 
Date = "Sun, 12 Feb 2017 19:14:19 GMT"; 
Expires = "Tue, 01 Jan 1980 06:00:00 GMT"; 
Server = "Lotus-Domino"; 
"Strict-Transport-Security" = "max-age=0";} } 
+0

我的猜測是你的代碼需要基本認證。檢查Domino服務器設置爲使用什麼驗證方法 –

+0

這似乎是一個特定於IBM的問題。我嘗試訪問IBM Websphere Portal並得到類似的結果,例如IBM Domino。但是,我測試了登錄Microsoft SharePoint網站,它是成功的。 –

+1

IBM Domino和IBM Websphere服務器可能已設置爲使用LTPA進行身份驗證。這可以解釋你的問題。因此,您很可能需要將IBM Domino服務器上的站點的身份驗證方法更改爲基礎身份驗證 –

回答

1

在@Per上進一步擴展Henrik Lausten的評論稱,Domino服務器提供了一種繞過會話身份驗證並允許基本身份驗證的方法或訪問特定應用程序的URL。該方法在this IBM technote中描述。這是比打開整個網站進行基本認證更好的選擇。我發現您使用的是https,這很好,但您正在訪問的NSF文件上的屬性也應該設置爲需要https連接(如果它們尚未以此方式設置)。

+0

那麼,最後仍然保留在基本身份驗證中,它確實有效。謝謝! –

+0

既然它有效,請考慮增加投票並接受我的答案。 –