【问题标题】:How do I specify Sharepoint credentials?如何指定 Sharepoint 凭据?
【发布时间】:2019-04-30 08:52:53
【问题描述】:

我有一个使用 Sharepoint 的 .NET 应用程序。在应用程序代码中,我使用 ICredentials 指定分配给 ClientContext 对象的 Credentials 属性的用户名和密码。

这很好用,但是,我希望能够从邮递员之类的 REST 工具中访问 Sharepoint,以便更轻松地测试和调试请求。在这种情况下,我不知道如何指定凭据。我尝试发送一个基本的身份验证标头,但没有成功。

弗兰克

【问题讨论】:

    标签: sharepoint


    【解决方案1】:

    示例代码供您参考。

    private static async Task<string> getWebTitle(string webUrl)
            {
                //Creating Password 
                const string PWD = "password";
                const string USER = "user@tenant.onmicrosoft.com";
                const string RESTURL = "{0}/_api/web?$select=Title";
    
                //Creating Credentials 
                var passWord = new SecureString();
                foreach (var c in PWD) passWord.AppendChar(c);
                var credential = new SharePointOnlineCredentials(USER, passWord);
    
                //Creating Handler to allows the client to use credentials and cookie 
                using (var handler = new HttpClientHandler() { Credentials = credential })
                {
                    //Getting authentication cookies 
                    Uri uri = new Uri(webUrl);
                    handler.CookieContainer.SetCookies(uri, credential.GetAuthenticationCookie(uri));
    
                    //Invoking REST API 
                    using (var client = new HttpClient(handler))
                    {
                        client.DefaultRequestHeaders.Accept.Clear();
                        client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
    
                        HttpResponseMessage response = await client.GetAsync(string.Format(RESTURL, webUrl)).ConfigureAwait(false);
                        response.EnsureSuccessStatusCode();
    
                        string jsonData = await response.Content.ReadAsStringAsync();
    
                        return jsonData;
                    }
                }
            }
            static void Main(string[] args)
            {
    
                //Creating Password 
                string webUrl = "https://tenant.sharepoint.com/sites/lee";
                var data=getWebTitle(webUrl).GetAwaiter().GetResult();
                Console.WriteLine("done");
                Console.ReadKey();
            }
    

    【讨论】:

    • 是的,我已经有了这样的代码。我要做的是使用 Postman 或类似的工具,而不是编写更多代码。
    猜你喜欢
    • 2020-10-26
    • 2019-11-09
    • 1970-01-01
    • 2013-01-28
    • 2018-02-09
    • 2016-12-30
    • 2011-03-29
    • 1970-01-01
    相关资源
    最近更新 更多