簡體   English   中英

如何從iPhone應用程序中將用戶名和密碼傳遞到Web服務?

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

看看這個代碼片段:

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

這是一個C#.net代碼。 在這個片段中“ objcommonService ”是web服務“的對象CommonService ”。 此Web服務根據提供的憑據對用戶進行預身份驗證。在C#.NET中, NetworkCredential是.NET的標准類。

如果我試圖在我的iPhone應用程序中訪問要求這些憑據的Web服務,該如何通過?

有沒有辦法做到這一點,以使我的應用程序的用戶不會遇到“身份驗證失敗!”之類的問題。 當他/她使用我的應用程序時。 該Web服務會預先驗證用戶身份。我需要在我的應用調用該服務時傳遞我的憑據。

我通過在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
}

}

希望這可以幫助

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM