【问题标题】:How to impersonate my account to service account on SharePoint2010如何在 SharePoint 2010 上将我的帐户模拟为服务帐户
【发布时间】: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


【解决方案1】:

发生错误是因为您试图将 Excel 文件保存到程序文件中的布局文件夹中。出于安全原因,用户上传的文件不应存储在那里。

丢失 Server.MapPath 并将上传的文件保存到 TEMP 目录或例如“D:\UploadedFiles”。

您可以使用 SPSecurity.RunWithElevatedPrivileges 使用 Web 应用程序池标识运行您的代码。这可以帮助您将文件系统文件夹级别的访问权限缩小到应用程序池帐户。

有关此功能的更多详细信息: http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spsecurity.runwithelevatedprivileges.aspx

提到的页面中的一个例子:

SPSecurity.RunWithElevatedPrivileges(delegate()
          // Your code here
});

请注意,您必须在提升的块创建 SPSite 和 SPWeb 对象才能使提升生效(如果需要的话)。

【讨论】:

  • 我试过 RunWithElevatedPrivileges 但它没有用。实际上,我们正在执行的代码是一个 excel 上传功能,当它试图从 excel 中获取数据时,我们的代码会出错。但是,如果我们以服务器的本地管理员身份登录到共享点服务器,则相同的代码可以正常工作。我想知道为什么会这样。您能否建议任何其他选择。我们知道这是一个权限问题,但不确定在哪里提供权限。我已经更新了我的问题。
猜你喜欢
  • 2020-06-18
  • 2020-05-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多