我正在使用具有简单标头身份验证的Web服务

- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge {
    NSString *userName = [_usernameTextField.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];;
    NSString *passWord  = [_passwordTextfield.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
    if ([challenge previousFailureCount] == 0) {
        //Creating new credintial
        NSURLCredential *newCredential = [NSURLCredential credentialWithUser:userName
                                                                    password:passWord
                                                                 persistence:NSURLCredentialPersistenceForSession];
        [[challenge sender] useCredential:newCredential forAuthenticationChallenge:challenge];
    }
    else {
        CommonCode*objCommon=[[CommonCode alloc]init];
        [_activityIndicator stopAnimating];
        [objCommon showAlert:@"Invalid Password, or no user found with this Email Address"];
    }
}

在注销时,我正在清除可可豆
- (void)resetCredintialCache {
    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[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[urlProtectionSpace][userName];
                [[NSURLCredentialStorage sharedCredentialStorage] removeCredential:cred forProtectionSpace:urlProtectionSpace];
            }
        }
        NSURLCache *sharedCache = [NSURLCache sharedURLCache];
        [sharedCache removeAllCachedResponses];
        NSHTTPCookieStorage *cookieStorage = [NSHTTPCookieStorage sharedHTTPCookieStorage];

        NSArray *cookies = [cookieStorage cookies];
        for (NSHTTPCookie *cookie in cookies) {

            [cookieStorage deleteCookie:cookie];
        }
    }
}

但是注销后,如果输入的密码错误,我将以以前的用户身份登录。如何从HTTP请求的标题中删除cookie?

最佳答案

NSURLRequest具有cachePolicy属性,该属性指定请求的缓存行为。

在发出请求时(例如下面的示例)将从URL而不是从缓存加载数据,请设置以下缓存策略 NSURLRequestReloadIgnoringLocalCacheData

NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:10];

NSURLRequestReloadIgnoringLocalCacheData

指定用于URL加载的数据应从
原始来源。不应使用现有的缓存数据来满足
URL加载请求。

https://developer.apple.com/library/prerelease/mac/documentation/Cocoa/Reference/Foundation/Classes/NSURLRequest_Class/index.html#//apple_ref/c/tdef/NSURLRequestCachePolicy

关于ios - 从http header 中删除cookie?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/30025988/

10-08 20:39