Question

I am trying to connect to SharePoint Online with ADAL from a WCF Service that is hosted in Azure as App Service.

I configured the App, got a client ID and Secret, enables AAD auth. This works, if i browse the site in a webbrowser. I can login, get redirected to my Site where the WCF is hosted.

The WCF tries to connect to my SPO site with ADAL. I get the Access Token and add it to the client context like this

ctx_.ExecutingWebRequest += (sender, args) =>
             {
                 AuthenticationResult token = ServiceHelper.GetAccessToken(ClientID, AppKey, TenantIDTitle, TentantID);


                 args.WebRequestExecutor.RequestHeaders["Authorization"] = "Bearer " + token.AccessToken;
             };

There is a a token that I get, so no errors or anything. when I try to load the web and the execute the Context, i get an 401 unauthorized, but I don't get why. Why would I get a token if I am not allowed to access SPO with it.

Am I getting the Token wrong?

var authContext = new AuthenticationContext(AUTHORITY);
var credential = new ClientCredential(CLIENT_ID, CLIENT_SECRET);
var result = (AuthenticationResult)authContext.AcquireTokenAsync(API_ID_URL, credential).Result;
var token = result;
return token;
Was it helpful?

Solution

The problem was that I was doing the whole thing the wrong way: I changed it to first GetAuthorizationRequestUrlAsync to login and then AcquireTokenByAuthorizationCodeAsync with the code I get from the first method.

Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top