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;
APP_ID_URL
is probably correct but note that it should be the URL of the resource you are requesting (In your case SharePoint). You have APP_ID and resource ID as same, may be try changing your APP_ID to something else (can be anything e.ghttps://your-azure-tenant.com/this-app-name
) and pass the SP URL in yourAccquireTokenAsync
Your authority should be of the formlogin.microsoftonline.com/tenantID
client and client secret should be from the Azure AD app you registered.