Hi All,
I'm developing a Provided-Hosted app for SharePoint 2013. I got items from a list using App Only Policy. Everything works fine.
My problem occurs when I try to use the Search Service Application to execute a query under the context of the app (Elevated privileges).
My code is the following:
var spContext = SharePointContextProvider.Current.GetSharePointContext(HttpContext.Current);
using (var ctx=spContext.CreateAppOnlyClientContextForSPHost())
{
SearchExecutor searchExecutor = new SearchExecutor(ctx);
keywordQuery.QueryText = "path:http://intranet";
var resultItems = searchExecutor.ExecuteQuery(keywordQuery);
ctx.ExecuteQuery();
}
I get the following exception in ULS:
An exception occurred when trying to issue security token
STS Call: Failed to issue new security token. Exception: System.InvalidOperationException: Operation is not valid due to the current state of the object.
at Microsoft.SharePoint.IdentityModel.SPSecurityTokenService.SPRequestInfo.InitializeForFederationAuthType(SPRequestSecurityToken request)
at Microsoft.SharePoint.IdentityModel.SPSecurityTokenService.GetTokenLifetime(Lifetime requestLifetime)
at Microsoft.IdentityModel.SecurityTokenService.SecurityTokenService.Issue(IClaimsPrincipal principal, RequestSecurityToken request)
at Microsoft.SharePoint.IdentityModel.SPSecurityTokenService.Issue(IClaimsPrincipal principal, RequestSecurityToken request)
----------------------------------------------
If I change my code to use the security context of the user, CreateUserClientContextForSPHost, it works. However, I need the app security context to execute this search.
Any clues why I get the error?
Saludos,
Alejandro Perez
Caracas, Venezuela
MCSD MCDBA MCT