1) I have created a provider hosted app on premise and able to execute the default code that Visual studio creates in the new default page without any issues. The code is to read the host web title. Now I wrote the below to see the context token.

string contextToken = TokenHelper.GetContextTokenFromRequest(Page.Request);

However the context token returned is NULL. Still the code can create context for host web and read the title. So in which case does the app get a context token and then ask for the access token ?

I am directly running the app using "Start" in visual studio.

2) When I run the app from visual studio it works however when I click on the app from the SharePoint site, it gives a page cannot be displayed error. The URLs it is redirected to are exactly the same !

1 Answer 1


try this code:

            var contextTokenString = TokenHelper.GetContextTokenFromRequest(Page.Request);
            var appWeb = new Uri(clientContext.Web.Url);

            if (contextTokenString != null)
                SharePointContextToken contextToken =
                    TokenHelper.ReadAndValidateContextToken(contextTokenString, Request.Url.Authority);

                string accessToken =
                    TokenHelper.GetAccessToken(contextToken, appWeb.Authority).AccessToken;

Hope this helps!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.