File.Open 在具有管理员凭据的 Windows 2008R2 IIS 下无法工作。在 VS2010 Cassini 下工作
下面的代码在 Cassini 下工作正常,但在 IIS 下根本不行。我得到文件未找到
,并且无法在远程共享上获取文件,或者在测试 C:\test.pdf(测试 IIS 权限)时在本地获取文件。
此应用程序的目的是创建一个HTTP 代理允许通过安全 URL 检索文件。此示例中省略了安全代码。我只关注这个普通示例中的文件访问。
我已确保
- 应用程序池(流程模型身份)正在运行域管理员帐户
- 网站物理路径凭据在同一管理员帐户下运行
- 管理员帐户同时具有
Batch
和Run 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
- Application pool (Process Model Identity) is running a Domain Admin account
- Website Physical Path Credentials are running under the same admin account
- The admin account has both
Batch
andRun 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在您的测试系统和 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.
上面的代码不适用于版本 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