iphone - iOS5:通过 https 进行身份验证

标签 iphone authentication ios5 ssl http-status-code-404

我在 iPhone 应用程序中使用以下代码通过 https 与服务器进行身份验证。

身份验证正在 Web 浏览器上进行,但是当我尝试从 iPhone 应用程序进行身份验证时,服务器响应代码为 404。 使用下面的代码,通过 http 从 iPhone 应用程序成功进行身份验证。

所以我想知道 iOS 5 中的 https 身份验证是否有重大变化。

我的代码如下。

-(void)connection:(NSURLConnection *)connection willSendRequestForAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
{
   if ([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust])
   {
       if ([challenge.protectionSpace.host isEqualToString:MY_PROTECTION_SPACE_HOST])
       {
           [challenge.sender useCredential:[NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust] forAuthenticationChallenge:challenge];
       }

       [challenge.sender continueWithoutCredentialForAuthenticationChallenge:challenge];
   }
   else if ([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodHTTPBasic])
   {
      if ([challenge previousFailureCount] == 0)
      {
          NSURLCredential *newCredential;

          newCredential = [NSURLCredential credentialWithUser:@"username"
                        password:@"password"
                        persistence:NSURLCredentialPersistenceForSession];

          [[challenge sender] useCredential:newCredential forAuthenticationChallenge:challenge];
      }
      else
      {
          [[challenge sender] cancelAuthenticationChallenge:challenge];


          NSLog (@"failed authentication");

         
      }
  }
}

提前致谢。

最佳答案

对于通过 https 进行身份验证,您可以尝试此代码。 更多详情可以从http://as.wiley.com/WileyCDA/WileyTitle/productCd-1119961327,descCd-DOWNLOAD.html下载源代码并查看其中的 iHotelApp。

self.networkQueue = [ASINetworkQueue queue];
  [self.networkQueue setMaxConcurrentOperationCount:6];
  [self.networkQueue setDelegate:self];
  [self.networkQueue go];

  ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:[NSURL URLWithString:LOGIN_URL]];

    [request setUsername:loginName];
    [request setPassword:password]; 

  [request setDelegate:self];

    [request setDidFinishSelector:@selector(loginDone:)];
    [request setDidFailSelector:@selector(loginFailed:)];   

    [self.networkQueue addOperation:request];

关于iphone - iOS5:通过 https 进行身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11419995/

相关文章:

ios - xcode 7.3 swift 哪些 UI View 构成了该显示的结构?

Java - javax.net.ssl.SSLPeerUnverifiedException : peer not authenticated

iphone - iOS 5 的潜在(大概) future 使用趋势是什么?

objective-c - 如何将 JSON 对象保存到 Core Data?

iphone - iOS/iPhone-单击另一个按钮时隐藏一个按钮

iphone - IOS 在 AppDelegate 中访问模态视图

ios - 如何在应用程序终止或终止时点击推送通知后打开特定 Controller

javascript - 针对 Liferay 验证登录 p_auth token

authentication - WINdows Phone 10 Google Web 授权代理

升级到 Xcode 4.5 后,iOS 5 SDK 消失了