I am creating an Azure AD Single tenant Application using asp.net to Provisioning SharePoint site collections. I am able to create site collections as the APP is giving full rights on SharePoint in Azure portal. But when i am trying to access the specific site collection (https://tenant.sharepoint.com/sites/sampletestsite/), getting the Error
Failed to refresh token
Below is the code i am using to get the access token from the azure and creating the SharePoint context.
public ClientContext GetSharePointContext(string SiteUrl)
{
ClientContext spContext = null;
try
{
Task<string> result = Task.Run<string>(async () => await GetAccessToken(SiteUrl));
result.Wait();
if (result != null)
spContext = TokenHelper.GetClientContextWithAccessToken(SiteUrl, result.Result);
}
catch (System.Exception ex)
{
}
return spContext;
}
public async Task<string> GetAccessToken(string siteurl)
{
AuthenticationResult authenticationResult=null;
try
{
string signedInUserID = ClaimsPrincipal.Current.FindFirst(ClaimTypes.NameIdentifier).Value;
string tenantID = ClaimsPrincipal.Current.FindFirst("http://schemas.microsoft.com/identity/claims/tenantid").Value;
string userObjectID = ClaimsPrincipal.Current.FindFirst("http://schemas.microsoft.com/identity/claims/objectidentifier").Value;
// get a token for the Graph without triggering any user interaction (from the cache, via multi-resource refresh token, etc)
ClientCredential clientcred = new ClientCredential(clientId, appKey);
// initialize AuthenticationContext with the token cache of the currently signed in user, as kept in the app's database
AuthenticationContext authenticationContext = new AuthenticationContext(aadInstance + tenantID, new ADALTokenCache(signedInUserID));
authenticationResult = await authenticationContext.AcquireTokenSilentAsync(siteurl, clientcred, new UserIdentifier(userObjectID, UserIdentifierType.UniqueId));
return authenticationResult.AccessToken;
}
catch (System.Exception ex)
{
return authenticationResult.AccessToken;
}
}
First i am passing the https://tenant-admin.sharepoint.com/
url and getting the access token and creating the SharePoint context and creating the site collection,which is working fine. Now again i am passing the site collection url https://tenenat.sharepoint.com/sites/sampletestsite/
and getting the Failed to refresh token Error. Can anyone help me on this.