1. 打開Visual Studio 2012. 2. 創建一個新的? C# SharePoint app 項目:RemoteWebApp。 3. 選擇 Autohosted (它是缺省項). 這將生成二個projects. 第一個是 SharePoint app web, 第二個是遠程web站點。當debug時,遠程的web app 將運行在本地的IIS Express上。當通過 remote web site. When debugging, the remote web app will run on a local copy of IIS Express. When deployed Office Store 或一個 App 目錄,遠程的web將被發布到 Azure 云。
? 4. 按F5運行app
這個頁面將查詢host web 站咪的title,并顯示在頁面上。它只用了幾行就做到了,因為它使用了TokenHelper 這個類。我們下面將詳細介紹它的使用。
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. 加入下面的代碼到Page_Load() :
// Get app info from web.configstring 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");
// Get values from Page.Requeststring reqAuthority = Request.Url.Authority;string hostWeb = Page.Request["SPHostUrl"];string hostWebAuthority = (new Uri(hostWeb)).Authority;
11. 加入下面的代碼到Page_Load() :
// Get Context Tokenstring contextTokenStr = TokenHelper.GetContextTokenFromRequest(Request);SharePointContextToken contextToken =TokenHelper.ReadAndValidateContextToken(contextTokenStr, reqAuthority);// Read data from the Context Tokenstring targetPrincipalName = contextToken.TargetPrincipalName;string cacheKey = contextToken.CacheKey;string refreshTokenStr = contextToken.RefreshToken;string realm = contextToken.Realm;
// Create principal and client stringsstring targetPrincipal = GetFormattedPrincipal(targetPrincipalName, hostWebAuthority, realm);string appPrincipal = GetFormattedPrincipal(clientID, null, realm);
app principal 哪個app正在做請求; target principal 確認哪個application, host 和 realm 將收到請求。
14. 加入下面的代碼到Page_Load() :
// Request an access token from ACSstring stsUrl = TokenHelper.AcsMetadataParser.GetStsUrl(realm);OAuth2AccessTokenRequest oauth2Request =OAuth2MessageFactory.CreateAccessTokenRequestWithRefreshToken(appPrincipal, clientSecret, refreshTokenStr, targetPrincipal);OAuth2S2SClient client = new OAuth2S2SClient();OAuth2AccessTokenResponse oauth2Response = client.Issue(stsUrl, oauth2Request) as OAuth2AccessTokenResponse;string accessTokenStr = oauth2Response.AccessToken;
這是連接回到host web的關鍵, 這段代碼請求 OAuth access token 并把它送到 Access Control Service (ACS). ACS發布了 access token 并且把它返回到遠程的。這里能用同步調用,因為在服務端而不是在sharepoint。
15. 加入下面的代碼到Page_Load() :
// Build the CSOM context with the access tokenClientContext clientContext = TokenHelper.GetClientContextWithAccessToken(hostWeb, accessTokenStr);clientContext.Load(clientContext.Web, web => web.Title);clientContext.ExecuteQuery();