日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

SharePoint 2013 APP 开发示例 (三)使用远程的web资源

發(fā)布時(shí)間:2025/4/5 编程问答 22 豆豆
生活随笔 收集整理的這篇文章主要介紹了 SharePoint 2013 APP 开发示例 (三)使用远程的web资源 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

在這個(gè)示例里我們將詳細(xì)介紹 TokenHelper 類, 我們將看到它是怎么簡單地從遠(yuǎn)程web站點(diǎn)訪問SharePoint的。我們還將取到它的一些值。這將幫助我們理解連接是怎么被構(gòu)造的,同時(shí)也方便我們的以一的調(diào)試。我們將創(chuàng)建一個(gè)簡單的 auto-hosted app,用TokenHelper類從相關(guān)的SharePoint服務(wù)器讀取數(shù)據(jù),并顯示在頁面上。我們還將取出一些token的值以方便看到它們的內(nèi)容。


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ì)介紹它的使用。


5. 停止調(diào)試
6. 打開Default.aspx.cs
7. 注釋Page_Load() 里已有的代碼
8. 加入下面的引用:

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");

??
client ID 和 secret 是從遠(yuǎn)程web的配置文件讀取到的,ID 指向 app, secret 被用作獲取 access tokens。


10. 加入下面的代碼到Page_Load() :

// 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;



SharePoint 還將傳遞encode? context token, ReadAndValidateContextToken() 方法把它轉(zhuǎn)換成一個(gè) SharePointContextToken 對(duì)象,這樣就更容易訪問它的內(nèi)容, 驗(yàn)證這個(gè)token指的是驗(yàn)證它的地址是來自這個(gè)app。剩下的代碼就是從token里取出一些值.


12. 把這下面這個(gè)方法加到 Default 頁面.

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);}}

13. 加入下面的代碼到Page_Load() :

// 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();

這里我們用access token創(chuàng)建一個(gè)CSOM client context 去請(qǐng)求 sharepoint . 我們這里用同步的ExecuteQuery(),因?yàn)檫@是服務(wù)端的代碼。


16. 加入下面的代碼到Page_Load() 顯示數(shù)據(jù) :

// Dump values to the pageDataTable dt = new DataTable();dt.Columns.Add("Name");dt.Columns.Add("Value");dt.Rows.Add("QueryString", Request.QueryString);dt.Rows.Add("clientID", clientID);dt.Rows.Add("clientSecret", clientSecret);dt.Rows.Add("hostWeb", hostWeb);dt.Rows.Add("contextTokenStr", contextTokenStr);dt.Rows.Add("contextToken", contextToken);dt.Rows.Add("targetPrincipalName", targetPrincipalName);dt.Rows.Add("cacheKey", cacheKey);dt.Rows.Add("refreshTokenStr", refreshTokenStr);dt.Rows.Add("realm", realm);dt.Rows.Add("targetPrincipal", targetPrincipal);dt.Rows.Add("appPrincipal", appPrincipal);dt.Rows.Add("stsUrl", stsUrl);dt.Rows.Add("oauth2Request", oauth2Request);dt.Rows.Add("client", client);dt.Rows.Add("oauth2Response", oauth2Response);dt.Rows.Add("accessTokenStr", accessTokenStr);dt.Rows.Add("Host Web Title", clientContext.Web.Title);grd.DataSource = dt;grd.DataBind();



17. 打開Default.aspx 頁面.
18. 加入下面的GridView :

<asp:GridView ID="grd" runat="server" CellPadding="4" ForeColor="#333333" GridLines="None" AutoGenerateColumns="True" Width="100%"><AlternatingRowStyle BackColor="White" /><EditRowStyle BackColor="#2461BF" /><FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" /><HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" /><PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" /><RowStyle BackColor="#EFF3FB" /><SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" /><SortedAscendingCellStyle BackColor="#F5F7FB" /><SortedAscendingHeaderStyle BackColor="#6D95E1" /><SortedDescendingCellStyle BackColor="#E9EBEF" /><SortedDescendingHeaderStyle BackColor="#4870BE" /></asp:GridView>

19. 按 F5 運(yùn)行APP.
這個(gè)app顯示了遠(yuǎn)程站點(diǎn)的 default 頁面,顯示了這個(gè)站點(diǎn)的token的值。這些值讓我們更方便地調(diào)試我們的app。

?

?

SharePoint 2013 APP 開發(fā)示例 系列

總結(jié)

以上是生活随笔為你收集整理的SharePoint 2013 APP 开发示例 (三)使用远程的web资源的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。