简体   繁体   English

如何从iPhone应用程序中将用户名和密码传递到Web服务?

[英]How to pass username and password to a web service from within an iPhone app?

Have a look to this code snippet:- 看看这个代码片段:

CommonService objcommonService;
NetworkCredential myCredentials = new NetworkCredential("144552", "F@mous123456");
objcommonService.Credentials = myCredentials;

This is a C#.net code. 这是一个C#.net代码。 In this snippet " objcommonService " is a object of the webservice " CommonService ". 在这个片段中“ objcommonService ”是web服务“的对象CommonService ”。 This webservice pre authenticates the user against its supplied credentials.In C#.NET NetworkCredential is a standard class of the .NET . 此Web服务根据提供的凭据对用户进行预身份验证。在C#.NET中, NetworkCredential是.NET的标准类。

If I am trying to hit a webservice which requires these credentials in my iPhone app , How should I pass? 如果我试图在我的iPhone应用程序中访问要求这些凭据的Web服务,该如何通过?

Is there any way to do it so that the user of my app will not face any difficulty like "Authentication Failed !!" 有没有办法做到这一点,以使我的应用程序的用户不会遇到“身份验证失败!”之类的问题。 when he/she uses my app. 当他/她使用我的应用程序时。 The webservice pre authenticates the user.And I need to pass my credentials as my app calls for the service. 该Web服务会预先验证用户身份。我需要在我的应用调用该服务时传递我的凭据。

I implement the auth stuff by using the following delegate method in URLConnection 我通过在URLConnection中使用以下委托方法来实现身份验证内容

-(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
}

} }

Hope this helps 希望这可以帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM