Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Resetting basic authentication credentials with AFNetworking

I am writing a REST client (with AFNetworking) and need the ability to trigger the creation of a new session within a single instance of an application.

In other words, I would like to:
1 - Authenticate with server
2 - Do some REST calls
3 - Simulate "Log out"
4 - Re-authenticate with server
5 - Do some more REST calls

AFNetworking is great with making that initial authentication and REST calls, but I can't figure out how I would clear the session and "reset" the connection within the same instance.

When I used ASIHTTP, I just did:
[ASIHTTPRequest clearSession];

Is there a way to do something similar with AFNetworking?

like image 802
user1174179 Avatar asked Jan 27 '12 19:01

user1174179


People also ask

How does basic authentication work?

Basic authentication works as follows: If a request requires authentication, the server returns 401 (Unauthorized). The client sends another request, with the client credentials in the Authorization header.

How does basic authentication work in cPanel?

Basic authentication works as follows: If a request requires authentication, the server returns 401 (Unauthorized). The response includes a WWW-Authenticate header, indicating the server supports Basic authentication. The client sends another request, with the client credentials in the Authorization header.

How do I enable basic authentication in IIS?

For a public-facing web site, you typically want to authenticate against an ASP.NET membership provider. To enable Basic authentication using IIS, set the authentication mode to "Windows" in the Web.config of your ASP.NET project: In this mode, IIS uses Windows credentials to authenticate. In addition, you must enable Basic authentication in IIS.

How to handle basic authentication for different browsers?

How to handle Basic Authentication for different browsers ? The basic authentication process for both Chrome and Firefox browsers can be done by appending the username and password in URL of the page. Here is a link to video tutorial explaining how to carry the basic authentication for Chrome browser, Click here.


1 Answers

Use AFHTTPClient (see the API client in the example project).

Credentials can be set with -setAuthorizationHeaderWithUsername:password:. Each request created from that HTTP client will have an Authorization HTTP header, kind of like a browser session.

When the user logs out, or you want to clear credentials, do -clearAuthorizationHeader.

like image 69
mattt Avatar answered Oct 13 '22 11:10

mattt