Sharepoint客户端身份验证

问题描述 投票:1回答:3

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

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

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

  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 Token,它允许使用Http Header Authorization连接到Sharepoint API。

有没有在Sharepoint Client中设置Bearer Token来调用Sharepoint Web Services?

谢谢!

sharepoint office365
3个回答
2
投票

以下示例演示了如何在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();
}

0
投票

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

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


0
投票

使用安全设置

 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);
© www.soinside.com 2019 - 2024. All rights reserved.