In this example we'll detail the Tokenhelper class, and we'll see how it simply accesses SharePoint from a remote Web site. We will also take some of its values. This will help us understand how the connection is constructed, and it is also convenient for us to debug with one. We'll create a simple auto-hosted app that reads data from the associated SharePoint server and displays it on the page with the Tokenhelper class. We'll also take out some token values to make it easier to see their contents.
1. Open Visual Studio 2012.
2. Create a new C # SharePoint app Project: Remotewebapp.
3. Select autohosted (it is the default).
This will generate two projects. The first one is the SharePoint app Web, and the second is the remote Web site. When debug, the remote Web app will run on the local IIS Express. When passing
Remote Web site. When debugging, the remote Web app would run on a local copy of IIS Express. When deployed
Office Store or an App directory, the remote Web will be published to the Azure cloud.
4. Press F5 to run the app
This page will query the title of the host Web site microphone and display it on the page. It only takes a few lines to do it, because it uses the Tokenhelper class. We will describe its use in detail below.
5. Stop Debugging
6. Open Default.aspx.cs
7. Note the code already in Page_Load ()
8. Add the following reference:
Using Microsoft.IdentityModel.S2S.Protocols.OAuth2;
Using Microsoft.SharePoint.Client;
Using System;
Using System.Collections.Generic;
Using System.Data;
Using System.Globalization;
Using System.Linq;
Using System.Web;
Using System.Web.Configuration;
Using System.Web.UI;
Using System.Web.UI.WebControls;
9. Add the following code to Page_Load ():
Get app info from web.config
string ClientID = string. IsNullOrEmpty (WebConfigurationManager.AppSettings.Get ("ClientId"))
? WebConfigurationManager.AppSettings.Get ("Hostedappname")
: WebConfigurationManager.AppSettings.Get (" ClientId ");
String Clientsecret = String. IsNullOrEmpty (WebConfigurationManager.AppSettings.Get ("Clientsecret"))
? WebConfigurationManager.AppSettings.Get ("Hostedappsigningkey")
: WebConfigurationManager.AppSettings.Get (" Clientsecret ");
The client ID and secret are read from the remote Web configuration file, the ID points to the app, and secret is used to get access tokens.
10. Add the following code to Page_Load ():
Get values from Page.Request
string reqauthority = Request.Url.Authority;
String hostweb = page.request["Sphosturl"];
String hostwebauthority = (new Uri (Hostweb)). authority;
11. Add the following code to Page_Load ():
Get context Token
string contexttokenstr = Tokenhelper.getcontexttokenfromrequest (Request);
Sharepointcontexttoken Contexttoken =
Tokenhelper.readandvalidatecontexttoken (contexttokenstr, reqAuthority);
Read data from the context Token
string targetprincipalname = Contexttoken.targetprincipalname;
string cachekey = Contexttoken.cachekey;
string refreshtokenstr = Contexttoken.refreshtoken;
String realm = Contexttoken.realm;
SharePoint also passes the encode context token, Readandvalidatecontexttoken () method converts it to a Sharepointcontexttoken object, which makes it easier to access its contents. Verify that the token refers to verifying that its address is from this app. The rest of the code is to remove some values from the token.
12. Add the following method to the Default page.
private static string Getformattedprincipal (String principalname, String hostName, String realm)
{
if (! String.IsNullOrEmpty (hostName))
{return
String.Format (CultureInfo.InvariantCulture, "{0}/{1}@{2}", PrincipalName, HostName, realm);
else
{return
String.Format (CultureInfo.InvariantCulture, "{0}@{1}", PrincipalName, realm);
}