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?
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.
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.
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 ? 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.
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With