NSURLConnection - Disable the authentication challenge response mechanism

后端 未结 2 799
心在旅途
心在旅途 2021-02-14 22:26

SITUATION

  • Using AFNetworking (NSURLConnection) to access my server API
  • The API needs Basic Authentication with token as username
相关标签:
2条回答
  • 2021-02-14 22:39

    You have a few of options to fully customize the authentication handling:

    1. Utilize setWillSendRequestForAuthenticationChallengeBlock: and setAuthenticationAgainstProtectionSpaceBlock: from class AFURLConnectionOperation and set corresponding blocks where you can tailor the mechanism you require.

      The headers contain documentation.

    2. Override connection:willSendRequestForAuthenticationChallenge: in a subclass of AFURLConnectionOperation. This will effectively override the complete authentication mechanism setup in AFNetworking.

    Note, that you cannot disable the "server validates client identity" authentication challenge -- this is part of the server. You MUST provide the correct credentials for the requested authentication method.

    0 讨论(0)
  • 2021-02-14 22:54

    Try this.

    - (void)connection:(NSURLConnection *)connection willSendRequestForAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
    {
        SecTrustRef trust = challenge.protectionSpace.serverTrust;
        NSURLCredential *cred;
        cred = [NSURLCredential credentialForTrust:trust];
        [challenge.sender useCredential:cred forAuthenticationChallenge:challenge];
    }
    
    0 讨论(0)
提交回复
热议问题