我正在編寫一個iPhone應用程序與運行Joomla(準確地說是JomSocial)的網站連接,並從中檢索某些信息。 我完全被困在最後的日子裏,就是認證過程。 我不是一個web/php專家(只知道基礎知識),並且首次與Joomla平臺結合開發。iPhone應用程序需要使用ASIHTTPRequest與Joomla進行身份驗證
這種情況是:有一個用戶登錄的登錄表單(用戶名/密碼/記住)的網站。 在iPhone上,用戶啓動應用程序,並顯示一個典型的登錄視圖,其中包含兩個UITextFields,一個用於用戶名,一個用於密碼。還有一個按鈕可以繼續進行身份驗證並調用下面的方法。 當用戶填寫必填字段時,應用程序會假定記住按鈕並繼續進行身份驗證,直到按下注銷按鈕。
爲此,我正在實施ASIHTTPRequest API以在網站上發送信息。爲驗證方法的代碼如下:
- (void)performAuthentication
{
NSURL *url = [NSURL URLWithString:@"http://mywebsite.com/index.php?option=com_community"];
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
[request setDelegate:self];
[request setUseKeychainPersistence:YES];
[request setUseCookiePersistence:YES];
[request setUseSessionPersistence:YES];
[request setShouldAttemptPersistentConnection:YES];
[request setRequestMethod:@"POST"];
[request setUserAgent:@"Mozilla/5.0 (iPhone; CPU iPhone OS 5_0_1 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko) Version/5.1 Mobile/9A405 Safari/7534.48.3"]; //does not matter, I was just trying it
[request setTimeOutSeconds:20];
//main login options - user fills in
[request setPostValue:emailText.text forKey:@"username"];
[request setPostValue:passwordText.text forKey:@"passwd"];
[request setPostValue:@"yes" forKey:@"remember"];
//secondary options derived from string
[request setPostValue:@"Log+in" forKey:@"submit"];
[request setPostValue:@"com_user" forKey:@"option"];
[request setPostValue:@"login" forKey:@"task"];
[request startAsynchronous];
}
- (void)requestFinished:(ASIHTTPRequest *)request
{
NSLog(@"STATUS CODE: %i", [request responseStatusCode]);
NSLog(@"STATUS MESSAGE: %@", [request responseStatusMessage]);
NSLog(@"HEADERS: %@", [request responseHeaders]);
}
- (void)requestFailed:(ASIHTTPRequest *)request
{
NSError *error = [request error];
NSLog(@"Error = %@", error);
}
從requestFinished的結果是:
2012-01-18 17:15:48.850 MyApp[3500:f803] STATUS CODE: 200
2012-01-18 17:15:48.850 MyApp[3500:f803] STATUS MESSAGE: HTTP/1.1 200 OK
2012-01-18 17:15:48.851 MyApp[3500:f803] HEADERS:
{
"Cache-Control" = "post-check=0, pre-check=0";
Connection = close;
"Content-Type" = "text/html; charset=utf-8";
Date = "Wed, 18 Jan 2012 15:15:51 GMT";
Expires = "Mon, 1 Jan 2001 00:00:00 GMT";
"Last-Modified" = "Wed, 18 Jan 2012 15:15:51 GMT";
P3P = "CP=\"NOI ADM DEV PSAi COM NAV OUR OTRo STP IND DEM\"";
Pragma = "no-cache";
Server = "Apache/2.2.21 (Unix) mod_ssl/2.2.21 OpenSSL/1.0.0-fips mod_bwlimited/1.4 mod_perl/2.0.5 Perl/v5.10.1";
"Set-Cookie" = "currentURI=http%3A%2F%mywebsite.com%2Findex.php%3Foption%3Dcom_community; expires=Thu, 19-Jan-2012 15:15:51 GMT; path=/";
"Transfer-Encoding" = Identity;
"X-Powered-By" = "PHP/5.2.17";
}
當使用火狐與的LiveHTTPHeaders插件,我得到這個:
POST /index.php?option=com_community HTTP/1.1
Host: mywebsite.com
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.7; rv:9.0.1) Gecko/20100101 Firefox/9.0.1
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en;q=0.5
Accept-Encoding: gzip, deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
DNT: 1
Connection: keep-alive
Referer: http://mywebsite.com/index.php
Cookie: referrerid=AUPRR-4F16C6005781D; currentURI=http%3A%2F%2Fmywebsite.com%2Findex.php; 5ddfdbeb767d245c52d97fea9c4ba768=92a78d2faee8e9029d29d56d5d5270dc; activeProfile=90
Content-Type: application/x-www-form-urlencoded
Content-Length: 256
username=my_user&passwd=my_pass&submit=log+in&option=com_user&task=login&return=L2luZGV4LnBocD9vcHRpb249Y29tX2NvbW11bml0eSZ2aWV3PWZyb250cGFnZSZJdGVtaWQ9MzM%3D&3946e5e25450d1a242ceffbd96ea8733=1&remember=yes
HTTP/1.1 303 See other
Date: Wed, 18 Jan 2012 13:29:13 GMT
Server: Apache/2.2.21 (Unix) mod_ssl/2.2.21 OpenSSL/1.0.0-fips mod_bwlimited/1.4 mod_perl/2.0.5 Perl/v5.10.1
X-Powered-By: PHP/5.2.17
P3P: CP="NOI ADM DEV PSAi COM NAV OUR OTRo STP IND DEM"
Set-Cookie: 5ddfdbeb767d245c52d97fea9c4ba768=bff8a2f942f8e23633a8816cd36de1cb; path=/
Set-Cookie: activeProfile=90; expires=Wed, 18-Jan-2012 13:44:13 GMT; path=/
Set-Cookie: afcee664c0a8fd2cd6ff5cc9df8eab30=51+9+2+218475F+A+216444A5C165F505456145D115F535B+31011+74350+25455405D78+459+45B541A52565446+A42+3+B+C4412+411114E5D13+613+212+2+7+912+951+715+2+1+C13+244; expires=Thu, 17-Jan-2013 13:29:13 GMT; path=/
Location: http://mywebsite.com/index.php?option=com_community
Content-Length: 0
Keep-Alive: timeout=5, max=100
Connection: Keep-Alive
Content-Type: text/html
我m面臨3個問題:
- 我是獲取200個響應代碼而不是303個livehttpheaders指示。
- 名爲activeProfile的cookie看起來並未保存在cookie存儲中。
- Content-Length響應中的長標記(如livehttpheader輸出中所示)不能使用,因爲它在每次認證時都是隨機的,我不知道該如何處理它。
一般來說,身份驗證方法似乎連接但不執行該過程,剩下的就是標準http訪問的cookie。我可以通過加載一個帶有網站地址的UIWebView來驗證這一點,並且我看到用戶沒有登錄。
我不是網絡專家(儘管我可以理解某些事情)並且使用Joomla身份驗證時間。 我只是想知道如果有人遇到類似的東西,並已找到一條途徑。 任何想法或建議是非常感激。
感謝名單;)
是的,我試過。添加了所有相關的頭文件,但仍然沒有進行身份驗 – Pericles 2012-01-19 12:39:02