ios退出登陆A,更换账号B登陆,请求得到的仍然是A账号的数据

发布于 2022-09-01 17:08:04 字数 2682 浏览 13 评论 0

退出时,做了如下操作:

  • (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);
         }
     }];

    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

愿与i 2022-09-08 17:08:04

应该换一个思路

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

一梦浮鱼 2022-09-08 17:08:04

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

年华零落成诗 2022-09-08 17:08:04

可以先清除缓存文件试试

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文