473,383 Members | 1,815 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

WebConfigurationManager.GetSection in Medium Trust

Is it possible for a medium trust web application to read the Membership
providers? The following throws a SecurityException:

MembershipSection membershipSection =
(MembershipSection)WebConfigurationManager.GetSect ion("system.web/membership");

I have custom sections in my web.config where I specify
requirePermission="false", which allows them to be read in medium trust, but
it does not seem possible to do this for the membership section. When I
attempt it, like putting the following at the top of web.config...

<section name="membership" type="System.Web.Configuration.MembershipSection,
System.Web" allowDefinition="MachineToApplication" requirePermission="false"
/>

....I get the error "There is a duplicate 'system.web/membership' section
defined". I understand it may be possible to get around this by editing the
machine-level web.config so that allowOverride="true", but this is not an
option in my case, as I am developing a web application for wide
distribution, and I do not want to require my users to edit the machine-level
web.config.

Thanks,
Roger Martin
Aug 14 '08 #1
4 4144
Hi Roger,

Sorry for the late response. From your description you have to use medium
trust for your web application. You also want to get some information
stored in the Web.config file (Membership provider information in your
case). If my understanding is wrong please correct me.

Unfortunately, calling WebConfigurationManager.GetSection() method directly
is not allowed in medium trust. To work it around I would like to suggest
following alternatives:

1. Store data in <appSettingsin the Web.config file:
<appSettings>
<add key="setting1" value="value1"/>
</appSettings>
Then get it in your code:
string s = ConfigurationManager.AppSettings["setting1"];

2. If Web.config file is not encrypted we can read this file directly, then
use Linq to xml or XmlReader class to parse the xml. Since the Web.config
locates in the application directory it will not violate the security
restrictions listed here:
http://msdn.microsoft.com/en-us/libr...93(VS.80).aspx

Please have a try and let me know if you need further assistance.

Regards,
Allen Chen
Microsoft Online Support
Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
ms****@microsoft.com.

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscripti...t/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.

--------------------
| Thread-Topic: WebConfigurationManager.GetSection in Medium Trust
| thread-index: Acj+HJza13XqL8JqQ/e0CUdnRNmlwA==
| X-WBNR-Posting-Host: 207.46.19.168
| From: =?Utf-8?B?Um9nZXIgTWFydGlu?= <Roger Ma****@community.nospam>
| Subject: WebConfigurationManager.GetSection in Medium Trust
| Date: Thu, 14 Aug 2008 07:47:02 -0700
| Lines: 24
| Message-ID: <0E**********************************@microsoft.co m>
| MIME-Version: 1.0
| Content-Type: text/plain;
| charset="Utf-8"
| Content-Transfer-Encoding: 7bit
| X-Newsreader: Microsoft CDO for Windows 2000
| Content-Class: urn:content-classes:message
| Importance: normal
| Priority: normal
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.3790.3119
| Newsgroups: microsoft.public.dotnet.framework.aspnet
| Path: TK2MSFTNGHUB02.phx.gbl
| Xref: TK2MSFTNGHUB02.phx.gbl
microsoft.public.dotnet.framework.aspnet:73875
| NNTP-Posting-Host: tk2msftibfm01.phx.gbl 10.40.244.149
| X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet
|
| Is it possible for a medium trust web application to read the Membership
| providers? The following throws a SecurityException:
|
| MembershipSection membershipSection =
(MembershipSection)WebConfigurationManager.GetSect ion("system.web/membership
");
|
| I have custom sections in my web.config where I specify
| requirePermission="false", which allows them to be read in medium trust,
but
| it does not seem possible to do this for the membership section. When I
| attempt it, like putting the following at the top of web.config...
|
| <section name="membership"
type="System.Web.Configuration.MembershipSection,
| System.Web" allowDefinition="MachineToApplication"
requirePermission="false"
| />
|
| ...I get the error "There is a duplicate 'system.web/membership' section
| defined". I understand it may be possible to get around this by editing
the
| machine-level web.config so that allowOverride="true", but this is not an
| option in my case, as I am developing a web application for wide
| distribution, and I do not want to require my users to edit the
machine-level
| web.config.
|
| Thanks,
| Roger Martin
|

Aug 20 '08 #2
Perhaps you are not aware of this, but it *is* possible to call
WebConfigurationManager.GetSection() method in medium trust.

I already do this for a new section named galleryServerPro I added to
web.config. I defined the section like this:

<configSections>
<sectionGroup name="system.web">
<section name="galleryServerPro"
type="GalleryServerPro.Configuration.GalleryServer ProConfigSettings,
GalleryServerPro.Configuration" allowDefinition="MachineToApplication"
restartOnExternalChanges="true" requirePermission="false" />
</sectionGroup>
</configSections>

Then I can access it like this:

GalleryServerProConfigSettings _alleryServerProConfigSection =
(GalleryServerProConfigSettings)System.Web.Configu ration.WebConfigurationManager.GetSection("system. web/galleryServerPro");

This works in Medium Trust. All I want to do is access the Membership
section as well.

I suspect the real answer is that, despite being able to access custom
sections, one cannot access pre-defined sections such as Membership. Can you
confirm?

Being able to access the AppSettings section does not help me access the
Membership section, so that is not a viable solution, unless of course if I
duplicate my settings in both the Membership and AppSettings section, but
that violates best practices.

It appears the only answer is to use the XmlReader class (Linq is not
available in my .NET 2.0 app). If you have any sample code that shows how to
access the Membership section this way, I would really appreciate it,
otherwise I can probably muddle through it on my own.

Thanks,
Roger

Aug 20 '08 #3
Hi Roger,
==================================================
'¡*. despite being able to access custom
sections, one cannot access pre-defined sections such as Membership. Can
you
confirm?'
==================================================
Sorry that my statement is not very clear. In medium trust, by default,
it's only allowed to access the section defined in the Web.config file that
is in the application directory. It's not allowed to read the configuration
settings in the machine.config file. Since the membership section is
defined in the machine.config it violates the security policy when calling
WebConfigurationManager.GetSection("system.web/membership").

==================================================
'¡*.the only answer is to use the XmlReader class (Linq is not
available in my .NET 2.0 app). If you have any sample code that shows how
to
access the Membership section this way, I would really appreciate it'
==================================================
To use XmlReader to read the Web.config file you can try this:
string xml = "";

using (FileStream fs = new
FileStream(Server.MapPath("Web.config"), FileMode.Open, FileAccess.Read,
FileShare.Read)) {
using (StreamReader sr = new StreamReader(fs)) {
XmlReader r = XmlReader.Create(sr);
while (r.Read()) {
if (r.Name == "membership")
{
xml= r.ReadInnerXml();
break;
}
}
}
}
Response.Write(HttpUtility.HtmlEncode( xml));

Please have a try and feel free to let me know if you need further
assistance.

Regards,
Allen Chen
Microsoft Online Support

--------------------
| Thread-Topic: WebConfigurationManager.GetSection in Medium Trust
| thread-index: AckC1QDYNcXdV3e4RtijJ2mqnWrX/A==
| X-WBNR-Posting-Host: 207.46.19.168
| From: =?Utf-8?B?Um9nZXIgTWFydGlu?= <Roger Ma****@community.nospam>
| References: <0E**********************************@microsoft.co m>
<AG**************@TK2MSFTNGHUB02.phx.gbl>
| Subject: RE: WebConfigurationManager.GetSection in Medium Trust
| Date: Wed, 20 Aug 2008 07:57:02 -0700
| Lines: 40
| Message-ID: <18**********************************@microsoft.co m>
| MIME-Version: 1.0
| Content-Type: text/plain;
| charset="Utf-8"
| Content-Transfer-Encoding: 7bit
| X-Newsreader: Microsoft CDO for Windows 2000
| Content-Class: urn:content-classes:message
| Importance: normal
| Priority: normal
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.3790.3119
| Newsgroups: microsoft.public.dotnet.framework.aspnet
| Path: TK2MSFTNGHUB02.phx.gbl
| Xref: TK2MSFTNGHUB02.phx.gbl
microsoft.public.dotnet.framework.aspnet:74316
| NNTP-Posting-Host: tk2msftibfm01.phx.gbl 10.40.244.149
| X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet
|
| Perhaps you are not aware of this, but it *is* possible to call
| WebConfigurationManager.GetSection() method in medium trust.
|
| I already do this for a new section named galleryServerPro I added to
| web.config. I defined the section like this:
|
| <configSections>
| <sectionGroup name="system.web">
| <section name="galleryServerPro"
| type="GalleryServerPro.Configuration.GalleryServer ProConfigSettings,
| GalleryServerPro.Configuration" allowDefinition="MachineToApplication"
| restartOnExternalChanges="true" requirePermission="false" />
| </sectionGroup>
| </configSections>
|
| Then I can access it like this:
|
| GalleryServerProConfigSettings _alleryServerProConfigSection =
|
(GalleryServerProConfigSettings)System.Web.Configu ration.WebConfigurationMan
ager.GetSection("system.web/galleryServerPro");
|
| This works in Medium Trust. All I want to do is access the Membership
| section as well.
|
| I suspect the real answer is that, despite being able to access custom
| sections, one cannot access pre-defined sections such as Membership. Can
you
| confirm?
|
| Being able to access the AppSettings section does not help me access the
| Membership section, so that is not a viable solution, unless of course if
I
| duplicate my settings in both the Membership and AppSettings section, but
| that violates best practices.
|
| It appears the only answer is to use the XmlReader class (Linq is not
| available in my .NET 2.0 app). If you have any sample code that shows how
to
| access the Membership section this way, I would really appreciate it,
| otherwise I can probably muddle through it on my own.
|
| Thanks,
| Roger
|
|

Aug 21 '08 #4
Thanks for confirming that. And thanks also for the XmlReader sample code.
That is exactly what I needed, and I am back in the ballgame now.

Cheers,
Roger Martin

"Allen Chen [MSFT]" wrote:
To use XmlReader to read the Web.config file you can try this:
string xml = "";

using (FileStream fs = new
FileStream(Server.MapPath("Web.config"), FileMode.Open, FileAccess.Read,
FileShare.Read)) {
using (StreamReader sr = new StreamReader(fs)) {
XmlReader r = XmlReader.Create(sr);
while (r.Read()) {
if (r.Name == "membership")
{
xml= r.ReadInnerXml();
break;
}
}
}
}
Response.Write(HttpUtility.HtmlEncode( xml));
Aug 22 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Michael Howes | last post by:
I have a webservice and use WSE so a user can upload a good sized file. The entire ASP.Net projects, web.config sets a Medium trust level IIS is set up to use "Integrated Windows authentication"...
0
by: AmitKu | last post by:
I am trying to do a URL post using HttpWebRequest, but it fails because I am hosting on Network Solutions, and their servers are all medium trust. Apparently HttpWebRequest doesn't work on medium...
0
by: AmitKu | last post by:
I am trying to do a URL post using HttpWebRequest, but it fails because I am hosting on Network Solutions, and their servers are all medium trust. Apparently HttpWebRequest doesn't work on medium...
7
by: AmitKu | last post by:
I am trying to do a URL post using HttpWebRequest, but it fails because I am hosting on Network Solutions, and their servers are all medium trust. Apparently HttpWebRequest doesn't work on medium...
1
by: Jordan M. | last post by:
Hi, Hopefully someone can help or at least point me in the right direction... I have developed a site that uses the CyberSource system to process credit cards. All works great locally on my...
3
by: Mukesh | last post by:
Hi all I have Created an web application using VS 2005, asp.net2.0, Ajax Extensions 1.0, Ent Lib 3.1 , MS sql Server 2005, ajax Control tool kit Version 1.0.10618.0
0
by: ilkka | last post by:
Hi ! SimpleWorkerRequest class can be used to host asp.net pages outside of IIS. However when a web application is configured to run as medium trust (or lower), you will get an exception...
3
by: =?Utf-8?B?Um9nZXIgTWFydGlu?= | last post by:
Is it possible for a medium trust web application to read the Membership providers? The following throws a SecurityException: MembershipSection membershipSection =...
5
by: Michael Howes | last post by:
I'm upgrading a VS 2003/.Net 1.1 ASP.Net application to VS 2008/.Net 3.0 The application uses an older version of the Microsoft Data Blocks for database access. The version in the...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.