I created an provider hosted app and use SP2013 on-prem and ADFS authentication. I have an app part with some form. This app part is added to a page in a webpart zone. Initial this app part is working. You will see the form and can submit. But when the browser is inactive for example 15 minutes and you go again to the page with the app part, you will get the error:
The remote server returned an error (401) Unauthorized.
It is not fixed after 15 minutes. It looks like it is random.
A work around is to log off, and then log in. Then everything is working fine. Is there some cookie/session/token expired period which is doing this behavior?
We implemented this ADFS implementation.
In the ULS I found this information which is related to this error:
got client_id: i:0i.t|ms.sp.ext|0aeb5a99-dcc6-4208-9398-a4fc10272c6b@3a5e94a9-2edd-42b3-9fca-5ef76e45719f and instance_id: https://apps.myCompany.nl/PCDocument/Create?SPHostUrl=https%3A%2F%2Fbm%2EmyCompany%2Enl&SPHostTitle=myCompany&SPAppWebUrl=""&SPLanguage=nl%2DNL&SPClientTag=1&SPProductNumber=15%2E0%2E4841%2E1000&SenderId=960DD4660 from query string.
looking for app instance that matches client_id: i:0i.t|ms.sp.ext|0aeb5a99-dcc6-4208-9398-a4fc10272c6b@3a5e94a9-2edd-42b3-9fca-5ef76e45719f
The current user has System.Threading.Thread.CurrentPrincipal.Identity.Name = 05.t|adfs|[email protected], System.Security.Principal.WindowsIdentity.GetCurrent().Name = NT AUTHORITY\IUSR, System.Web.HttpContext.Current.User.Identity.Name = 05.t|adfs|[email protected].
app instance found whose AppPrincipalId matches client_id i:0i.t|ms.sp.ext|0aeb5a99-dcc6-4208-9398-a4fc10272c6b@3a5e94a9-2edd-42b3-9fca-5ef76e45719f
redirectLaunUrl after getting it from query string, web or app instance: https://apps.myCompany.nl/PCDocument/Create?SPHostUrl=https%3A%2F%2Fbm%2EmyCompany%2Enl&SPHostTitle=myCompany&SPAppWebUrl=""&SPLanguage=nl%2DNL&SPClientTag=1&SPProductNumber=15%2E0%2E4841%2E1000&SenderId=960DD4660
trying to get app tokens for site: 5998b5cd-6cef-41b4-b36a-eca33cbfbb03 Unable to load app tokens from appInstanceId: 05138aae-7267-455e-a630-a82098a5185e
redirectLaunUrl after getting token replacement: https://apps.myCompany.nl/PCDocument/Create?SPHostUrl=https%3A%2F%2Fbm%2EmyCompany%2Enl&SPHostTitle=myCompany&SPAppWebUrl=""&SPLanguage=nl%2DNL&SPClientTag=1&SPProductNumber=15%2E0%2E4841%2E1000&SenderId=960DD4660
m_oauthAppId after NormalizeAppIdentifier() i:0i.t|ms.sp.ext|0aeb5a99-dcc6-4208-9398-a4fc10272c6b@3a5e94a9-2edd-42b3-9fca-5ef76e45719f. Now getting app principal info.
decided that we need to do a POST to the app.
App token requested from appredirect.aspx for site: 5998b5cd-6cef-41b4-b36a-eca33cbfbb03 but there was an error in generating it. This may be a case when we do not need a token or when the app principal was not properly set up. LaunchUrl:https://apps.myCompany.nl/PCDocument/Create?SPHostUrl=https://bm.myCompany.nl&SPHostTitle=myCompany&SPAppWebUrl=""&SPLanguage=nl-NL&SPClientTag=1&SPProductNumber=15.0.4841.1000&SenderId=960DD4660 Exception Message:De app i:0i.t|ms.sp.ext|0aeb5a99-dcc6-4208-9398-a4fc10272c6b@3a5e94a9-2edd-42b3-9fca-5ef76e45719f has no endpoint or endpoint is not valid. Stacktrace: at Microsoft.SharePoint.ApplicationPages.AppRedirectPage.ValidateAndProcessRequest(). Since this is a nonfatal error, it will be sanitized and posted to the app as part of the app launch.
Getting Error Message for Exception Microsoft.SharePoint.SPException: The app i:0i.t|ms.sp.ext|0aeb5a99-dcc6-4208-9398-a4fc10272c6b@3a5e94a9-2edd-42b3-9fca-5ef76e45719f has no endpoint or endpoint is not valid. at Microsoft.SharePoint.ApplicationPages.AppRedirectPage.ValidateAndProcessRequest()
Doing appredirect from appredirect.aspx: in site: 5998b5cd-6cef-41b4-b36a-eca33cbfbb03 with RedirectLaunchUrl: https://apps.myCompany.nl/PCDocument/Create?SPHostUrl=https%3A%2F%2Fbm%2EmyCompany%2Enl&SPHostTitle=myCompany&SPAppWebUrl=""&SPLanguage=nl%2DNL&SPClientTag=1&SPProductNumber=15%2E0%2E4841%2E1000&SenderId=960DD4660
UPDATE
Here are my modified TokenHelper.cs and SharePointContext.cs files.
UPDATE
I was triggered by Sergei Sergeev to increase the outer token lifetime. I did some investigation and this is the fix. The outer token lifetime (10 minutes) needs to be higher than the HighTrustAccessTokenLifetime (12 hours). Another reason why it was not working in our situation, is because we save the SharePointHighTrustSamlContext in the session like they do for SharePointAcsContext and SharePointHighTrustContext. In this example of Tobias Lekman the tokens will always recreated for each new request and you dont have a dependancy with the lifetime of the tokens.