查看此代码段: -
CommonService objcommonService;
NetworkCredential myCredentials = new NetworkCredential("144552", "F@mous123456");
objcommonService.Credentials = myCredentials;
这是一个C#.net代码。在此代码段中,“objcommonService
”是网络服务“CommonService
”的对象。此Web服务根据其提供的凭据对用户进行身份验证。在C#.NET NetworkCredential
中是.NET的标准类。
如果我想在我的iPhone应用中点击需要这些凭据的网络服务,我应该如何通过?
有没有办法做到这一点,以便我的应用程序的用户不会遇到任何困难,如“身份验证失败!!”当他/她使用我的应用程序时。 webservice预先验证用户身份。我需要在我的应用程序调用服务时传递我的凭据。
答案 0 :(得分:4)
我通过在URLConnection
中使用以下委托方法来实现auth的东西-(void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge{
if ([challenge previousFailureCount] == 0) {
NSURLCredential *newCredential;
newCredential=[NSURLCredential credentialWithUser:username
password:password
persistence:NSURLCredentialPersistenceNone];
[[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
}
}
希望这有帮助