Iis 访问被拒绝/401运行clientContext.ExecuteQuery()时出错

Iis 访问被拒绝/401运行clientContext.ExecuteQuery()时出错,iis,sharepoint,Iis,Sharepoint,我正在创建一个MVC应用程序,它要求我从Sharepoint网站获取组用户。当我通过IIS(本地主机)发布并运行它时,我遇到了一个401错误。不过,当我通过调试器运行它时,一切都按预期运行 我假设这是基于上下文的权限错误。是否有办法以编程方式(而不是像IIS帐户/用户那样)正常使用我当前的NTLM凭据运行应用程序 代码 (我现在在手机上,所以如果有任何东西看起来不可靠,我很抱歉…) //构造函数 clientContext=新的clientContext(“http://sharepointsi

我正在创建一个MVC应用程序,它要求我从Sharepoint网站获取组用户。当我通过IIS(本地主机)发布并运行它时,我遇到了一个401错误。不过,当我通过调试器运行它时,一切都按预期运行

我假设这是基于上下文的权限错误。是否有办法以编程方式(而不是像IIS帐户/用户那样)正常使用我当前的NTLM凭据运行应用程序

代码 (我现在在手机上,所以如果有任何东西看起来不可靠,我很抱歉…)

//构造函数
clientContext=新的clientContext(“http://sharepointsite");
clientContext.Credentials=CredentialCache.DefaultNetworkCredentials;

//Credentials=新的网络凭据(“用户名”、“密码”、“域”) 您可以指定使用CSOM连接到SharePoint的凭据,其外观如下所示:

        string currentUser = System.Security.Principal.WindowsIdentity.GetCurrent().Name;
        System.Security.Principal.WindowsImpersonationContext impersonationContext;
        impersonationContext =
            ((System.Security.Principal.WindowsIdentity)User.Identity).Impersonate();
        currentUser += "Impersonate:" + System.Security.Principal.WindowsIdentity.GetCurrent().Name;
        impersonationContext.Undo();
        ViewBag.U = currentUser;
        return View();
clientContext.Credentials=新的网络凭据(“用户名”、“密码”、“域”)

您可以这样进行MVC模拟:

        string currentUser = System.Security.Principal.WindowsIdentity.GetCurrent().Name;
        System.Security.Principal.WindowsImpersonationContext impersonationContext;
        impersonationContext =
            ((System.Security.Principal.WindowsIdentity)User.Identity).Impersonate();
        currentUser += "Impersonate:" + System.Security.Principal.WindowsIdentity.GetCurrent().Name;
        impersonationContext.Undo();
        ViewBag.U = currentUser;
        return View();
我在这篇文章中发现了这个例子:


我只能访问用户名(可能还有域),我正在尝试对此应用程序使用Windows身份验证。是否有其他解决方案?您可以尝试使用以下方法:NetworkCredential cred=CredentialCache.DefaultNetworkCredentials;这将为您提供当前登录用户的凭据。我已经尝试过了,但不幸的是,它仍然会给我一个401错误。您可以在问题中发布更多的源代码吗?您确定这不是一个正确拒绝访问该帐户的情况吗?如果将context credentials=设置为current users creds,则应引发异常,因为它无法检索凭据,或者身份验证应正常工作。我已更新帖子。如果我硬编码自己的当前凭据,新的NetworkCredentials行可以工作,但是当我使用默认行时,它拒绝了我。