IOS https身份验证 [英] IOS https authentication

查看:193
本文介绍了IOS https身份验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望我的应用程序对设备进行身份验证,即通过https传递用户名和密码。

I want my application to authenticate against appliance i.e passing username and password over https.

我查看了一些示例,基本上构建了以下内容:

I looked at few examples and basically build the following:

-(void)authentication
{
    //setting the string of the url taking from appliance IP.
    NSString *urlString =[NSString 
                          stringWithFormat:
                          @"http://%@",appliance_IP.text];
    //define the url with the string
    NSURL *url = [NSURL URLWithString:urlString];

    //creating request
    NSURLRequest *request = [NSURLRequest requestWithURL:url];

    //setting credential taking from the username and password field
    NSURLCredential *credential = [NSURLCredential credentialWithUser:username.text password:password.text persistence:NSURLCredentialPersistenceForSession];


    NSURLProtectionSpace *protectionSpace=
    [[NSURLProtectionSpace alloc]initWithHost:urlString port:443 protocol:@"https" realm:nil authenticationMethod:nil ];


    [[NSURLCredentialStorage sharedCredentialStorage] setDefaultCredential:credential forProtectionSpace:protectionSpace];

    [ NSURLConnection sendSynchronousRequest:request returningResponse:NULL error:NULL];

}

我使用的例子我需要更多的理解,上面示例中的NSURLConnection不包含用户名和密码,如何添加?所以请求也将包含用户名和密码,或者最好传递该信息。
目前该请求仅包含url字符串。

AS I use examples which I took I need some more understanding, NSURLConnection in the example above does not contain the username and password how do I add it ? so the request will also contain username and password or maybe there is better to pass that information. Currently the request only contain the url string.

谢谢
ER

Thanks ER

推荐答案

在提出要求身份验证的请求时,您将收到didReceiveAuthenticationChallenge的回调,您可按如下方式处理:

When making a request requiring authentication, you will receive a callback to didReceiveAuthenticationChallenge, which you handle as follows:

-(void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(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:MY_USERNAME
                        password:MY_PASSWORD
                        persistence:NSURLCredentialPersistenceForSession];

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

          // inform the user that the user name and password
          // in the preferences are incorrect

          NSLog (@"failed authentication");

          // ...error will be handled by connection didFailWithError
      }
  }
}

这篇关于IOS https身份验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆