2010-10-13 3 views
2

Après avoir déménagé mon site web à partir de mon environnement de développement local à un hôte partagé je reçois:ASP.NET exception de sécurité avec OpenWebConfiguration sur l'hôte partagé

Security Exception 

Description: The application attempted to perform an operation not allowed by 
the security policy. To grant this application the required permission please 
contact your system administrator or change the application's trust level in 
the configuration file. 

Le problème se produit dans mon application web partout ce qui suit est appelé:

WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath) 

Depuis mon application Web ne cherche à ouvrir propre fichier web.config de, je ne sais pas pourquoi cela est signalé comme une exception de sécurité. Peut-être que quelqu'un peut expliquer ... Mais plus important encore, j'ai besoin d'une solution, les solutions de couple que j'ai trouvé via Google sont douloureuses.

Une solution (à partir de nombreux messages) dit de configurer le niveau de confiance à Full, mais on me dit que ce n'est pas possible sur mon hôte partagé.

Une autre solution (de http://www.4guysfromrolla.com/articles/100307-1.aspx) dit de ne pas utiliser OpenWebConfiguration(), mais je dois l'utiliser pour chiffrer les sections de configuration (par exemple connectionStrings) en utilisant DPAPI (pour plus d'informations voir http://www.4guysfromrolla.com/articles/021506-1.aspx).

Veuillez expliquer pourquoi IIS barfs sur mon application Web essayant d'ouvrir son propre web.config, et une solution de contournement pour être en mesure de chiffrer des parties de web.config en utilisant DPAPI.

+0

Je viens de trouver un ancien dépassement de pile affichant here qui indique que DPAPI requiert une confiance totale, ce qui est logique pour l'utilisation d'une clé au niveau de la machine. Cependant, j'aimerais toujours pouvoir utiliser OpenWebConfiguration() sur mon propre web.config (par Request.ApplicationPath), pour permettre un accès facile à la section appSettings. La confiance totale est-elle vraiment nécessaire pour cela? – harrije

Répondre

0

J'ai eu l'expérience de ce problème dans le passé. La méthode OpenWebConfiguration() lit également le fichier machine.config. Sous approbation partielle et sans les autorisations correctes, vous ne pouvez pas utiliser cette méthode.

Si vous deviez entrer dans les assemblys .NET Framework avec votre débogueur dans Visual Studio 2008/2010, vous pouvez voir exactement ce qui se passe.

Ce qui suit est une pile d'appel capturé lors d'entrer dans WebConfigurationManager.OpenWebConfiguration():

 
mscorlib.dll!System.IO.FileStream.Init(string path = "C:\\Windows\\Microsoft.NET\\Framework\\v2.0.50727\\Config\\machine.config", System.IO.FileMode mode = Open, System.IO.FileAccess access = Read, int rights = 0, bool useRights = false, System.IO.FileShare share = Read, int bufferSize = 4096, System.IO.FileOptions options = None, Microsoft.Win32.Win32Native.SECURITY_ATTRIBUTES secAttrs = null, string msgPath = "machine.config", bool bFromProxy = false) Line 326 C# 

mscorlib.dll!System.IO.FileStream.FileStream(string path, System.IO.FileMode mode, System.IO.FileAccess access, System.IO.FileShare share) Line 259 C# 

System.Configuration.dll!System.Configuration.Internal.InternalConfigHost.StaticOpenStreamForRead(string streamName) + 0x56 bytes 

System.Configuration.dll!System.Configuration.Internal.InternalConfigHost.System.Configuration.Internal.IInternalConfigHost.OpenStreamForRead(string streamName, bool assertPermissions) + 0x7d bytes 

System.Configuration.dll!System.Configuration.Internal.InternalConfigHost.System.Configuration.Internal.IInternalConfigHost.OpenStreamForRead(string streamName) + 0xb bytes 

System.Configuration.dll!System.Configuration.Internal.DelegatingConfigHost.OpenStreamForRead(string streamName) + 0xe bytes 

System.Configuration.dll!System.Configuration.UpdateConfigHost.OpenStreamForRead(string streamName) + 0x2f bytes 

System.Configuration.dll!System.Configuration.BaseConfigurationRecord.InitConfigFromFile() + 0x126 bytes 

System.Configuration.dll!System.Configuration.BaseConfigurationRecord.Init(System.Configuration.Internal.IInternalConfigRoot configRoot, System.Configuration.BaseConfigurationRecord parent, string configPath, string locationSubPath) + 0xaa5 bytes 

System.Configuration.dll!System.Configuration.MgmtConfigurationRecord.Init(System.Configuration.Internal.IInternalConfigRoot configRoot, System.Configuration.Internal.IInternalConfigRecord parent, string configPath, string locationSubPath) + 0x39 bytes 

System.Configuration.dll!System.Configuration.MgmtConfigurationRecord.Create(System.Configuration.Internal.IInternalConfigRoot configRoot, System.Configuration.Internal.IInternalConfigRecord parent, string configPath, string locationSubPath) + 0x2a bytes 

System.Configuration.dll!System.Configuration.Internal.InternalConfigRoot.GetConfigRecord(string configPath) + 0x12d bytes 

System.Configuration.dll!System.Configuration.Configuration.Configuration(string locationSubPath, System.Type typeConfigHost, object[] hostInitConfigurationParams) + 0xfd bytes 

System.Configuration.dll!System.Configuration.Internal.InternalConfigConfigurationFactory.System.Configuration.Internal.IInternalConfigConfigurationFactory.Create(System.Type typeConfigHost, object[] hostInitConfigurationParams) + 0x1e bytes 

System.Web.dll!System.Web.Configuration.WebConfigurationHost.OpenConfiguration(System.Web.Configuration.WebLevel webLevel, System.Configuration.ConfigurationFileMap fileMap, System.Web.VirtualPath path, string site, string locationSubPath, string server, string userName, string password, System.IntPtr tokenHandle) Line 862 C# 

System.Web.dll!System.Web.Configuration.WebConfigurationManager.OpenWebConfigurationImpl(System.Web.Configuration.WebLevel webLevel, System.Configuration.ConfigurationFileMap fileMap, string path, string site, string locationSubPath, string server, string userName, string password, System.IntPtr userToken) Line 77 + 0x1c bytes C# 

System.Web.dll!System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration(string path) Line 140 + 0x25 bytes C# 

Malheureusement, votre seule alternative est d'utiliser WebConfigurationManager.GetSection() qui n'est pas aussi riche en fonctionnalités. En ce qui concerne le chiffrement de vos chaînes de connexion.

Malheureusement, cette fonctionnalité exige la confiance totale, il n'y a pas d'autre moyen de contourner cela.

+0

J'ai également pu utiliser ConfigurationManager.AppSettings(). Get ("keyname"), afin que je puisse obtenir les paramètres de l'application avec un minimum de travail. – harrije

Questions connexes