1. 打開Visual Studio 2012. 2. 創(chuàng)建一個(gè)新的? C# SharePoint app 項(xiàng)目:RemoteWebApp。 3. 選擇 Autohosted (它是缺省項(xiàng)). 這將生成二個(gè)projects. 第一個(gè)是 SharePoint app web, 第二個(gè)是遠(yuǎn)程web站點(diǎn)。當(dāng)debug時(shí),遠(yuǎn)程的web app 將運(yùn)行在本地的IIS Express上。當(dāng)通過 remote web site. When debugging, the remote web app will run on a local copy of IIS Express. When deployed Office Store 或一個(gè) App 目錄,遠(yuǎn)程的web將被發(fā)布到 Azure 云。
? 4. 按F5運(yùn)行app
這個(gè)頁面將查詢host web 站咪的title,并顯示在頁面上。它只用了幾行就做到了,因?yàn)樗褂昧薚okenHelper 這個(gè)類。我們下面將詳細(xì)介紹它的使用。
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 哪個(gè)app正在做請(qǐng)求; target principal 確認(rèn)哪個(gè)application, host 和 realm 將收到請(qǐng)求。
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的關(guān)鍵, 這段代碼請(qǐng)求 OAuth access token 并把它送到 Access Control Service (ACS). ACS發(fā)布了 access token 并且把它返回到遠(yuǎn)程的。這里能用同步調(diào)用,因?yàn)樵诜?wù)端而不是在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();