简体   繁体   English

Windows Azure云服务-拒绝访问已部署的xml文件

[英]Windows Azure Cloud Service - Access to deployed xml file denied

I have an MVC app which runs fine on premise, it runs fine on azure web sites. 我有一个MVC应用程序,该应用程序在内部可以很好地运行,在Azure网站上可以很好地运行。 But when deployed as a cloud service I have access denied problems accessing certain xml files. 但是当部署为云服务时,我无法访问某些xml文件。

These xml files are just bits of data required for the application which help determine certain settings within the application. 这些xml文件只是应用程序所需的数据位,有助于确定应用程序内的某些设置。

There is a large number of these under a hierarchical folder structure, as the idea is that each of these get processed to allow an inheritance of these settings I need within the application. 在分层文件夹结构下有很多这样的应用程序,因为它们的想法是要对它们中的每一个进行处理,以允许继承应用程序中所需的这些设置。 But anyway that is largely irrelevant as at the end of the day these are simply xml files stored in a folder under the web root. 但是无论如何,与一天结束时几乎没有任何关系,这些只是存储在Web根目录下的文件夹中的xml文件。

The build action properties of the xml files are set to content so that they do get deployed with a publish. xml文件的构建操作属性设置为content,以便通过发布进行部署。 In fact I have RDP'd onto the cloud service VM just to check the xml files are getting deployed and can confirm they are. 实际上,我已经将RDP部署到云服务VM上只是为了检查xml文件是否已部署并可以确认它们是否已部署。

However whenever the application tried to read one of these files i get the following access denied error. 但是,只要应用程序尝试读取这些文件之一,我都会收到以下访问被拒绝错误。

Access to the path 'E:\\sitesroot\\0\\Templates\\Applications\\ControlProperties.xml' is denied. 拒绝访问路径“ E:\\ sitesroot \\ 0 \\ Templates \\ Applications \\ ControlProperties.xml”。

(Note : This is not a hard coded path as assumed by the answer below, I am using HttpContext.Current.Server.MapPath to determine the physical path of the file relative to the web root) (注意:这不是下面的答案所假定的硬编码路径,我正在使用HttpContext.Current.Server.MapPath来确定文件相对于Web根的物理路径)

This is just the standard access denied error indicating the application does not have access to read the file. 这只是标准访问拒绝错误,表明应用程序无权读取文件。

Now if I RDP again to the machine and grant everybody full access to this particular file (just for diagnostic purposes!), then the application works fine without throwing an error. 现在,如果我再次向计算机发送RDP并授予所有人对该特定文件的完全访问权限(仅出于诊断目的!),则该应用程序可以正常运行而不会引发错误。 So this proves it really is an access issue. 因此,这证明这确实是访问问题。

The question is, these are simply simple xml files deployed with the project, and I don't really want to be having to set file permissions on each deployment. 问题是,这些只是与项目一起部署的简单xml文件,我真的不想在每个部署中都设置文件权限。 This would just be wrong. 这将是错误的。

So I am trying to understand why as part of the standard deploy would the cloud service not have access to read these deployed xml files, and then am interested in a proper solution. 因此,我试图了解为什么作为标准部署的一部分,云服务将无权读取这些部署的xml文件,然后对合适的解决方案感兴趣。 ie Either some permission settings maybe which get deployed with the solution, or maybe there is a better alternative approach to this problem. 即要么一些权限设置随解决方案一起部署,要么针对此问题有更好的替代方法。

In terms of the code I am using to read the xml file, I am simply using the XmlSerializer to deserialize the contents of the file back into a object. 就我用来读取xml文件的代码而言,我只是在使用XmlSerializer将文件的内容反序列化回一个对象。 (as below) (如下)

 public static T Deserialise(string settingsFile)
    {
        using (var fs = new FileStream(settingsFile, FileMode.Open))
        {
            var sr = new XmlSerializer(typeof (T));
            var obj = (T) sr.Deserialize(fs);
            fs.Close();
            return obj;
        }
    }

I understand that there is local storage for web roles, and this would be fine for me to use if I wanted to be able to read and write to storage as part of my application. 我知道Web角色有本地存储,如果我希望能够在应用程序中读取和写入存储,那对我来说很好。 But essentially these are configuration settings files which need to be deployed along with the application. 但实际上,这些是配置设置文件,需要与应用程序一起部署。

In the end the problem turned out to be the file access method. 最后,问题出在文件访问方法上。 I was initially using a Filestream to open the contents of the file, and even when running under elevated role permissions I was getting this accessed denied error. 最初,我使用Filestream打开文件的内容,即使在提升的角色权限下运行时,我也收到此访问被拒绝的错误。

However changing the code to read the file contents into a string first, I was able to avoid this error. 但是,更改代码以首先将文件内容读取为字符串,就可以避免此错误。

    public static T Deserialise(string settingsFile)
    {
        var fileContents = File.ReadAllText(settingsFile);
        using (var fs = new MemoryStream(Encoding.ASCII.GetBytes(fileContents)))
        {
            var sr = new XmlSerializer(typeof (T));
            var obj = (T) sr.Deserialize(fs);
            fs.Close();
            return obj;
        }
    }

not sure as to why the Filestream method needed these extra permissions, but the above solution works for me in this scenario. 不知道为什么Filestream方法需要这些额外的权限,但是在这种情况下,上述解决方案对我来说有效。

Project itself you can add folders and map that file path in your web.config instead of E:\\sitesroot\\0\\Templates\\Applications\\ControlProperties.xml . 项目本身,您可以添加文件夹并在web.config中而不是E:\\sitesroot\\0\\Templates\\Applications\\ControlProperties.xml映射该文件路径。 So that Access permissions problems will be automatically avoided when ever your hosting in to Cloud Service. 这样,当您托管到Cloud Service时,将自动避免访问权限问题。

I have done like below, 我做了如下

In web.config: 在web.config中:

<configuration>
        <appSettings>
             <add key="DocsPath" value="http://somesitename.cloudapp.net/Files/"/>
        </appSettings>
  <connectionStrings>

In Code Behind: 在后面的代码中:

string Location = ConfigurationManager.AppSettings["DocsPath"] + "ChildFolderName" + "\\" + Filename.XML;

In Cloud URL this will not give any access permission errors. 在Cloud URL中,这不会给出任何访问权限错误。 In this way of web.config you can do debugging also. 通过web.config的这种方式,您还可以进行调试。 Just you need to point the path like above i mentioned in web.config . 只是您需要指出我在web.config提到的上述路径。 change the connection string and other stuffs too in web.config. 在web.config中也更改连接字符串和其他内容。

The difference is in the file access method. 区别在于文件访问方法。 It turns out that you cannot open files inside of an Azure Web/Worker role for writing. 事实证明,您无法在Azure Web / Worker角色中打开文件进行写入。 Even though you just read from the files File.Open opens them with read/write access. 即使您只是从文件File.Open中读取,也可以使用读/写访问权限打开它们。 So this should work too: 所以这也应该工作:

using (var stream = File.Open(settingsFile, FileMode.Open, FileAccess.Read))
{
...
}

I guess the whole reason behind this is that in case of certain problems Azure automatically re-deploys your role using the originally uploaded package. 我想这背后的全部原因是,如果遇到某些问题,Azure会使用原始上载的包自动重新部署您的角色。 In this case any modifications previously made to the files would be lost and replaced with the original files in the package. 在这种情况下,以前对文件所做的任何修改都将丢失,并用包中的原始文件替换。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 &#39;访问被拒绝&#39;到Windows应用程序中的xml文件 - 'Access denied' to an xml file in windows application Windows服务中拒绝访问路径 - Access to the Path is denied in windows service Windows服务创建文件时拒绝访问路径 - Access to the path is Denied When create file by windows service 如何下载Windows Azure云服务定义.csdef文件 - How to Download Windows Azure Cloud Service Definition .csdef File Azure云服务上的Umbraco 7.0.2 - 拒绝访问路径“E:\\ sitesroot \\ 1 \\ config \\ applications.config” - Umbraco 7.0.2 on Azure Cloud Service - Access to the path 'E:\sitesroot\1\config\applications.config' is denied 拒绝访问XML文件的路径 - Access to path of XML file is denied 使用Windows Azure从Cloud Service访问服务管理REST API - Access Service Management REST API from Cloud Service using Windows Azure 在Windows Azure上运行WCF服务时获取“拒绝访问”异常消息 - Getting 'Access is Denied' exception message when running WCF service on Windows Azure 如何在 azure 上部署的云服务上引用 x.509 证书? - How to reference x.509 certificates on a cloud service deployed on azure? Windows Service托管的WCF服务在尝试读取文件时获得“访问被拒绝” - Windows Service Hosted WCF Service Getting “Access Denied” When Trying To Read a File
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM