This article introduces the content of ASIHTTPRequest authentication for iOS development, including specifying the user name and password to be used for the URL and the user name and password to be used for the request, store credenkeyin the keychain, and store credenchain In the session. NTLM authorization uses a proxy to provide creden. Currently, the built-in authorization dialog box is only valid for iOS ), sends creden to the server before the server requests creden.
ASIHTTPRequest is easy to use and encapsulates CFNetwork APIs. Makes it easier to communicate with Web servers. It is written in Objective-C and can be used in mac OS X and iPhone applications.
You can refer to the ASIHTTPRequest authorization flowchart to learn how to find the authorization credential and apply the authorization credential to the request.
Specify the user name and password to use for the URL
- NSURL *url = [NSURL URLWithString:@"http://www.dreamingwish.com/"];
- ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
Specify the user name and password to use for the request
- NSURL *url = [NSURL URLWithString:@"http://www.dreamingwish.com/"];
- ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
- [request setUsername:@"username"];
- [request setPassword:@"password"];
Store credenkeyto keychain
If keychainPersistence is enabled, all available user names and passwords provided will be stored in the keychain, and these user names and passwords will be reused in future requests, even if you close the program and re-open it, it does not affect.
- NSURL *url = [NSURL URLWithString:@"http://www.dreamingwish.com/"];
- ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
- [request setUseKeychainPersistence:YES];
- [request setUsername:@"username"];
- [request setPassword:@"password"];
If you use keychain but want to manage it by yourself, you can find related class methods in the ASIHTTPRequest. h file.
Store creden in session
If useSessionPersistence is enabled by default), ASIHTTPRequest stores creden。 in the memory, and later requests reuse these creden.
- NSURL * url = [NSURL URLWithString: @ "http://www.dreamingwish.com/"];
- ASIHTTPRequest * request = [ASIHTTPRequest requestWithURL: url];
- [Request setUsername: @ "username"];
- [Request setPassword: @ "password"];
- [Request setUseSessionPersistence: YES]; // This item is the default one, so it is not necessary.
-
- // Our username and password will be reused
- Request = [ASIHTTPRequest requestWithURL: url];
NTLM authorization
To use an NTLM-authorized Windows server, you must specify the authorization domain.
- NSURL *url = [NSURL URLWithString:@"http://www.dreamingwish.com/"];
- ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
- [request setUsername:@"username"];
- [request setPassword:@"password"];
- [request setDomain:@"my-domain"];
Use a proxy to provide creden
You do not have to specify authorization creden。 in advance. You can also request creden。 from each request when they cannot find creden。 from the session or keychain. This is useful if you want to connect to a server that you do not know the authorization type.
Your delegate must implement authenticationNeededForRequest: method. When the request waits for the credential, ASIHTTPRequest will suspend the request. If you hold the creden you need, set the creden。 for the request first, and then call [request retryUsingSuppliedCredentials. If you want to cancel the authorization, call [request cancelAuthentication]. At this time, the request will also be canceled.
From version 1.0.8, only one request's delegate can receive the authenticationNeededForRequest: Or proxyAuthenticationNeededForRequest :. When delegate processes the first request, other requests that require authorization will be paused. If one credential is provided, all other requests in the current process will assume that the credential is valid for this URL and try to reuse this credential. If delegate cancels authorization and the shouldCancelAllRequestsOnFailure value of the queue is YES, all other requests will be canceled and they will not try to request creden ).
You cannot use proxy mode to authorize synchronous requests.
In older versions, this will cause the program to be suspended. Starting from 1.0.8, the proxy function will not be called even if you do so.
The built-in authorization dialog box is currently only valid for iOS)
This feature is attributed to the new ASIAuthenticationDialog class of version 1.0.8. This feature is mainly used to authorize the proxy which will be introduced later), but it can also be used to obtain authorization creden。 from the user.
For better user experience, most apps connected to a single service must implement authenticationNeededForRequest for the request's delegate: method, or avoid using proxy authorization at the same time.
Most apps that connect to a single service shocould implement authenticationNeededForRequest: in their request delegates, or avoid the use of delegation-style authentication altogether.
However, in some cases, the standard authorization dialog box for normal authorization to use ASIHTTPRequest is better:
- You don't want to create your own login form
- You may need to obtain data from external resources, but you do not know if you need authorization.
In these cases, set shouldPresentAuthenticationDialog to YES for the request. If your agent does not implement
AuthenticationNeededForRequest: method. This dialog box is displayed.
Only one dialog box can be displayed at a time, so when a dialog box is displayed, all other requests that require authorization will be paused. If one credential is provided, all other requests in the current process will assume that the credential is valid for this URL and try to reuse this credential. If delegate cancels authorization and the shouldCancelAllRequestsOnFailure value of the queue is YES, all other requests will be canceled and they will not try to request creden ).
For synchronous requests, the authorization dialog box is not displayed.
This dialog box partially imitates the authorization dialog box used by Safari on the iPhone. It contains the following content:
- A piece of information indicates that these creden are used for websever instead of a proxy)
- The host name or IP address you want to connect to the server
- If the authorization domain is provided)
- Enter the region of the user name and password
- When a server is connected to the NTLM authorization mode, it also contains a domain region.
- Indicates whether the creden will be sent in plaintext. For example, "The creden will be sent in plaintext only when non-SSL-based basic authorization mode is used ")
To change its appearance, you must inherit ASIHTTPRequest and override showAuthenticationDialog to display your own dialog box or ASIAuthenticationDialog subclass.
Send creden to the server before the server requests creden
IMPORTANT
Starting from 1.8.1, when a request in the basic authorization mode is used, the behavior of this feature changes. You may need to modify your code.
When a request is generated for the first time, ASIHTTPRequest can first send creden。 to the server if any), instead of providing creden。 when the server requires creden. This feature can improve the execution efficiency of authorized programs, because it avoids unnecessary requests.
To trigger this action in the basic authorization mode, you must manually set the authenticationScheme of the request to kCFHTTPAuthenticationSchemeBasic:
- [request setAuthenticationScheme:(NSString *)kCFHTTPAuthenticationSchemeBasic];
For other authorization schemes, creden can also be sent before the server requests, but only after another request is successfully authorized.
You may want to disable this feature in the following situations:
- Your program may use a series of creden at a time to talk to the server.
- Security is very important for your program. It is relatively insecure to use this feature because you cannot verify that you are connected to the correct server before the creden are sent.
To disable this feature, do the following:
- [request setShouldPresentCredentialsBeforeChallenge:NO];