【问题标题】:Can't get SharePoint access token无法获取 SharePoint 访问令牌
【发布时间】:2014-09-24 16:38:15
【问题描述】:

我在 SharePoint 2013 中使用 AppRegNew.aspx 创建了一个应用程序,从中我获得了一个应用程序密钥和 ID。对于这个应用程序,我使用 AppInv.aspx 设置了对文档列表的读写权限。

现在,我想使用另一个项目中的 app secrete 和 Id 来访问文档列表。我正在使用 TokenHelper 类,如下所示:

TokenHelper.GetAppOnlyAccessToken(TokenHelper.SharePointPrincipal, siteUri.Authority, realm).AccessToken

但我得到一个例外:

远程服务器返回错误:(404) 未找到。 - 请求的命名空间不存在。

我从 TokeHelper 类中调试了代码,发现异常来自这个网络调用:

https://accounts.accesscontrol.windows.net/metadata/json/1?realm=my_realm_value

位于方法中:

private static JsonMetadataDocument GetMetadataDocument(string realm)

任何帮助将不胜感激,谢谢:-)

【问题讨论】:

    标签: c# asp.net sharepoint oauth sharepoint-2013


    【解决方案1】:

    最后我设法弄明白了。我使用了具有以下权限的 SharePoint 应用程序:

    <AppPermissionRequests AllowAppOnlyPolicy="true">
      <AppPermissionRequest Scope="http://sharepoint/content/sitecollection/web/list" Right="Write">
        <Property Name="BaseTemplateId" Value="101"/>
      </AppPermissionRequest>
    </AppPermissionRequests>
    

    除此之外,我使用证书来获取访问令牌。我与应用程序链接的证书并使用 PowerShell 脚本将其设置为信任令牌颁发者:

    $issuerID = [System.Guid]::NewGuid().ToString().ToLower()
    $publicCertPath = "your_cer_file.cer"
    $certificate = Get-PfxCertificate $publicCertPath
    $web = Get-SPWeb "http://your.sharepoint.url/"
    $realm = Get-SPAuthenticationRealm -ServiceContext $web.Site
    $fullAppIdentifier = $issuerId + '@' + $realm
    New-SPTrustedSecurityTokenIssuer -Name "High Trust App" -Certificate $certificate - RegisteredIssuerName $fullAppIdentifier -IsTrustBroker
    $issuerID
    iisreset
    

    然后我像这样使用 TokenHelper 类:

    var siteUri = new Uri("my_site_url");
    TokenHelper.GetS2SAccessTokenWithWindowsIdentity(siteUri, null);
    

    【讨论】:

    • 虽然你得到了它,但这是两种不同的获取令牌的方式
    • 虽然你得到了它,但这是两种不同的获取令牌的方式。您遇到错误的选项是低信任 Oauth 令牌,该令牌失败是因为 GetMetadataDocument(string realm) 内部使用 webclient 与 ACS 通信,如果您在代理环境中工作,则需要像 webClient.Proxy = 那样明确设置代理新的 WebProxy("myproxy.com"); .您开始使用的选项 2 是通过使用证书而不使用 Oauth 使其成为高度信任的应用程序
    猜你喜欢
    • 2020-08-03
    • 2017-11-02
    • 2022-01-01
    • 2016-02-02
    • 1970-01-01
    • 1970-01-01
    • 2023-02-21
    相关资源
    最近更新 更多