2017-02-12 8 views
0

私はiOS開発の初心者です。認証を使用してIBM Domino Serverから一部のデータにアクセスしたいとします。コードはサーバーのログインページを返すだけです。誰が何が間違っているのか分かりますか? (と私の英語のため申し訳ありません)。ここSwift 3 IBM Domino Server上の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 Serverの認証方法を確認してください –

+0

これはIBM特有の問題です。 IBM Websphere Portalにアクセスしようとしましたが、IBM Dominoなどの同様の結果が得られました。しかし、私はMicrosoft SharePointサイトへのログインをテストしましたが、成功しました。 –

+1

IBM DominoとIBM Websphereサーバーが認証にLTPAを使用するように設定されている可能性があります。これはあなたの問題を説明することができます。だから、ほとんどの場合、IBM Dominoサーバー上のサイトの認証方法を基盤認証 –

答えて

1

@Per Henrik Lausten氏のコメントを基にして、Dominoサーバーはセッション認証をバイパスし、基本認証を許可する方法を提供しています。fまたは特定のアプリケーションにアクセスするURL。方法はthis IBM technoteに記載されています。これは、サイト全体を基本認証に開放するよりも優れた方法です。私はあなたがhttpsを使用していることを知っています。これは良いですが、あなたがアクセスしているNSFファイルのプロパティも、そうした方法で設定されていない場合、https接続を要求するように設定する必要があります。

+0

に変更する必要があります。最終的に基本認証に残り、実際に動作します。ありがとう! –

+0

動作しているので、アップ投票し、私の答えを受け入れることを検討してください。 –

関連する問題