SharePoint On Premise Authentication

Eng Soon Cheah 81 Reputation points
2020-10-07T00:25:04.81+00:00

Hi All,

I would like to ask how can I code for SharePoint On Premise Authentication? because I try the get the Calendar RSS from SharePoint On Premise.

Here with my source code.

string key = "iNivDmHLpUXXXXXXXXXXXXj1PVkH";

            var securityKey = new Microsoft
               .IdentityModel.Tokens.SymmetricSecurityKey(Encoding.UTF8.GetBytes(key));

            var credentials = new Microsoft.IdentityModel.Tokens.SigningCredentials
                              (securityKey, SecurityAlgorithms.HmacSha256Signature);

            var header = new JwtHeader(credentials);

            var payload = new JwtPayload
           {


                { "aud", "XChange Subject"},
  { "iss",  Guid.NewGuid().ToString()},
  { "nbf", "USER"},
  { "exp", 1601518113},
  { "nameid", "https://mvponduty.sharepoint.com/XChange_App/"},
  { "actor", "https://mvponduty.sharepoint.com/XChange_App_Api/"},

           };


            var secToken = new JwtSecurityToken(header, payload);
            var handler = new JwtSecurityTokenHandler();

            var tokenString = handler.WriteToken(secToken);


            Console.WriteLine(tokenString);

            GetSharepointEvent(tokenString);
        }

        private void GetSharepointEvent(string tokenString)
        {
            try
            {
                var client = new RestClient("https://XXXX/sites/sg/_layouts/15/listfeed.aspx?List=%7B1F62FC88%2D7821%2D4EDA%2D853C%2D091D3023A99F%7D");
                client.Timeout = -1;
                var request = new RestRequest(Method.GET);
                request.AddHeader("Authorization", "Bearer " + tokenString);
                request.AddHeader("Accept", "application/json;odata=verbose");
                IRestResponse response = client.Execute(request);
                Console.WriteLine(response.Content);

                Label1.Text = response.Content;
            }
            catch(Exception ex)
            {
                Label1.Text = ex.ToString();
            }
        }
SharePoint Server Development
SharePoint Server Development
SharePoint Server: A family of Microsoft on-premises document management and storage systems.Development: The process of researching, productizing, and refining new or existing technologies.
1,609 questions
{count} votes

Accepted answer
  1. Amos Wu-MSFT 4,051 Reputation points
    2020-10-07T02:07:27.423+00:00

    You could use this way to call rest api and you could use DefaultCredentials to authenticate.

     HttpWebRequest endpointRequest = (HttpWebRequest)HttpWebRequest.Create("http://sp/_layouts/15/listfeed.aspx?List=%7B42925B98-CBD0-41F8-B0F5-6D4D85B0A21C%7D");  
                endpointRequest.Method = "GET";  
                endpointRequest.Headers.Add("X-FORMS_BASED_AUTH_ACCEPTED", "f");  
                endpointRequest.Credentials = System.Net.CredentialCache.DefaultCredentials;  
                HttpWebResponse endpointResponse = (HttpWebResponse)endpointRequest.GetResponse();  
                try  
                {  
                    WebResponse webResponse = endpointRequest.GetResponse();  
                    Stream webStream = webResponse.GetResponseStream();  
                    StreamReader responseReader = new StreamReader(webStream);  
                    string response = responseReader.ReadToEnd();//results  
                    responseReader.Close();  
                    Console.WriteLine(response);  
                    Console.ReadLine();  
                }  
                catch (Exception e)  
                {  
                    Console.Out.WriteLine(e.Message); Console.ReadLine();  
                }  
    

    Test result:
    30527-image.png


    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.