8

I have made a simple SharePoint client App for iPhone, which require access to some SharePoint web services (mainly /_vti_bin/Lists.asmx). I am having a trouble figuring out how to do this on newer SharePoint environment such as Office365.

With old BPOS environment having forms-based authentication, I was able to authenticate to those services by simply implementing didReceiveAuthenticationChallenge method;

-(void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge {
NSURLCredential *newCredential = [NSURLCredential credentialWithUser:username
                                               password:password
                                            persistence:NSURLCredentialPersistenceForSession];
[[challenge sender] useCredential:newCredential
       forAuthenticationChallenge:challenge];
}

This obviously didn't work any more with SharePoint sites having claims authentication, so I did some research and found out that I need FedAuth cookies to be attached to the request.

http://msdn.microsoft.com/en-us/library/hh147177.aspx

According to this article, with .NET Apps, it seems possible to retrieve those HTTPOnly FedAuth cookies using WININET.dll, but I guess that's not available on iPhone?

Then, I saw SharePlus App presenting UIWebView and getting user to login to their Office365 account first on the browser screen (which is the same concept as explained in "Enabling User Login for Remote Authentication" section of the article above).

So, I tried to see if I can somehow get access to those FedAuth cookies by logging into Office365 account via UIWebView, but [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies] did not let me access HTTPOnly cookies.

Is there a way to achieve claims authentication on iPhone apps without needing designated intermediate .NET service for handling authentications, or requiring user to turn off HTTPOnly property on those cookies?

Sorry, I am very new to SharePoint so I may not even be looking at the right direction, but I would appreciate any advise on getting claims authentication to work on iPhone apps. Thanks in advance!

user1657506
  • 101
  • 1
  • 3

1 Answers1

2

I've figured this out myself. Had to laugh at my own stupidity and impatience.

First of all, [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies] DO let you access HTTPOnly cookies. Though, when user logs into Office 365 on the UIWebView, (void)webViewDidFinishLoad:(UIWebView *)webView delegate method get called several times so you just need to wait until FedAuth appears in the cookies jar.

Here is my (void)webViewDidFinishLoad:(UIWebView *)webView implementation;

- (void)webViewDidFinishLoad:(UIWebView *)webView {

    NSHTTPCookieStorage *storage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
    NSArray *cookiesArray = [storage cookies];
    for (NSHTTPCookie *cookie in cookiesArray) {
        if ([[cookie name] isEqualToString:@"FedAuth"]) {
            /*** DO WHATEVER YOU WANT WITH THE COOKIE ***/
            break;
        }
    }
}

Once you have obtained the cookie, you just need to append that to the NSURLRequest using (void)setAllHTTPHeaderFields:(NSDictionary *)headerFields method when you call SharePoint web services.

Hope this helps someone.

user1657506
  • 101
  • 1
  • 3
  • Awesome! @user1657506, is it possible to obtain FedAuth token without launching UIWebView? Create UIView with 2 text fields (username/password), and send HTTP-request to Identity Provider (Google, Office 365, etc.) to get the token under the hood? – surlac Oct 03 '12 at 12:33
  • Sorry for late reply x( Jumping to the conclusion, it is POSSIBLE but VERY HARD. This site may give you an idea; [Wictor Wilén - SharePoint MCA, MCM and MVP](http://www.wictorwilen.se/Post/How-to-do-active-authentication-to-Office-365-and-SharePoint-Online.aspx). Long story short, you need to call STS and obtain the cookies yourself. I've tried this myself but gave up so can't give you much details... It seems there are resources for .NET that makes this process slightly easier, so creating your own webservice with .NET to handle authentication, etc., might be an option. – user1657506 Oct 15 '12 at 05:47