<?PHPif (!isset($_SERVER['PHP_AUTH_USER'])) { header('WWW-Authenticate: Basic realm="My Realm"'); header('http/1.0 401 Unauthorized'); //Stuff that users will see if they click 'Cancel' exit;}else { //ValIDation Code echo "You entered info.";}?>
在这一点上,我正在使用一个同步的NSURLConnection,我知道苹果文档状态对认证的支持较少。
但是,甚至有可能吗?我可以很容易地进行cookie身份验证NSURLProtectionSpaces或NSURLCredentials或任何认证类。还有,有什么资源可以在这里阅读有关可可认证类的更多信息?
谢谢。
更新:mikeabdullahuk
你提供的代码(第二个例子)与我所写的几乎相同。我已经做了更多的调查,并发现NSURLConnection返回错误…
Error Domain=NSURLErrorDomain Code=-1012 UserInfo=0x1a5170 "Operation Could not be completed. (NSURLErrorDomain error -1012.)"
代码对应于NSURLErrorUserCancelledAuthentication。显然,我的代码没有访问NSURLCredentialStorage,而是取消认证。这可能与PHP http身份验证功能有关吗?在这一点上我很困惑
解决方法 同步NSURLConnection将绝对与NSURLCredentialStorage配合使用。以下是通常工作的方式:> NSURLConnection从服务器请求页面
>服务器回复401响应
NSURLConnection看起来可以看到从URL获取的凭据
>如果URL没有提供完整的凭据(用户名和密码),NSURLConnection还将参考NSURLCredentialStorage填写空白
>如果完整凭据尚未确定,NSURLConnection将发送-connection:dIDReceiveAuthenticationChallenge:委托方法请求凭据
>如果NSURLConnection现在终于有完整的凭据,它将重试原始请求,包括授权数据。
通过使用同步连接方法,您只能在步骤5中输入提供自定义身份验证的能力。因此,您可以在URL中预先提供身份验证凭据,也可以在发送请求之前将其置于NSURLCredentialStorage中。例如
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://user:[email protected]"]];[NSURLConnection sendSynchronousRequest:request returningResponse:NulL error:NulL];
要么:
NSURLCredential *credential = [NSURLCredential credentialWithUser:@"user" password:@"pass" persistence:NSURLCredentialPersistenceForSession];NSURLProtectionSpace *protectionSpace = [[NSURLProtectionSpace alloc] initWithHost:@"example.com" port:0 protocol:@"http" realm:nil authenticationMethod:nil];[[NSURLCredentialStorage sharedCredentialStorage] setDefaultCredential:credential forProtectionSpace:protectionSpace];[protectionSpace release];NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://example.com"]];[NSURLConnection sendSynchronousRequest:request returningResponse:NulL error:NulL];总结
以上是内存溢出为你收集整理的objective-c – 我可以使用NSURLCredentialStorage进行HTTP基本身份验证吗?全部内容,希望文章能够帮你解决objective-c – 我可以使用NSURLCredentialStorage进行HTTP基本身份验证吗?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)