【问题标题】:iPhone application needs to authenticate with Joomla using ASIHTTPRequestiPhone 应用程序需要使用 ASIHTTPRequest 向 Joomla 进行身份验证
【发布时间】:2012-01-18 15:49:49
【问题描述】:

我正在编写一个 iPhone 应用程序来连接一个运行 Joomla(确切地说是 JomSocial)的网站并从中检索某些信息。 最近几天我完全陷入困境的是身份验证过程。 我不是 web/php 专家(只是了解基础知识)并且第一次结合 Joomla 平台进行开发。

场景是:有一个网站,有一个用户登录的登录表单(用户名/密码/记住)。 在 iPhone 上,用户启动应用程序并显示一个典型的登录视图,其中包含两个 UITextField,一个用于用户名,一个用于密码。还有一个按钮可以继续进行身份验证并调用下面发布的方法。 当用户填写必填字段时,应用假定记住按钮并继续进行身份验证,直到按下注销按钮为止。

为此,我正在实现 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 插件的 Firefox 时,我明白了:

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

我面临 3 个问题:

  1. 我收到的是 200 响应代码,而不是 livehttpheaders 指示的 303。
  2. 名为 activeProfile 的 cookie 似乎没有保存在 cookie 存储中。
  3. 无法使用 Content-Length 响应中的长令牌(如 livehttpheader 输出所示),因为它在每次身份验证时都是随机的,我不知道如何处理它。

一般来说,身份验证方法似乎连接但不执行该过程,剩下的是标准http访问的cookie。我可以通过加载带有网站地址的 UIWebView 来验证这一点,我看到用户没有登录。

我不是网络专家(虽然我可以理解某些事情)并且第一次使用 Joomla 身份验证。 我只是想知道是否有人遇到过类似的事情并找到了解决办法。 非常感谢任何想法或建议。

谢谢 ;)

【问题讨论】:

    标签: iphone ios joomla asihttprequest


    【解决方案1】:

    刚刚注意到您没有在代码中设置任何 HttpHeaders。检查应与 Joomla 标头匹配的 HttpHeaders。

    【讨论】:

    • 是的,我试过了。添加了所有已发送但仍无身份验证的标头。
    猜你喜欢
    • 2012-12-12
    • 2011-06-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-11
    • 1970-01-01
    • 2017-03-03
    • 2017-05-06
    相关资源
    最近更新 更多