65.9K
CodeProject 正在变化。 阅读更多。
Home

配置 SharePoint 2013 提供程序托管高信任应用程序并部署到单独的 IIS 站点

starIconstarIconstarIconstarIconstarIcon

5.00/5 (4投票s)

2015 年 10 月 9 日

CPOL

3分钟阅读

viewsIcon

27147

此技巧介绍了如何为本地 SharePoint 服务器创建提供程序托管的应用,并将其托管在受信任的 IIS 站点中。

引言

如您所知,提供程序托管的应用模型是 Sharepoint 2013 应用模型之一,其中应用位于 SharePoint 服务器之外。我们可以使用 Azure 网站或 IIS 进行托管。编写此技巧的主要目的是使开发人员了解在 IIS 中托管应用的逐步方法。

逐步方法

为高信任应用创建自签名证书。步骤如下:

  1. 在 IIS 管理器中单击服务器,然后双击服务器证书
  2. 创建自签名证书
  3. 导出为受密码保护的 pfx 文件到文件夹
  4. 双击创建的证书,然后单击“详细信息”选项卡
  5. 单击“复制到文件”,然后按照步骤创建 .cer 文件

配置 Sharepoint 2013 以使用创建的证书

  1. 通过启动 Powershell 并输入 [guid]::newguid().tostring().tolower() 来创建 IssuerId
  2. 将以下 cmdlet 放在 SharePoint shell 命令中
###http://msdn.microsoft.com/en-us/library/fp179901.aspx
$publicCertPath = "C:\root\High_Trust_App_1.cer"
#$issuerId = [System.Guid]::NewGuid().ToString()
$issuerId = ([Guid]"450d02a5-5f69-46ea-9b56-996c9692663d").ToString()
$spurl ="http://sp:1984/sites/Devsite"

$spweb = Get-SPWeb $spurl
$sc = Get-SPServiceContext $spweb.site
$realm = Get-SPAuthenticationRealm -ServiceContext $sc
$certificate = Get-PfxCertificate $publicCertPath

$fullIssuerIdentifier = $issuerId + '@' + $realm

New-SPTrustedSecurityTokenIssuer -Name $issuerId -Certificate $certificate 
	-RegisteredIssuerName $fullIssuerIdentifier –IsTrustBroker
iisreset
write-host "Full Issuer ID: " -nonewline
write-host $fullIssuerIdentifier -ForegroundColor Red
write-host "Issuer ID for web.config: " -nonewline
write-host $issuerId -ForegroundColor Red

#Disable OAuth HTTPS requirement FOR DEV!!
$serviceConfig = Get-SPSecurityTokenServiceConfig
$serviceConfig.AllowOAuthOverHttp = $true
$serviceConfig.Update()


New-SPTrustedRootAuthority -Name "$($certificate.Subject)_$($certificate.Thumbprint)" 
	-Certificate $certificate

注意:在上面的 cmdlet 中,您需要更改 guid、站点 URL 和证书路径。

如果您未使用“https”,请运行此命令

#Disable OAuth HTTPS requirement FOR DEV!!
$serviceConfig = Get-SPSecurityTokenServiceConfig
$serviceConfig.AllowOAuthOverHttp = $true
$serviceConfig.Update()

IIS 站点创建

  1. 在 IIS 中创建一个站点,并在编辑绑定中,使用创建的证书添加“https”
  2. 创建站点后,确保在站点的“身份验证”下禁用“匿名身份验证”并启用“Windows 身份验证”。并检查 Windows 身份验证的提供程序是否包含“NTLM
  3. 选择网站,然后双击目录浏览并启用它。现在你的网站已准备好
    例如:https://:1650/

在 Visual Studio 中创建提供程序托管的应用

  1. 转到 Visual Studio 并单击“新建项目”,然后选择提供程序托管的应用
  2. 在“配置身份验证设置”下,单击“使用证书”并提供上面创建的详细信息
  3. 从 Visual Studio 或 Shell 命令创建“客户端 ID”的 GUID
  4. web.config 下添加以下内容
<appSettings>
    <add key="ClientId" value="450d02a5-5f69-46ea-9b56-996c9692663d" />
    <add key="ClientSigningCertificatePath" value="C:\root\test_cert_1.pfx" />
    <add key="ClientSigningCertificatePassword" value="Pass@123" />
    <add key="IssuerId" value="fda8d804-7ba0-4a00-8dfd-d1fcc36f81a2" />
  </appSettings>

并在 AppManifest 中添加以下内容

<AppPrincipal>
    <RemoteWebApplication ClientId="450d02a5-5f69-46ea-9b56-996c9692663d" />
  </AppPrincipal>

AppManifest 在“权限”选项卡下,将 Web 的权限配置为完全控制。

注册应用主体,即在 SPFarm 中注册 clientid

在 shell 命令中运行以下 cmdlet

$clientId = "450d02a5-5f69-46ea-9b56-996c9692663d"
$spweb = Get-SPWeb "http://sp:1985/sites/DevSite"
$realm = Get-SPAuthenticationRealm -ServiceContext $spweb.Site
$fullAppIdentifier = $clientId + '@' + $realm
$appPrincipal = Register-SPAppPrincipal -NameIdentifier $fullAppIdentifier 
	-Site $spweb -DisplayName "SimpleHTApp"
Set-SPAppPrincipalPermission -Site $spweb -AppPrincipal $appPrincipal -Scope Site -Right FullControl

修改 App 项目中的 TokenHelper.cs

要使用证书,我们需要修改 tokenhelper 以通过其序列号检索它。

删除 ClientSigningCertificatePathClientSigningCertificatePasswordClientCertificate#region private fields 下,并在该位置添加以下行。

Private static readonly string ClientSigningCertificateSerialNumber = 
	WebConfigurationManager.AppSettings.Get("ClientSigningCertificateSerialNumber");

private static readonly X509SigningCredentials SigningCredentials = 
	GetSigningCredentials(GetCertificateFromStore());

#region private methods 下创建一个函数

private static X509Certificate2 GetCertificateFromStore()
{
  if (string.IsNullOrEmpty(ClientSigningCertificateSerialNumber))
  {
     return null;
  }

  // Get the machine's personal store 
  X509Certificate2 storedCert;
  X509Store store = new X509Store(StoreName.My, StoreLocation.LocalMachine);
  try
  {
     // Open for read-only access
     store.Open(OpenFlags.ReadOnly);
     // Find the cert
     storedCert = store.Certificates.Find(X509FindType.FindBySerialNumber,
                                          ClientSigningCertificateSerialNumber,
                                          true)
                                    .OfType<X509Certificate2>().SingleOrDefault(); 
  }
  finally  {
    store.Close();
  }
  return storedCert;
}

 

private static X509SigningCredentials GetSigningCredentials(X509Certificate2 cert)
{
  return (cert == null) 
         ? null 
         : new X509SigningCredentials(cert, 
                                      SecurityAlgorithms.RsaSha256Signature,
                                      SecurityAlgorithms.Sha256Digest); 
}

还要在 tokenhelper 类中将所有 ClientCertificate 替换为 GetCertificateFromStore()

发布 Sharepoint WebApp

  1. 创建一个新的配置文件“SampleProfile
  2. 在连接下,单击“Web 部署包”并给出包位置
  3. 为站点/应用程序指定上述步骤中创建的 https 站点
  4. 单击“发布”

创建加载项包

  1. 单击应用项目上的“发布”
  2. 选择我们在上述步骤中创建的配置文件
  3. 在“托管”选项卡中,提及以下值
    Website : https://:1650 (created in above steps)
    Client ID: 450d02a5-5f69-46ea-9b56-996c9692663d
    Cert location = C:\psmi\test_cert_1.pfx
    Cert password = pass1
    IssuerId = "fda8d804-7ba0-4a00-8dfd-d1fcc36f81a2"
  4. 单击“完成”
  5. app.publish 文件夹,获取“sharepointapp.app”,我们需要在指定 SharePoint 站点下的应用目录中上传它
  6. 在已发布的文件夹中向下钻取 zip 文件并获取内容文件夹。复制 PackageTmp 下的文件夹和文件
  7. 将其放置在 IIS 站点文件夹中 (https://:1650)
  8. 信任应用并查看您的提供程序托管的应用

希望这些步骤能比屏幕截图更清楚地向您解释。

参考文献

© . All rights reserved.