【发布时间】:2015-01-03 13:15:09
【问题描述】:
当我以系统管理员身份登录 SharePoint 2010 站点时,我有一段代码正在执行且没有错误。但是我在使用普通用户帐户执行相同操作时遇到问题。有人可以帮助我如何通过使用 C# 代码以不同的 SharePoint 帐户(管理员帐户)登录来执行代码块。
示例:
Using(Domain,LoginID,Password)
{
//Execute Code
//logout as admin
}
SharePoint 2010 上的错误消息:
发生错误:System.UnauthorizedAccessException:对路径“C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\template\layouts\Test\Dev\ABC.xlsx”的访问被拒绝。在 System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath) 在 System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs , String msgPath, Boolean bFromProxy) at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options, String msgPath, Boolean bFromProxy) at System.IO.FileStream..ctor (String path, FileMode mode) at System.Web.HttpPostedFile.SaveAs(String filename) at App.Test.btnFileUpload_Click(Object sender, EventArgs e)
代码示例:
if (fileUpload.HasFile)
{
strTarget = Server.MapPath(fileUpload.FileName);
string[] arrCheckExtension = strTarget.Split('.');
if (arrCheckExtension.Length >= 2)
{
if (arrCheckExtension[1].ToString().Equals("xls") || arrCheckExtension[1].ToString().Equals("xlsx"))
{
fileUpload.SaveAs(strTarget);
strConnForExcel = String.Format(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=""Excel 12.0;HDR=YES;IMEX=1;""", strTarget);
strQueryForExcel = String.Format("select id from [{0}$]", "Test");
OleDbDataAdapter adap = new OleDbDataAdapter(strQueryForExcel, strConnForExcel);
ds = new DataSet();
adap.Fill(ds);
if (ds.Tables[0].Rows.Count > 0)
{
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
if (strids == "")
{
strids += ds.Tables[0].Rows[i]["id"].ToString();
}
else
{
strids += "," + ds.Tables[0].Rows[i]["id"].ToString();
}
}
txtUpload.Text = strids;
}
}
else
{
Response.Write("<script language='javascript'>alert('Please Select File with .xls or xlsx Extension');</script>");
}
}
}
【问题讨论】:
-
也许您应该尝试将上传的文件保存到 SharePoint 的布局文件夹之外的其他位置。这是一个相当大的安全风险...使用临时目录或创建一个新文件夹 (C:\UploadedFiles) 并确保必要的用户对其具有写入权限。如果您知道将存储 excel 的位置,那么您根本不需要 Server.MapPath 命令。
标签: c# .net sharepoint sharepoint-2010 impersonation