Getting Access Denied when creating ClientContext without Credentials

25 Views Asked by At

I have an ASP.NET MVC Web API (on .NET 4.7.2) that is published under SharePoint IIS site.

In this Web API, I am trying to create list item. It works fine when I pass credentials in the code - like this:

public static void AddTestElementToSPListAdm()
{
    var AppConf = ConfigurationManager.AppSettings;

    context = new ClientContext(AppConf["spSiteURL"]);
    var credentials = new NetworkCredential("login", "pass", "domain");
    context.Credentials = credentials;

    List list = context.Web.Lists.GetByTitle("testlist");
    ListItemCreationInformation itemCreateInfo = new ListItemCreationInformation();

    ListItem oListItem = list.AddItem(itemCreateInfo);
    oListItem["Title"] = "My New2 Item!";
    oListItem.Update();

    context.ExecuteQuery();
}

But I need to omit credentials to do things under current user account. When I omit the credentials, I get an "Access Denied" error (it fails on context.ExecuteQuery()). I am sure that user has site collection administrator rights.

public static void AddTestElementToSPListAdm()
{
    var AppConf = ConfigurationManager.AppSettings;
    context = new ClientContext(AppConf["spSiteURL"]);
    CreateSpItem();
}

When I debug, it works fine, even when the credentials are omitted.

Is it possible to omit credentials? May be I need to add something in web.config or maybe I miss some attributes in controller?

Also it works fine as a console app from company PC without passing credentials, but once published to IIS, it stops working.

0

There are 0 best solutions below