csom context credential security

804 Views Asked by At

I have a task schedular in which i am connceting to sharepoint site and fetching some data using csom script c# and after processing sending the mails to users.

i am creating the context by passing user credential which are stored in app.config file as below code.

  ClientContext context = new ClientContext(siteUrl);
            context.RequestTimeout = int.MaxValue;
            System.Net.ServicePointManager.ServerCertificateValidationCallback = ((sender, certificate, chain, sslPolicyErrors) => true);
            var credentials = new NetworkCredential(siteUserName, siteUserPassword, siteDomain);
            Web web = context.Web;
            context.Load(web, w => w.Title);
            context.Credentials = credentials;
            return context;

how we can store these credentials other than config file for security?

2

There are 2 best solutions below

2
On

You can store the credentials in a database, and retrieve them when you want to create the ClientContext.

0
On

You can store the credentials in Windows Credentials Manager, then use this library to get the ICredentials object

ICredentials credential = CredentialManager.GetSharePointOnlineCredential("site url");

By doing this, you let Windows handles the encryption and storing of username and password.