SharePoint 2010 - System.UnauthorizedAccessException: Access is denied

本文介绍了解决ASP.NET应用访问SharePoint时遇到的权限拒绝错误的方法,包括确保目标框架正确、使用高权限运行代码块及设置应用程序池标识等。

I am sure many would have experienced this issue while connecting to a SharePoint site from an Asp.Net application. You normally would get these errors:

1. System.UnauthorizedAccessException: Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED)) at Microsoft.SharePoint.Utilities.SPUtility.HandleAccessDenied(Exception ex) at Microsoft.SharePoint.Library.SPRequest.GetListsWithCallback(String bstrUrl, Guid foreignWebId, String bstrListInternalName, Int32 dwBaseType, Int32 dwBaseTypeAlt, Int32 dwServerTemplate, UInt32 dwGetListFlags, UInt32 dwListFilterFlags, Boolean bPrefetchMetaData, Boolean bSecurityTrimmed, Boolean bGetSecurityData, Boolean bPrefetchRelatedFields, ISP2DSafeArrayWriter p2DWriter, Int32& plRecycleBinCount) at Microsoft.SharePoint.SPListCollection.EnsureListsData(Guid webId, String strListName) at Microsoft.SharePoint.SPListCollection.GetListByName(String strListName, Boolean bThrowException) at _Default.btnClick_Click(Object sender, EventArgs e) in c:\inetpub\wwwroot\Contoso\Default.aspx.cs:line 35

2. The Web application at http://localhost/sites/SPApplication101/ could not be found. Verify that you have typed the URL correctly. If the URL should be serving existing content, the system administrator may need to add a new request URL mapping to the intended application.

However, if you try to access the same SharePoint application from a console application or windows application (provided the target framework is Any CPU or x64; because, SharePoint 2010 runs in x64 mode only).

Now, there could be following reasons behind these errors.

1. Your ASP.NET application may be targeting to x86 framework and not Any CPU or x64.

2. The user account which is running the ASP.NET application may not have access to SharePoint application.

3. The application pool identify of the application pool of the ASP.NET application doesn’t have the access to SharePoint.

The solution to the above:

1. Make sure your ASP.NET application is targeting x64/Any CPU.

2. Run your code inside the block “SPSecurity.RunWithElevatedPrivileges”.

3. Make sure the application identity of the app pool of ASP.NET application has the access to the SharePoint application which your accessing. Alternatively, you can use the SharePoint’s application with your ASP.NET application.

try

        {

            SPSecurity.RunWithElevatedPrivileges(delegate()

            {

                SPSite mySite = new SPSite("http://localhost/sites/SPApplication101/");

                using (SPWeb myWeb = mySite.OpenWeb())

                {

                    int i = 1;

                    string strFolder = "Folder";

                    mySite.AllowUnsafeUpdates = true;

                    myWeb.AllowUnsafeUpdates = true;

                    SPDocumentLibrary docLibrary = (SPDocumentLibrary)myWeb.Lists["Word Docs"];

                    SPFolderCollection myFolders = myWeb.Folders;

                    if (docLibrary != null)

                    {

                        while (i <= 10)

                        {

                            strFolder = strFolder + " " + i.ToString();

                            if (!myWeb.GetFolder(strFolder).Exists)

                            {

                                myFolders.Add(docLibrary.ParentWebUrl + "/Word%20Docs/" + strFolder + "/");

                                docLibrary.Update();

                            }

                            strFolder = "Folder";

                            i++;

                        }

                    }

                }

            });

        }

        catch (Exception ex)

        {

            Response.Write(ex.ToString());

        }

-------------

Hope this helps many!

<think>嗯,用户遇到了System.UnauthorizedAccessException异常,具体是访问路径'C:\Users\Administrator\Desktop\MA_v2.0.25_b20251126\TestItem\TestItem.csv'被拒绝。这个问题在.NET开发中很常见,通常与文件权限有关。 用户提供的参考引用里有两个解决方案,都是关于配置权限的。第一个引用提到上传文件到共享文件夹时的权限问题,第二个引用详细说明了如何给Authenticated Users添加权限。虽然用户的问题不是共享文件夹而是本地文件,但核心都是权限不足。 用户当前的问题路径在Administrator桌面下,但程序运行时可能不是以管理员身份运行的。即使程序以管理员账号运行,如果权限设置不当也会出问题。需要检查几个关键点:运行程序的账户是否有文件权限,文件是否被其他进程占用,路径是否写错,以及是否触发了UAC保护。 解决方案应该分步骤展开。首先确认文件路径是否正确,然后检查文件是否被占用。接着检查运行程序的账户权限,重点看是否需要对Authenticated Users或NETWORK SERVICE添加权限。如果是IIS托管的应用,还要考虑应用程序池的标识账户权限。 用户可能不太熟悉Windows权限设置,所以步骤要详细。比如在文件夹属性里如何找到安全标签页,如何添加用户组等。同时要提醒用户谨慎设置"完全控制"权限,存在安全风险。 考虑到用户是开发者,可能还需要代码层面的建议,比如使用try-catch处理异常,或者用File.Exists检查文件是否存在。如果文件被占用,可以建议用FileShare.ReadWrite模式打开。 最后要提醒用户,如果问题持续,可能需要检查杀毒软件或文件系统错误。这类问题有时会被安全软件拦截。 </think> 针对 `System.UnauthorizedAccessException` 访问路径 `'C:\Users\Administrator\Desktop\MA_v2.0.25_b20251126\TestItem\TestItem.csv'` 被拒绝的问题,以下是系统化的解决方案,结合引用信息和权限配置逻辑: --- ### **核心原因** 该异常表明**运行程序的账户(进程标识)缺乏对目标文件/文件夹的足够权限**。即使路径在管理员桌面下,应用程序运行时可能并非以管理员身份执行,或权限未正确继承。 --- ### **解决方案步骤** #### **1. 验证文件状态** - **文件是否被占用**:检查 `TestItem.csv` 是否被其他程序(如 Excel、文本编辑器)锁定。关闭所有可能访问该文件的程序。 - **路径是否正确**:确认路径 `MA_v2.0.25_b20251126\TestItem\` 存在且文件名无拼写错误。 #### **2. 配置文件/文件夹权限**(关键步骤) > 参考引用[2]的权限配置逻辑,但需针对本地路径调整: ```mermaid graph TD A[右键点击 TestItem.csv 或父文件夹] --> B[选择“属性” > “安全”] B --> C[点击“编辑”] C --> D[点击“添加”] D --> E[输入“Authenticated Users”] E --> F[勾选“完全控制”] F --> G[应用并确定] ``` **详细操作**: 1. 找到 `TestItem.csv` 或其父目录 `TestItem`,右键 → **属性** → **安全** → **编辑**。 2. 点击 **添加** → **高级** → **立即查找** → 选择 **`Authenticated Users`**。 3. 勾选 **完全控制** 权限 → 应用 → 确定。 4. **如果父目录受限**:对上层文件夹 `MA_v2.0.25_b20251126` 重复上述步骤,并勾选 **“替换子容器和对象的所有者”**。 #### **3. 检查应用程序运行身份** - **调试模式**:若在 Visual Studio 中运行,尝试**以管理员身份启动 VS**(右键 VS 图标 → 以管理员身份运行)。 - **IIS 托管应用**: - 打开 **IIS 管理器** → 选择应用程序池 → 右键对应池 → **高级设置**。 - 将 **标识 (Identity)** 改为 **`LocalSystem`** 或具有管理员权限的账户。 - **独立程序**:右键应用程序 → 属性 → 兼容性 → 勾选 **“以管理员身份运行此程序”**。 #### **4. 处理文件访问冲突** ```csharp // 示例:使用 FileShare 模式避免占用 using (var stream = new FileStream( filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) // 允许其他进程读写 { // 文件操作代码 } ``` #### **5. 验证所有权** - 若权限列表无当前用户: 1. 在 **安全** 选项卡 → **高级** → **所有者** → **编辑**。 2. 将所有者改为当前用户或 `Administrators` 组。 3. 勾选 **“替换子容器和对象的所有者”** → 应用。 #### **6. 禁用只读属性** - 右键文件 → 属性 → 取消 **“只读”** 勾选(若存在)。 --- ### **补充场景** - **杀毒软件拦截**:临时禁用杀软(如 360、Defender)测试是否误拦截。 - **路径长度限制**:缩短路径深度(例如将文件夹移到 `C:\MA`),避免超过 260 字符限制。 - **服务账户权限**:若程序作为服务运行,在 **服务管理器** 中为其配置高权限账户。 --- ### **效果验证** 修改后运行程序,若仍报错: 1. 使用 **Process Monitor** 工具监控文件访问请求,筛选 `ACCESS DENIED` 错误,定位具体拒绝的账户。 2. 在事件查看器(`eventvwr.msc`)中检查 **Windows 日志 → 应用程序**,获取详细错误堆栈。 > **重要提醒**:生产环境中避免滥用“完全控制”权限,遵循最小权限原则。若为共享文件,应配置专用服务账户而非 `Authenticated Users`[^1][^2]。 ---
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值