diff --git a/AFNetworking/AFURLConnectionOperation.h b/AFNetworking/AFURLConnectionOperation.h index 94c07e8..0e0b805 100644 --- a/AFNetworking/AFURLConnectionOperation.h +++ b/AFNetworking/AFURLConnectionOperation.h @@ -124,6 +124,18 @@ */ @property (readonly, nonatomic, assign) NSStringEncoding responseStringEncoding; + +///-------------------------------- +/// @name Accessing URL Credentials +///-------------------------------- + +/** + The credential used for authentication challenges in `-connection:didReceiveAuthenticationChallenge:`. + + @discussion This will be overridden by any shared credentials that exist for the username or password of the request URL, if present. + */ +@property (nonatomic, strong) NSURLCredential *credential; + ///------------------------ /// @name Accessing Streams ///------------------------ diff --git a/AFNetworking/AFURLConnectionOperation.m b/AFNetworking/AFURLConnectionOperation.m index 0d27490..0c540bf 100644 --- a/AFNetworking/AFURLConnectionOperation.m +++ b/AFNetworking/AFURLConnectionOperation.m @@ -144,6 +144,7 @@ static inline BOOL AFStateTransitionIsValid(AFOperationState fromState, AFOperat @synthesize totalBytesRead = _totalBytesRead; @dynamic inputStream; @synthesize outputStream = _outputStream; +@synthesize credential = _credential; @synthesize userInfo = _userInfo; @synthesize backgroundTaskIdentifier = _backgroundTaskIdentifier; @synthesize uploadProgress = _uploadProgress; @@ -545,10 +546,10 @@ didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge } else { if ([challenge previousFailureCount] == 0) { NSURLCredential *credential = nil; - + NSString *username = (__bridge_transfer NSString *)CFURLCopyUserName((__bridge CFURLRef)[self.request URL]); NSString *password = (__bridge_transfer NSString *)CFURLCopyPassword((__bridge CFURLRef)[self.request URL]); - + if (username && password) { credential = [NSURLCredential credentialWithUser:username password:password persistence:NSURLCredentialPersistenceNone]; } else if (username) { @@ -556,6 +557,10 @@ didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge } else { credential = [[NSURLCredentialStorage sharedCredentialStorage] defaultCredentialForProtectionSpace:[challenge protectionSpace]]; } + + if (!credential) { + credential = self.credential; + } if (credential) { [[challenge sender] useCredential:credential forAuthenticationChallenge:challenge];