【问题标题】:Swift 4 - Alamofire - Authenticate NTLM works with invalid credentialsSwift 4 - Alamofire - 验证 NTLM 使用无效凭据
【发布时间】:2020-04-30 14:21:57
【问题描述】:

我在验证 NTLM 凭据时遇到问题,当我使用有效凭据时,它可以工作,但是当我使用无效凭据时它不会失败,它的工作方式与使用有效凭据时一样。仅当我首先输入有效凭据时才会出现这种情况。是否可以清除凭据或我在这里做错了什么?这是我的代码:

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)

                }

            }

    }

我确实注意到一件事,如果我输入有效的凭据,然后等待几分钟并输入无效的凭据,它会按预期工作。

更新

我已经像这样更新了我的代码:

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)

                }

            }

    }

添加缓存策略,但我仍然得到相同的结果:(

【问题讨论】:

  • 根据您的最后一句话,您可能遇到了缓存

标签: ios swift alamofire


【解决方案1】:

.failure 不会针对 HTTP 错误返回。您需要查看指示身份验证失败的 HTTP 响应的 .success 案例中的响应代码。

看这里:Swift Alamofire: How to get the HTTP response status code 了解如何做到这一点。

或者您可能正在访问缓存。见:How to disable caching in Alamofire

【讨论】:

  • 是的,响应代码与有效凭证与无效凭证相同....200
  • 如果您 100% 肯定您发送了错误的凭据,则服务器中似乎存在错误。
  • 我认为这可能是一个缓存问题,只是想弄清楚如何清除缓存
  • 不要清除它,而是让您的请求不使用缓存。 urlRequest.cachePolicy = .reloadIgnoringCacheData
  • 我应用了缓存策略,但它不起作用,请参阅更新后的问题
猜你喜欢
  • 2016-08-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-08-07
  • 1970-01-01
  • 2021-12-17
  • 1970-01-01
相关资源
最近更新 更多