2009-07-24 71 views
0

,看一下這個代碼片段: -如何從iPhone應用程序中將用戶名和密碼傳遞給Web服務?

CommonService objcommonService;
NetworkCredential myCredentials = new NetworkCredential("144552", "[email protected]");
objcommonService.Credentials = myCredentials;

這是一個C#.NET代碼。在這個片段中,「objcommonService」是webservice「CommonService」的一個對象。此webservice預先根據其提供的憑據對用戶進行身份驗證。在C#中.NET NetworkCredential是.NET的標準類。

如果我想打這需要在我的iPhone應用程序,這些憑據Web服務,我應該如何傳遞?

有沒有辦法做到這一點,使我的應用程序的用戶將不會面臨像任何困難「驗證失敗!」當他/她使用我的應用程序時。 web服務預先驗證user.And我需要通過我的憑據,因爲我的應用程序要求服務。

回答

4

我使用以下的委託方法中的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 
} 

}

希望實現身份驗證的東西這有助於

相關問題