【问题标题】:Using user authentication/sessions with Django and ASIHttpRequest通过 Django 和 ASIHttpRequest 使用用户身份验证/会话
【发布时间】:2012-05-04 10:00:39
【问题描述】:

我正在尝试在 iOS 中创建一个基本的身份验证系统,该系统将 POST 发送到 Django,并在 Django 端对用户进行身份验证并启动会话。现在我可以通过在 URL 中将值作为数据传递并对其进行身份验证来发送用户信息,但是如何从 Django 响应中检索会话数据或 cookie?当我尝试存储或打印出 cookie 时,它​​告诉我数组为空。 request.requestCookies 和 request.responseCookies 我都试过了。

NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:@"test_user", @"username", @"pass", @"password", nil];

NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://127.0.0.1:8000/login/"]];

NSError *error;
NSData *data = [NSJSONSerialization dataWithJSONObject:dict options:0 error:&error];

if ( error ) {
    NSLog( @"ERROR - %@", error.localizedDescription );
} else {
    __block ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
    [request addRequestHeader: @"Content-Type" value:@"application/json; charset=utf-8"]; 
    [request appendPostData:data];
    [request setRequestMethod:@"POST"];

    [request setCompletionBlock:^{

        UIAlertView *alerView = [[UIAlertView alloc] initWithTitle:@"Login"
                                                           message:@"Login was sent"
                                                          delegate:nil
                                                 cancelButtonTitle:nil
                                                 otherButtonTitles:@"Ok", nil];
        [alerView performSelectorOnMainThread:@selector(show) withObject:nil waitUntilDone:NO];

        NSLog(@"RESPONSE: %@", [[NSString alloc] initWithData:request.responseData encoding:NSUTF8StringEncoding]);//, [request.requestCookies objectAtIndex:0]);
        NSLog(@"COOKIE: %@", [request.requestCookies objectAtIndex:0]);
        [ASIHTTPRequest addSessionCookie:[request.requestCookies objectAtIndex:0]];

    }];

【问题讨论】:

    标签: ios django authentication asihttprequest session-cookies


    【解决方案1】:

    好的,所以我解决了这个问题,结果发现我在服务器端输入了数据,但没有使用 Django 正式登录(没有返回正确的 cookie),这意味着我的应用程序没有收到正确的标头.

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-12-29
      • 2017-06-21
      • 2012-03-26
      • 2019-02-20
      • 2018-12-13
      • 2019-07-15
      • 2011-12-23
      相关资源
      最近更新 更多