I have a (working) provider hosted app that mainly acts as a REST-Service (MVC5). I want to unit test my Rest-Controllers but can't find out how to give the right credentials.

I already got the URL including crucial parameters the following way:

private string GetBaseUrl(AppInstance myApp, string hostUrl)
    string tokens = string.Format("SPHostUrl={0}&SPLanguage={1}&SPAppWebUrl={2}",
    Uri.EscapeDataString(hostUrl), "en-US", Uri.EscapeDataString(myApp.AppWebFullUrl));
    string url = myApp.StartPage;
    url= url.Replace("{StandardTokens}", tokens);
    string returnUrl = string.Format("{0}{{0}}?{1}", url.Split('?')[0], url.Split('?')[1]);
    return returnUrl;

string hostUrl="http://mySp.sharepoint.com";            
using (context = new ClientContext(hostUrl)) {
    context.Credentials = new SharePointOnlineCredentials(Username, Password);

    var web = context.Web;

    var addInInstance = AppCatalog.GetAppInstances(context, web);

    var myApp = addInInstance.FirstOrDefault(app=>app.Title.ToLower().Contains("myApp"));

    string url=string.Format(GetBaseUrl(myApp, hostUrl),"/api/users";   
    // Returns something like "localhost://443400/api/users?SPHostUrl=http%3A%2F%2F...&SPAppWebUrl=http%3A%2F%2F...&SPLanguage=en-US"

url contains something like "localhost://443400/api/users?SPHostUrl=http%3A%2F%2F...&SPAppWebUrl=http%3A%2F%2F...&SPLanguage=en-US"

I did all this actions just because I needed the right Request-Parameters which are automatically generated when opening the app from inside sharepoint. If I call the Url using a httpRequest

HttpWebRequest request = HttpWebRequest.CreateHttp(url);
request.Credentials=new SharePointOnlineCredentials(Username, password);
var response = (HttpWebResponse)request.GetResponse()

it just does not work as expected. My App is called, but when debugging the app I can see that the spContext is just null (which does not happen when being called from within sharepoint).

When opening the same Url in a browser where I am not logged in the behaviour is the same. So I expect that this has to do something with credentials / userpermissions.

1 Answer 1


SharePoint does a lot of things under the hood before calling your provider-hosted app's REST service. The most important thing - it provides a context token (if you are not familiar with OAuth/OpenId you can imagine that it's a kind of a password) to the REST service, which the service uses to establish a connection back to SharePoint (hence you can use spContext). Since most of this is done under the hood and transparently to you - you do not see it directly in code. More information about what happens when you open a provider-hosted app from SharePoint can be found here: https://msdn.microsoft.com/en-us/library/office/fp142382.aspx

Therefore unit testing a provider hosted REST app is tricky. I don't know the right way to do that, but my suggestion would be to drop the idea and instead test the logic behind your controllers.

I have also found a post in one forum which is marked as an answer on how you can access provider-hosted app pages via code (in javascript, in this case), but it's not for the faint of heart: https://social.msdn.microsoft.com/Forums/office/en-US/e010b8a3-156e-448f-9d69-ba48d0a8233f/launch-application-by-known-url?forum=appsforsharepoint

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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