afnetworking - ios退出登陆A,更换账号B登陆,请求得到的仍然是A账号的数据
巴扎黑
巴扎黑 2017-04-17 15:41:19
[iOS讨论组]

退出时,做了如下操作:

  • (void)deleteCredient
    {

    //1、
    NSDictionary *credentialsDict = [[NSURLCredentialStorage sharedCredentialStorage] allCredentials];
    if ([credentialsDict count] > 0) {
        // the credentialsDict has NSURLProtectionSpace objs as keys and dicts of userName => NSURLCredential
        NSEnumerator *protectionSpaceEnumerator = [credentialsDict keyEnumerator];
        id urlProtectionSpace;
        // iterate over all NSURLProtectionSpaces
        while (urlProtectionSpace = [protectionSpaceEnumerator nextObject]) {
            NSEnumerator *userNameEnumerator = [[credentialsDict objectForKey:urlProtectionSpace] keyEnumerator];
            id userName;
            // iterate over all usernames for this protectionspace, which are the keys for the actual NSURLCredentials
            while (userName = [userNameEnumerator nextObject]) {
                NSURLCredential *cred = [[credentialsDict objectForKey:urlProtectionSpace] objectForKey:userName];
                [[NSURLCredentialStorage sharedCredentialStorage] removeCredential:cred forProtectionSpace:urlProtectionSpace];
            }
        }
    }
    
    //2、
    NSURLCache *sharedCache = [NSURLCache sharedURLCache];
    [sharedCache removeAllCachedResponses];
    
    //3、
    NSHTTPCookieStorage *cookieStorage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
    NSArray *cookies = [cookieStorage cookies];
    for (NSHTTPCookie *cookie in cookies) {
        [cookieStorage deleteCookie:cookie];
        NSLog(@"deleted cookie");
    }
    [[NSUserDefaults standardUserDefaults]synchronize];
    

    }
    再次登陆时,先获取到当前的登陆账号和密码,account,passwd并缓存到本地,
    所有的请求接口都被封装在如下方法中,采用的是NTLM认证:

  • (void)getWithURL:(NSString )url params:(NSDictionary )params success:(void (^)(id))success failure:(void (^)(NSError *))failure
    {

    
    AFHTTPRequestOperationManager *mgr = [AFHTTPRequestOperationManager manager];
    NSString *account = [ZYFUserDefaults objectForKey:ZYFAccountKey];
    NSString *passwd = [ZYFUserDefaults objectForKey:ZYFPwdKey];
    NSURLCredential *credential =  [NSURLCredential credentialWithUser:[@"XXXXXX\\" stringByAppendingString:account] password:passwd
                                                           persistence:NSURLCredentialPersistenceForSession];
                                                              
    mgr.credential = credential;                                                     
    [mgr GET:url parameters:params
     success:^(AFHTTPRequestOperation *operation, id responseObject) {
         if (success) {
             success(responseObject);
         }
     } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
         if (failure) {
             failure(error);
         }
     }];

    }

巴扎黑
巴扎黑

全部回复(3)
PHP中文网

应该换一个思路

绑定一个Device token,然后随便他怎么换账号都没用。

大家讲道理

登陆时把从服务器获取到的token存起来,之后所有的网络请求等操作都要带上token这个字段。

高洛峰

可以先清除缓存文件试试

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号