如何创建用于Alamofire的NTLM身份验证标头?

编程入门 行业动态 更新时间:2024-10-25 02:20:48
本文介绍了如何创建用于Alamofire的NTLM身份验证标头?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

这些是请求标头:

let userName = "someUserName" let password = "aPasswordForSomeUserName" var headers: HTTPHeaders = [ "Accept": "application/json", ] if let authorizationHeader = Request.authorizationHeader(user: userName, password: password) { headers[authorizationHeader.key] = authorizationHeader.value }

因此这会生成授权。

基本aC5paHFoOkulbXKhNpk43A == (我为安全性对其进行了修改。)

Basic aC5paHFoOkulbXKhNpk43A== (I have modified it for security).

但是当我在Advance Rest Client中发出相同请求时( chrome扩展名)。我看到这一点:

But when I make the same request in Advance Rest Client (a chrome extension). I am seeing this:

Accept: application/json Authorization: NTLM TlMMTVNTUAADAAAAGAAYAG4AAAAYABgAhgAAAAYABgBAAAAADAAMAEYAAAAcABwAUgPPPAAAAACeAAAAAYIAAEUARwBBAGgALgBzAGgAYQBoAUIOVABHAC4AUSDFGC4ARQBHAEEALgBMAEEAToD38IenExnddmNhyXz+u0cmIHEl/p8P9OWe2rePPsiRkZO1Ne6ZrWxnIxHK1CZcyTU=

在为我的用户名和密码生成的授权密钥中,注意NTLM和Basic。

Notice, NTLM and Basic in both the generated authorization key for my username and password.

如何在iOS中(以及可能使用Alamofire)执行此操作?

How to do this in iOS (and possibly with Alamofire)?

这也导致了我之前提出的这个问题。

This also leads to this question I asked previously.

如何用Alamofire 4.0发出NTML请求?

推荐答案

我在链接,并处理使用Alamofire发送的任何请求,而无需添加登录名对于每个ViewController:

I enhanced the correct answer in this link, and make work of any request sent using Alamofire instead of adding the login for each ViewController:

private var manager : SessionManager? var username: String? = nil var password: String? = nil func doesHaveCredentials() -> Bool { self.username = Defaults[.username] self.password = Defaults[.password] guard let _ = self.username else { return false } guard let _ = self.password else { return false } return true } func apiManager() -> SessionManager{ if let m = self.manager{ return m }else{ let configuration = URLSessionConfiguration.default configuration.timeoutIntervalForRequest = 25 configuration.timeoutIntervalForResource = 25 self.manager = Alamofire.SessionManager(configuration: configuration) let delegate: Alamofire.SessionDelegate = self.manager!.delegate delegate.taskDidReceiveChallengeWithCompletion = { session, task, challenge, completionHandler in print("Got challenge") guard challenge.previousFailureCount == 0 else { print("too many failures") challenge.sender?.cancel(challenge) completionHandler(.cancelAuthenticationChallenge, nil) return } guard challenge.protectionSpace.authenticationMethod == NSURLAuthenticationMethodNTLM else { print("unknown authentication method \(challenge.protectionSpace.authenticationMethod)") challenge.sender?.cancel(challenge) completionHandler(.cancelAuthenticationChallenge, nil) return } guard self.doesHaveCredentials() else { challenge.sender?.cancel(challenge) completionHandler(.cancelAuthenticationChallenge, nil) DispatchQueue.main.async { print("Userdata not set") }; return } let credentials = URLCredential(user: self.username!, password: self.password!, persistence: .forSession) challenge.sender?.use(credentials, for: challenge) completionHandler(.useCredential, credentials) } return self.manager! } }

更多推荐

如何创建用于Alamofire的NTLM身份验证标头?

本文发布于:2023-11-07 17:42:46,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1567035.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:身份验证   Alamofire   NTLM

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!