0

I'm building a Windows service that will run in a customer network.

This service should upload files to some SharePoint Online document library.

I'm struggling about authentication.

I could store the user/password of a fake user in the tenant in my service configuration, but I don't like the idea of storing user credentials.

I'm trying to understand alternatives. As far as I understand, I may be able to create some app to get an AppId and a secret. I created one in My apps page, but I didn't found how to setup my ClientContext (or rest call) to use these values.

I also didn't found how to grant this application permission in specific libraries in my tenant.

All other documentation I found relate to Addin development.

Everything is mixing in my head.

What is the correct way to grant a windows service permission to some SP365 site ?

0

If you used CSOM, you can do authentication as code below.

    public static void setOnlineCredential(ClientContext clientContext,string userName,string password)
    {
        //set the user name and password
        SecureString secureString = new SecureString();
        foreach (char c in password.ToCharArray())
        {
            secureString.AppendChar(c);
        }
        clientContext.Credentials = new SharePointOnlineCredentials(userName, secureString);           
    }

If you used System.Net to call REST API, you can do authentication as code below

    /// <summary>
    /// Get JSON data of list via REST API
    /// </summary>
    /// <param name="webUri"></param>
    /// <param name="credentials"></param>
    /// <param name="listTitle"></param>
    /// <returns></returns>
    public static JToken GetList(Uri webUri, string userName, string password, string listTitle)
    {
        SecureString secureString = new SecureString();

        foreach (char c in password.ToCharArray())
        {
            secureString.AppendChar(c);
        }

        using (WebClient client = new WebClient())
        {
            client.Headers.Add("X-FORMS_BASED_AUTH_ACCEPTED", "f");
            client.Credentials = new SharePointOnlineCredentials(userName, secureString);
            client.Headers.Add(HttpRequestHeader.ContentType, "application/json;odata=verbose");
            client.Headers.Add(HttpRequestHeader.Accept, "application/json;odata=verbose");
            Uri endpointUri = new Uri(webUri + "/_api/web/lists/getbytitle('" + listTitle + "')");
            string result = client.DownloadString(endpointUri);

            JObject jo = (JObject)JsonConvert.DeserializeObject(result);
            JToken jToken = jo["d"];
            return jToken;
        }
    }
  • thanks, however my question was not how to authenticate using user password, but if there is an alternative that can use for app authentication and not user authentication. – Steve B Jan 21 '18 at 14:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.