OData V4访问安全的URI

时间:2018-02-16 20:35:43

标签: wpf odata

我需要开发一个使用Project Online Server的OData服务的WPF应用程序。当我尝试使用ODataLib V4引用OData服务时,它不起作用,因为OData服务是安全的。

任何人都知道解决此问题的方法吗?

1 个答案:

答案 0 :(得分:0)

我找到了解决方案。

  1. 在xml文件中提取Project Online的元数据(https://[your pwa site] / _ api / ProjectData / $ metadata)
  2. 将xml文件的扩展名更改为.edmx
  3. 在Framework目录中执行DataSvcUtil(DataSvcUtil /in:edmxfile.edmx /out:csfile.cs / language:CSharp)
  4. 在.Net项目中添加csfile.cs
  5. 在项目中添加以下引用:

    - Microsoft.SharePoint.Client
    - Microsoft.SharePoint.Client.DocumentManagement
    - Microsoft.SharePoint.Client.Publishing
    - Microsoft.SharePoint.Client.Runtime
    - Microsoft.SharePoint.Client.Runtime.Windows
    - Microsoft.SharePoint.Client.Taxonomy
    - Microsoft.SharePoint.Client.UserProfiles
    - Microsoft.SharePoint.Client.WorkflowServices
    - Microsoft.SharePoint.WorkflowServices.Activities
    - Microsoft SharePoint Solutions Framework
    
  6. 这里是代码示例:

        private const string PSDATA = "https://[your pwa site]";
    
        ProjectOData.ReportingData context =
                new ProjectOData.ReportingData(new Uri(PSDATA + "/_api/ProjectData/", UriKind.Absolute)) { IgnoreMissingProperties = true };
    
        var username = <username>;
        var password = <password>;
    
        var secureString = new System.Security.SecureString();
        foreach (char c in password.ToCharArray())
        {
            secureString.AppendChar(c);
        }
    
        context.Credentials = new SharePointOnlineCredentials(username, secureString);
    
        SharePointOnlineCredentials credentials = new Microsoft.SharePoint.Client.SharePointOnlineCredentials(username, secureString);
    
        var authenticationCookie = credentials.GetAuthenticationCookie(new Uri(PSDATA));
    
        context.SendingRequest += delegate (object sender, SendingRequestEventArgs e)
        {
            e.RequestHeaders.Clear();
            e.RequestHeaders.Add("Cookie", authenticationCookie);
        };
    
        var projectQuery1 = from p in context.Projects
                            select p;