【问题标题】:Sharepoint Client AuthenticationSharepoint 客户端身份验证
【发布时间】:2015-03-20 14:15:03
【问题描述】:

我正在开发一个使用 Office 365 身份验证的 Web 应用程序。

我需要访问用户的 SharePoint 文件。该应用程序是多租户应用程序,这意味着我不知道 Sharepoint URL,但我可以使用 Microsoft Discover API 来发现当前用户的 Sharepoint URL。

我想使用Microsoft.Sharepoint.Client 库访问共享点文件。考虑以下代码:

ClientContext context = new ClientContext("https://discoveredserver.sharepoint.com");

// The SharePoint web at the URL.
Web web = context.Web;

// We want to retrieve the web's properties.
context.Load(web);

我收到 403 未授权,因为客户端对象没有凭据。问题是我无法在运行时设置凭据,因为我没有它,我唯一拥有的是 Bearer 令牌,它允许使用 HTTP 标头授权连接到 Sharepoint API。

有什么方法可以在 Sharepoint 客户端中设置 Bearer 令牌来调用 Sharepoint Web Services?

【问题讨论】:

    标签: sharepoint office365


    【解决方案1】:

    以下示例演示如何在ClientContext 中显式指定Bearer Token

    public static ClientContext GetClientContext(Uri webUri)
    {
        var ctx = new ClientContext(webUri);
        ctx.ExecutingWebRequest += delegate(object sender, WebRequestEventArgs e)
         {
             string realm = TokenHelper.GetRealmFromTargetUrl(webUri); //get the realm 
             string accessToken = TokenHelper.GetAppOnlyAccessToken(TokenHelper.SharePointPrincipal, webUri.Authority, realm).AccessToken; //get access token
             e.WebRequestExecutor.WebRequest.Headers.Add("Authorization", "Bearer " + accessToken);
         };
         return ctx;
     }
    

    用法

    using (var ctx = GetClientContext(webUri))
    {
        ctx.Load(ctx.Web);
        ctx.ExecuteQuery();
    }
    

    【讨论】:

      【解决方案2】:

      SharePoint 提供程序托管的应用程序使用的基于 ACS 的访问令牌不能用于其他服务,例如发现服务。

      http://blogs.msdn.com/b/kaevans/archive/2015/03/20/an-architecture-for-sharepoint-apps-that-call-other-services.aspx

      如果您需要使用发现服务或其他受 Azure AD 保护的服务,您需要先使用 Azure AD 对用户进行身份验证。

      http://blogs.msdn.com/b/kaevans/archive/2015/03/23/using-openid-connect-with-sharepoint-apps.aspx

      通过身份验证后,您需要请求特定于所请求资源的访问令牌。我的示例显示了 Exchange Online,但您可以将其更改为轻松使用 SharePoint Online API。

      http://blogs.msdn.com/b/kaevans/archive/2015/03/23/call-o365-exchange-online-api-from-a-sharepoint-app.aspx

      【讨论】:

        【解决方案3】:

        使用安全设置

         string pass="your password"
         SecureString password = new SecureString();
                foreach (var item in pass.ToCharArray())
                {
                    password.AppendChar(item);
                }
        var ctx = new ClientContext("yourSiteName");
        
        ctx.Credentials = new SharePointOnlineCredentials(username, password);
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2013-03-22
          • 2016-07-09
          • 1970-01-01
          • 1970-01-01
          • 2022-06-23
          • 2011-04-09
          • 2013-10-07
          相关资源
          最近更新 更多