iphone - NSURLConnection,使用错误的凭据登录成功

标签 iphone objective-c cookies authentication nsurlconnection

我正在使用 NSURLConnection 通过 GET 请求与服务器连接。要登录,我将发送带有表单 url 的 GET 请求

http://<myUserName>:<myPassword>@my.serverurl.com/user/login

示例 URL 为

http://someusername:<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="32415d5f5742534141724545451c555d5d555e571c515d5f" rel="noreferrer noopener nofollow">[email protected]</a>/user/login 

当我第一次登录时,如果我输入了正确的用户名和密码,我就能够成功登录,如果我输入了错误的用户名或密码,我会收到服务器的响应,说我的用户名和密码不正确。那么一切都好

但是当我使用正确的用户名/密码登录时,进入我的主页。我在那里注销以返回登录屏幕。现在我输入错误的用户名和密码并尝试登录,我收到登录成功的响应..这是我的问题。

这意味着我所有后续的登录请求都成功返回,但用户名和密码不正确。我用浏览器检查了相同的网址(正确和错误),一切都正常工作。

这是我用来登录的代码

-(void)postRequest{
    NSMutableURLRequest *request    =   [[NSMutableURLRequest alloc] init] ; 
    [request setURL:[NSURL URLWithString:
              @"http://myusername:<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="422f3b32233131022f3b6c31273034273037302e6c212d2f" rel="noreferrer noopener nofollow">[email protected]</a>/user/login"]]; 
    [request setHTTPMethod:@"GET"];
//    [request setHTTPShouldHandleCookies:NO];   ==> I tried without this first

    loginRequestConnection  =   [[CustomURLConnection alloc]
                                      initCustomURLWithRequest:request 
                                      delegate:self 
                                      withTAG:LOGIN_REQUEST_CONNECTION];
    [request release];
}

现在 CustomURLConnection 只不过是 NSURLConnection 的子类,并添加了一个标签来识别委托(delegate)中的多个连接请求

@interface CustomURLConnection : NSURLConnection{
}
@property()NSInteger tag;
@property(nonatomic, retain) NSMutableData *receivedData;

-(id)initCustomURLWithRequest:(NSURLRequest *)request delegate:(id)delegate withTAG:(NSInteger)tagInt;
@end

我认为问题是 NSURLConnection 请求将 cookie 存储在某处并在后续请求中重用它们。这就是为什么我添加了

[request setHTTPShouldHandleCookies:NO];

到代码。但无论有没有这个问题仍然存在。那么对于导致此问题的原因还有其他建议吗?

最佳答案

尝试使用这个 initWithURL:cachePolicy:timeoutInterval: 并将缓存策略设置为NSURLRequestReloadIgnoringCacheData

尝试添加此委托(delegate)方法。

 - (NSCachedURLResponse *)connection:(NSURLConnection *)connection willCacheResponse:(NSCachedURLResponse *)cachedResponse {   
return nil; 
}

确保调用此方法。

使用 NSURLCredentialStorageremoveCredential 删除 NSURLConnection 存储的凭据缓存。

Please check this answer as well.

希望有效

关于iphone - NSURLConnection,使用错误的凭据登录成功,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10135577/

相关文章:

iPhone:多次自动释放对象

ios - 使用 Fabric SDK 的 Twitter 登录按钮

ios - LongPressGestureRecognizer 从 View 背后识别

java - 如何在 https 连接上检索 cookie?

windows - 更改 cookie 文件后 RabbitMQ 节点身份验证失败

javascript - 使用 cookie 检查回访者

iphone - 在不处于编辑模式时重新排序 UITableView 上的控件?

iphone - 隐藏键盘但显示光标

ios - 以编程方式创建的后退按钮未显示在导航栏中?

ios - Q : Custom TableViewCell scroll to not see, 并再次显示,我的自定义行关闭