File.Open 在具有管理员凭据的 Windows 2008R2 IIS 下无法工作。在 VS2010 Cassini 下工作

发布于 2024-10-14 13:39:02 字数 2261 浏览 3 评论 0原文

下面的代码在 Cassini 下工作正常,但在 IIS 下根本不行。我得到文件未找到,并且无法在远程共享上获取文件,或者在测试 C:\test.pdf(测试 IIS 权限)时在本地获取文件。

此应用程序的目的是创建一个HTTP 代理允许通过安全 URL 检索文件。此示例中省略了安全代码。我只关注这个普通示例中的文件访问。

我已确保

  1. 应用程序池(流程模型身份)正在运行域管理员帐户
  2. 网站物理路径凭据在同一管理员帐户下运行
  3. 管理员帐户同时具有BatchRun as本地策略中的服务权限。

我使用以下 URL

http://localhost:1651/services/GetFile.svc/get?swt=\\remoteserver\share\file.pdf访问 WCF 服务

[ServiceContract(SessionMode = SessionMode.NotAllowed)]
public interface IGetFile
{
    [OperationContract]
    [WebGet(UriTemplate = "/get?swt={filename}", RequestFormat = WebMessageFormat.Xml, ResponseFormat = WebMessageFormat.Xml, BodyStyle = WebMessageBodyStyle.Bare)]
    Stream Get(string filename); 
}


 [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class GetFile : IGetFile
{
    bool debug = true;

    public Stream Get(string filename )
    {
        //this will cause the file dialog to show the file name instead of "get" 
        WebOperationContext.Current.OutgoingResponse.Headers.Add( "Content-disposition", string.Format("inline; filename={0}", filename));
        WebOperationContext.Current.OutgoingResponse.ContentType = "application/octect-stream";

        FileStream fs1= null;

        //WindowsIdentity winId = new WindowsIdentity("[email protected]");
        //using (winId.Impersonate())
        {
            try
            {
                fs1 = File.OpenRead(filename);
            }
            catch (FileNotFoundException e)
            {
                if (debug)
                throw;
                else
                return null;
            }
            catch (IOException e)
            {
                if (debug)
                throw;
                else
                // message: Either a required impersonation level was not provided, or the provided impersonation level is invalid.
                return null;
            }
        }

        return fs1;

The following code works fine under Cassini, but not at all under IIS. I get file not found, and am unable to get files on a remote share, or locally when I tested C:\test.pdf (to test IIS permissions)

The intent of this application is to make a HTTP proxy that will allow files to be retrieved through a secure URL. The security code has been omitted from this sample. I'm just focusing on file access in this plain-vanilla sample.

I've made sure that the

  1. Application pool (Process Model Identity) is running a Domain Admin account
  2. Website Physical Path Credentials are running under the same admin account
  3. The admin account has both Batch and Run as a Service rights in the local policy.

I access the WCF service using the following URL

http://localhost:1651/services/GetFile.svc/get?swt=\\remoteserver\share\file.pdf

[ServiceContract(SessionMode = SessionMode.NotAllowed)]
public interface IGetFile
{
    [OperationContract]
    [WebGet(UriTemplate = "/get?swt={filename}", RequestFormat = WebMessageFormat.Xml, ResponseFormat = WebMessageFormat.Xml, BodyStyle = WebMessageBodyStyle.Bare)]
    Stream Get(string filename); 
}


 [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class GetFile : IGetFile
{
    bool debug = true;

    public Stream Get(string filename )
    {
        //this will cause the file dialog to show the file name instead of "get" 
        WebOperationContext.Current.OutgoingResponse.Headers.Add( "Content-disposition", string.Format("inline; filename={0}", filename));
        WebOperationContext.Current.OutgoingResponse.ContentType = "application/octect-stream";

        FileStream fs1= null;

        //WindowsIdentity winId = new WindowsIdentity("[email protected]");
        //using (winId.Impersonate())
        {
            try
            {
                fs1 = File.OpenRead(filename);
            }
            catch (FileNotFoundException e)
            {
                if (debug)
                throw;
                else
                return null;
            }
            catch (IOException e)
            {
                if (debug)
                throw;
                else
                // message: Either a required impersonation level was not provided, or the provided impersonation level is invalid.
                return null;
            }
        }

        return fs1;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

二智少女猫性小仙女 2024-10-21 13:39:02

在您的测试系统和 cassini 下,您以自己的用户帐户运行,该帐户具有 C:\ 驱动器根目录的权限。在 IIS 下,您在一个特殊帐户下运行,该帐户没有 C:\ 驱动器根目录的权限。将网站所需的文件放置在此位置是一种不好的做法。

Under your test system and cassini, you're running as your own user account, which has permissions to the root of the C:\ drive. Under IIS, you're running under a special account which does not have permissions to the root of the C:\ drive. It's poor practice to place files needed for your web site at this location.

缱倦旧时光 2024-10-21 13:39:02

上面的代码不适用于版本 3.5...仅 4.0

将应用程序池更改为 4.0 修复了问题,并允许我从任何 UNC 读取

The code above doesn't work on version 3.5... just 4.0

Changing the app pool to 4.0 fixed the issue, and allowed me to read from any UNC

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文