2

I am having an issue with authenticate NTLM credenitals, when I use valid credentials, it works, but when I use invalid credentials it does not fail, it works as the same as it would with valid credentials. This is only the case when I enter valid credentials first. Is there away to clear the credentials or what am I doing wrong here? Here is my code:

func loginUser(_ username: String, password: String, completion: @escaping (_ result: Bool) -> Void)
    {

        let user = username

        let password = password

        let url = webservice

        let credential = URLCredential(user: user, password: password, persistence: .none)

        let headers = ["Accept": "application/json;odata=verbose", "Content-type": "application/json;odata=verbose"]

        Alamofire.request(url, method: .get, headers: headers).authenticate(usingCredential: credential).responseJSON {
                (response) in

                print(response.result)

                switch response.result {

                case .success:
                    if let value = response.result.value {

                        completion(true)

                    }else{

                        print("There is error in the server response")

                        completion(false)
                    }

                case .failure (let error):

                    print("The NTLM request error is: ", error.localizedDescription)

                    completion(false)

                }

            }

    }

One thing I did notice, if I enter valid creds, then wait a few minutes and enter invalid ones it works as expected.

UPDATE

I have updated my code like so:

func loginUser(_ username: String, password: String, completion: @escaping (_ result: Bool) -> Void)
    {

        let user = username

        let password = password

        let url = webservice

        let credential = URLCredential(user: user, password: password, persistence: .none)

        var urlRequest = URLRequest(url: URL(string: url)!)

        urlRequest.cachePolicy = NSURLRequest.CachePolicy.reloadIgnoringCacheData

        urlRequest.httpMethod = "get"

        urlRequest.setValue("application/json;odata=verbose", forHTTPHeaderField: "Content-type")

        urlRequest.setValue("application/json;odata=verbose", forHTTPHeaderField: "Accept")

        Alamofire.request(urlRequest).authenticate(usingCredential: credential).responseJSON {
                (response) in

                switch response.result {

                case .success:
                    if let value = response.result.value {


                       completion(true)

                    }else{

                        print("There is error in the server response")

                        completion(false)
                    }

                case .failure (let error):

                    print("The NTLM request error is: ", error.localizedDescription)

                    completion(false)

                }

            }

    }

To add a cache policy, but I still get the same result :(

user979331
  • 11,039
  • 73
  • 223
  • 418

1 Answers1

0

.failure is not returned for HTTP errors. You need to look at the response code in the .success case for the HTTP response that indicates that auth failed.

Look here: Swift Alamofire: How to get the HTTP response status code for how to do that.

Or you might be hitting a cache. See: How to disable caching in Alamofire

Lou Franco
  • 87,846
  • 14
  • 132
  • 192