---
page_type: sample
products:
- office-365
languages:
- csharp
extensions:
contentType: samples
createdDate: 1/14/2016 6:35:35 AM
---
# PnP-WOPI
This repository contains an application that integrates with Office Online for viewing/editing Office documents. This type of integration classifies this application as a WOPI host. WOPI (Web Application Open Platform Interface) is a protocol for integrating with Office Online and is documented in detail at [https://wopi.readthedocs.org](https://wopi.readthedocs.org "https://wopi.readthedocs.org"). This sample illustrates many important patterns and practices for implementing a WOPI host, a number of which are outlined in this readme.
This WOPI host implementation is deployed to [https://officewopi.azurewebsites.net](https://officewopi.azurewebsites.net "https://officewopi.azurewebsites.net") and can be used/tested by anyone with an organization/student account registered with Microsoft (read: Office 365 logins). It is provided for testing/experimenting purposes and offered with no service level agreement.
> NOTE: You cannot simply clone and run this sample locally. Integrating with Office Online requires that your host domain is white-listed by Microsoft. The first step to white-listing a domain is to join the Cloud Storage Provider Program detail [HERE](http://dev.office.com/programs/officecloudstorage "HERE"). Additionally, a WOPI host must expose endpoints to the internet that Office Online can communicate with (read: localhost probably won't work).
## Solution Overview ##
This WOPI host sample is written in ASP.NET/C# with MVC for the user interface and Web API for the WOPI endpoints. Although it uses Azure AD for user identity, Azure AD has NOTHING to do with the WOPI integration. A WOPI host can use virtually any identity provider (or function anonymously). The sample stores files in Azure Blob Storage and file metadata in Azure DocumentDB (a NoSQL platform service similar to MongoDB). There a number of configuration values that should be updated in the web.config to support the identity and storage providers:
<!-- These are Azure AD specific properties-->
<add key="ida:ClientId" value="CLIENT ID FROM AZURE AD" />
<add key="ida:ClientSecret" value="CLIENT SECRET FROM AZURE AD" />
<!-- This is the private key to the self-signed cert...probably shouldn't be in config file -->
<add key="CertPassword" value="CERT PRIVATE KEY/PASSWORD"/>
<!-- These are properties for Azure Blob Storage -->
<add key="abs:Protocol" value="AZURE BLOB STORAGE PROTOCOL...LIKELY https" />
<add key="abs:AccountName" value="AZURE BLOB STORAGE ACCOUNT NAME" />
<add key="abs:AccountKey" value="AZURE BLOB STORAGE ACCOUNT KEY" />
<!-- These are properties for DocumentDB -->
<add key="ddb:endpoint" value="DOCUMENTDB ENDPOINT" />
<add key="ddb:authKey" value="DOCUMENTDB AUTH KEY" />
<add key="ddb:database" value="DOCUMENTDB DATABASE NAME" />
A WOPI host is composed of two primary components...a frame to host the Office Online renderings and endpoints that Office Online can call into to perform specific operations (ex: GetFile, PutFile, etc). Both of these components and their unique considerations are detailed below.
## WOPI Host Page ##
The WOPI host page for this sample is found in the **Home/Detail** view with logic in the **Detail** method of the **HomeController.cs**. This page can only be invoked with a WOPI action and a file id. The WOPI action includes details on how to reach Office Online for the desired action (ex: view, edit, etc). The file id is used to look up file details which placed in a form in the Detail view that is posted to the WOPI action URL. When invoked, the controller will also generate a user and file specific access token that is part of the POST to the WOPI action URL.
<form id="office_form" name="office_form" target="office_frame" action='@ViewData["wopi_urlsrc"]' method="post">
<input name="access_token" value='@ViewData["access_token"]' type="hidden" />
<input name="access_token_ttl" value='@ViewData["access_token_ttl"]' type="hidden" />
</form>
<span id="frameholder"></span>
<script type="text/javascript">
var frameholder = document.getElementById("frameholder");
var office_frame = document.createElement("iframe");
office_frame.name = "office_frame";
office_frame.id ="office_frame";
frameholder.appendChild(office_frame);
//Submit the form the WOPI action URL
document.getElementById("office_form").submit();
</script>
Essentially, the WOPI Host Page hosts and posts data to a big IFRAME that renders Office Online.
## WOPI Endpoints ##
The WOPI endpoints should not use the default auth that is configured in Startup.Auth.cs. Remember, Office Online is what calls into these endpoints and it has no dependency on Azure AD. Office Online will pass an access token in the header of all WOPI requests (using the Authorization header). This is the exact same access token that the WOPI Host Page generated a posted to the WOPI action URL. To accomplish this from the same web application, the **WebApiConfig.cs** needs to ignore the default authentication:
// Ignore AAD Auth for WebAPI...will be handled by WopiTokenValidationFilter class
config.SuppressDefaultHostAuthentication();
The application also needs a **AuthorizeAttribute** to validate the access token on requests. This sample implements this in the **WopiTokenValidationFilter** class. The WebAPI routes are all configured with this filter as seen below. The **WopiSecurity** class contains methods to generate and validate our custom access tokens.
[WopiTokenValidationFilter]
[HttpGet]
[Route("wopi/files/{id}")]
public async Task<HttpResponseMessage> Get(Guid id)
{
//Handles CheckFileInfo
return await HttpContext.Current.ProcessWopiRequest();
}
One of the challenges of implementing the WOPI endpoints with WebAPI is that most of the WOPI operations use the same few routes. Operations are instead determined by the header details included on requests. As such, the **filesController** has four generic endpoints that simply call a **ProcessWopiRequest** extension on the HttpContext:
[WopiTokenValidationFilter]
public class filesController : ApiController
{
[WopiTokenValidationFilter]
[HttpGet]
[Route("wopi/files/{id}")]
public async Task<HttpResponseMessage> Get(Guid id)
{
//Handles CheckFileInfo
return await HttpContext.Current.ProcessWopiRequest();
}
[WopiTokenValidationFilter]
[HttpGet]
[Route("wopi/files/{id}/contents")]
public async Task<HttpResponseMessage> Contents(Guid id)
{
//Handles GetFile
return await HttpContext.Current.ProcessWopiRequest();
}
[WopiTokenValidationFilter]
[HttpPost]
[Route("wopi/files/{id}")]
public async Task<HttpResponseMessage> Post(Guid id)
{
//Handles Lock, GetLock, RefreshLock, Unlock, UnlockAndRelock, PutRelativeFile, RenameFile, PutUserInfo
return await HttpContext.Current.ProcessWopiRequest();
}
[WopiTokenValidationFilter]
[HttpPost]
[Route("wopi/files/{id}/contents")]
public async Task<HttpResponseMessage> PostContents(Guid id)
{
//Handles PutFile
return await HttpContext.Current.ProcessWopiRequest();
}
}
Most of the WOPI logic exists in the **WOPIExtensions.cs** and **WOPIUtils.cs** files. The **WOPIExtensions.cs** file contains extension methods for each WOPI operation and the **WOPIUtils.cs** contains utility methods for doing things such as WOPI discovery (which lists all the actions and proof keys for the WOPI integration), validating WOPI proof (ie - proving that the WOPI request actually came from Offi
没有合适的资源?快使用搜索试试~ 我知道了~
PnP-WOPI-master.zip
共70个文件
cs:24个
js:15个
cshtml:8个
需积分: 0 1 下载量 184 浏览量
2024-08-28
22:51:52
上传
评论
收藏 430KB ZIP 举报
温馨提示
office文件读写程序
资源推荐
资源详情
资源评论
收起资源包目录
PnP-WOPI-master.zip (70个子文件)
PnP-WOPI-master
com.microsoft.dx.officewopi
packages.config 3KB
Utils
AzureStorageUtil.cs 9KB
WopiUtil.cs 16KB
DocumentDBRepository.cs 5KB
SettingsHelper.cs 891B
WopiExtensions.cs 35KB
Web.config.backup.1 3KB
Views
Shared
_LayoutWOPI.cshtml 430B
Error.cshtml 192B
_LoginPartial.cshtml 398B
_Layout.cshtml 1KB
Web.config 2KB
Home
Error.cshtml 98B
Detail.cshtml 1KB
Index.cshtml 7KB
_ViewStart.cshtml 54B
Web.Debug.config 1KB
Global.asax 118B
Project_Readme.html 5KB
wopitest.wopitest 14B
App_Start
RouteConfig.cs 620B
FilterConfig.cs 282B
WebApiConfig.cs 799B
BundleConfig.cs 1KB
Startup.Auth.cs 4KB
Startup.cs 272B
Properties
AssemblyInfo.cs 1KB
favicon.ico 31KB
Web.config 5KB
Content
bootstrap.min.css 96KB
bootstrap.css 118KB
Site.css 1KB
Web.Release.config 1KB
Security
WopiSecurity.cs 5KB
WopiTokenValidationFilter.cs 3KB
Scripts
bootstrap.js 58KB
modernizr-2.6.2.js 50KB
jquery.validate-vsdoc.js 42KB
bootstrap.min.js 28KB
filedrop-min.js 18KB
jquery.validate.unobtrusive.min.js 6KB
jquery.validate.js 39KB
respond.js 10KB
respond.min.js 5KB
jquery.validate.min.js 21KB
jquery.validate.unobtrusive.js 19KB
_references.js 684B
jquery-1.10.2.min.map 137KB
jquery-1.10.2.js 268KB
jquery-1.10.2.min.js 92KB
jquery-1.10.2.intellisense.js 156KB
Controllers
HomeController.cs 6KB
filesController.cs 2KB
foldersController.cs 1KB
Global.asax.cs 677B
Models
FileModel.cs 1KB
DetailedFileModel.cs 4KB
Wopi
WopiProof.cs 590B
WopiAction.cs 705B
WopiRequest.cs 482B
WopiRequestType.cs 1KB
fonts
glyphicons-halflings-regular.svg 62KB
glyphicons-halflings-regular.ttf 29KB
glyphicons-halflings-regular.woff 16KB
glyphicons-halflings-regular.eot 14KB
com.microsoft.dx.officewopi.csproj 19KB
LICENSE 1KB
.gitignore 3KB
README.md 8KB
com.microsoft.dx.officewopi.sln 1KB
共 70 条
- 1
资源评论
码力码力我爱你
- 粉丝: 2319
- 资源: 199
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功