-
-
Notifications
You must be signed in to change notification settings - Fork 7.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Basic Auth user credentials not being passed with request #32
Comments
I'm having the same issue. The example in the README actually doesn't work either. |
These are doing different things. Alamofire is creating a credential that will be used during an authentication challenge, whereas AFNetworking is setting the |
Came here to ask this exact question. @mattt I understand your answer, but maybe update the ReadMe to explain a bit more? |
Thanks for the clarification @mattt. I'll explain for anyone else who may be running into this issue. Based on the implementation in AFNetworking, what I need to do is create the Authorization header myself instead of using the current authenticate method. In AFNetworking (and in general) this header should look like this.
Here, The issue with using AlamoFire over AFNetworking (for now) is that you have to do the Base64 encoding yourself. AFNetworing included an encoding implementation which was called behind the scenes while creating the header. All you had to do is pass in the username and password. That being said, I imagine @mattt or someone will add that functionality into AlamoFire at some point. |
This caught me too. Thank you for explaining! Code below to hopefully save someone else time. let plainString = "username:password" as NSString
let plainData = plainString.dataUsingEncoding(NSUTF8StringEncoding)
let base64String = plainData?.base64EncodedStringWithOptions(NSDataBase64EncodingOptions.fromRaw(0)!)
Alamofire.Manager.sharedInstance.defaultHeaders["Authorization"] = "Basic " + base64String! Encoding code adopted from http://ios-blog.co.uk/tutorials/quick-tips/base64-decoding-in-ios-7-objective-c-and-ios8-swift/ |
How about adding a pre-auth param to the For comparison, |
This caught me too. Many APIs (including GitHub's popular API) do not send the |
For future travelers, if you'd like to pre-authorize: let plainString = "\(user):\(password)" as NSString
let plainData = plainString.dataUsingEncoding(NSUTF8StringEncoding)
let base64String = plainData?.base64EncodedStringWithOptions(NSDataBase64EncodingOptions.fromRaw(0)!)
Alamofire.Manager.sharedInstance.session.configuration.HTTPAdditionalHeaders = ["Authorization": "Basic " + base64String!]
Alamofire.request(.GET, "http://example.com")
.response {(request, response, _, error) in
println(response)
} |
Update for people seeing @loopj's comment in 2015: the README now notes that
|
@brow Thanks for the heads up. Can someone please comment on why this is not recommended? |
I think the idea is that NSURLConnection is designed to manage stateful authorization for you according to RFC 2617 and therefore doesn't expect you to override the |
Just got trolled by this... 👊 |
Same thing. 👊 : And update for @loopj code in favor of Swift 1.2: let plainString = "\(user):\(password)" as NSString
let plainData = plainString.dataUsingEncoding(NSUTF8StringEncoding)
let base64String = plainData?.base64EncodedStringWithOptions(NSDataBase64EncodingOptions(rawValue: 0))
Alamofire.Manager.sharedInstance.session.configuration.HTTPAdditionalHeaders = ["Authorization": "Basic " + base64String!] |
Very helpful post. Save my day! |
Thanks @skywinder - @mattt perhaps we could simply point out in the README that the HTTP basic auth challenge in Alamofire is different in nature to |
Using the Alamofire.Manager.sharedInstance seems a little dangerous if, for example, I'm making multiple requests to different endpoints that require different credentials. Doing preemptive authentication seems like it's common enough to warrant an enhancement to the API. I like @macu's suggestion of adding a PreAuthenticate property or something similar |
Just ran into this as well. +1 for Authorization headers.... |
As an update to @loopj @skywinder implementation, with AlamoFire > 4.0.0, you can alternatively use the static func authorizationHeader in Request.swift, which conveniently does the encoding for you. Something like:
|
I'm trying to use the example from NSHipster to do Basic Auth. I form my request like so:
Email and password are just strings here. I am getting a response suggesting that the credentials aren't attached.
When I make the same request while using AFNetworking, I get a successful 200 response.
If I'm making a mistake in my request with AlamoFire, would you mind pointing it out?
The text was updated successfully, but these errors were encountered: