Quantcast
Channel: SharePoint 2013 - Development and Programming forum
Viewing all articles
Browse latest Browse all 7589

CSOM error 401 Unauthorized

$
0
0

I am calling piece of code from SP2013 workflow as a web service. It is not important what code does, it is important that it works only when I write user credentials explicitly, which is not good.

So, if I do this:

ClientContext ctx = new ClientContext("mySite");

NetworkCredential credentials = new NetworkCredential("myUser", "myPass", "myDomain");
ctx.Credentials = credentials;
ICredentials cred;
Site site1 = ctx.Site;
Web web = ctx.Web;
UserCollection coll = web.SiteUsers;
ctx.Load(coll);
ctx.ExecuteQuery();
foreach (User user in coll) {
      if (user.IsSiteAdmin) {
              cred = user.Context.Credentials;
              ctx.Credentials = cred;
       }

}

It works (but, I gave the user's password here), but if I replace line:

NetworkCredential credentials = new NetworkCredential("myUser", "myPass", "myDomain");

with:

NetworkCredential credentials = CredentialCache.DefaultNetworkCredentials;


it doesn't work. How can I execute my code without passing the password of the user?


Viewing all articles
Browse latest Browse all 7589

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>