Basic Authentication with Alamofire

前端 未结 6 1813
轻奢々
轻奢々 2020-12-15 05:41

Experiencing an issue when authenticating with Basic Auth. I am using a standard enum that conforms to URLRequestConvertible protocol to construct my requests.

6条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-15 06:34

    Swift 4

    private func getHeaders() -> [String: String] {
            let userName = "xxxx"
            let password = "xxxx"
            let credentialData = "\(userName):\(password)".data(using: .utf8)
            guard let cred = credentialData else { return ["" : ""] }
            let base64Credentials = cred.base64EncodedData(options: [])
            guard let base64Date = Data(base64Encoded: base64Credentials) else { return ["" : ""] }
            return ["Authorization": "Basic \(base64Date.base64EncodedString())"]
        }
    

提交回复
热议问题