Retolinho
Retolinho

Reputation: 3

Auth Challenge Alamofire 2.0

Since we updated our project to Swift 2 and Alamofire to version 2.0 we observe the following behavior regarding base auth: When we send a request (no matter what kind of) with authentication set, the request is always sent without authentication header the first time. After the backend answers with status code 401, alamofire adds the authentication header and and resends the request once again. We send the request using the following snipped:

Alamofire.request(request).authenticate(user: Config.serviceAuthUser, password: Config.serviceAuthPassword)

Is there a way to force Alamofire to include the authentiation header in every request? We want to avoid this kind of auth challenge for every request to lower network and server traffic. We had one solution working for iOS8, where we added the auth header in the session config of the shared instance of Alamofires Manager as follows:

Alamofire.Manager.sharedInstance.session.configuration.HTTPAdditionalHeaders = authHeader

But with iOS9 this does not work anymore, since only a copy of the configuration object is returned and modified.

Is there another way to avaoid this auth challenge process and force Alamofire to include the auth header with every request?

Upvotes: 0

Views: 270

Answers (1)

cnoon
cnoon

Reputation: 16643

Making two requests is how the underlying URL Loading System was designed by Apple. The Alamofire authenticate methods simply allow you to provide the credentials to supply to the challenge if it occurs.

If you want to provide the header directly, the use the headers parameter on the request method. Additionally, you could insert the user:password credentials directly into the URL.

Upvotes: 1

Related Questions