473,701 Members | 2,718 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

System.Security .SecurityExcept ion: Request


I am trying to understand the following error:
Any thing you can tell me about this is appreciated.

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.

Exception Details: for the permission of type
'System.Securit y.Permissions.E nvironmentPermi ssion, mscorlib,
Version=2.0.0.0 , Culture=neutral , PublicKeyToken= b77a5c561934e08 9' failed.

Source Error:
Line 45:
Line 46: //create the smtp client
Line 47: SmtpClient _smtp = new SmtpClient("mre lay.perfora.net ");
Line 48: _smtp.DeliveryM ethod = SmtpDeliveryMet hod.Network;
Line 49: try

Source File: e:\kunden\homep ages\34\d252335 749\comments.as px.cs Line: 47

Stack Trace:
[SecurityExcepti on: Request for the permission of type
'System.Securit y.Permissions.E nvironmentPermi ssion, mscorlib,
Version=2.0.0.0 , Culture=neutral , PublicKeyToken= b77a5c561934e08 9' failed.]
System.Security .CodeAccessSecu rityEngine.Chec k(Object demand,
StackCrawlMark& stackMark, Boolean isPermSet) +0
System.Security .CodeAccessPerm ission.Demand() +59
System.Net.Cred entialCache.get _DefaultCredent ials() +59
System.Net.Conf iguration.SmtpN etworkElementIn ternal..ctor(Sm tpNetworkElemen t
element) +55
System.Net.Conf iguration.SmtpS ectionInternal. .ctor(SmtpSecti on section)
+65
System.Net.Conf iguration.SmtpS ectionInternal. GetSection() +173
System.Net.Mail .SmtpClient.get _MailConfigurat ion() +45
System.Net.Mail .SmtpClient.Ini tialize() +223
System.Net.Mail .SmtpClient..ct or(String host) +162
comments.contac tUS_Click(Objec t sender, EventArgs e) in
e:\kunden\homep ages\34\d252335 749\comments.as px.cs:47
System.Web.UI.W ebControls.Butt on.OnClick(Even tArgs e) +105
System.Web.UI.W ebControls.Butt on.RaisePostBac kEvent(String eventArgument)
+107
System.Web.UI.W ebControls.Butt on.System.Web.U I.IPostBackEven tHandler.RaiseP ostBackEvent(St ring
eventArgument) +7
System.Web.UI.P age.RaisePostBa ckEvent(IPostBa ckEventHandler
sourceControl, String eventArgument) +11
System.Web.UI.P age.RaisePostBa ckEvent(NameVal ueCollection postData) +33
System.Web.UI.P age.ProcessRequ estMain(Boolean
includeStagesBe foreAsyncPoint, Boolean includeStagesAf terAsyncPoint) +7350
System.Web.UI.P age.ProcessRequ est(Boolean includeStagesBe foreAsyncPoint,
Boolean includeStagesAf terAsyncPoint) +213
System.Web.UI.P age.ProcessRequ est() +86
System.Web.UI.P age.ProcessRequ estWithNoAssert (HttpContext context) +18
System.Web.UI.P age.ProcessRequ est(HttpContext context) +49
ASP.comments_as px.ProcessReque st(HttpContext context) in
App_Web_xytww0y g.0.cs:0
System.Web.Call HandlerExecutio nStep.System.We b.HttpApplicati on.IExecutionSt ep.Execute()
+358
System.Web.Http Application.Exe cuteStep(IExecu tionStep step, Boolean&
completedSynchr onously) +64
When I did a Google search I came up with the following url and explanation:

http://support.microsoft.com/kb/814741

CAUSE
The System.Security .Permissions.En vironmentPermis sion class in Mscorlib.dll
controls the access to the user environment variables.
SystemInformati on.UserName in the application requests the permission from
the EnvironmentPerm ission class to access the UserName environment variable.
However, in the Local Intranet zone, System.Windows. Forms does not have
permissions to access the Windows user name, and the request is not served
by Mscorlib.dll. Therefore, a security exception is raised when you run the
application.

The problem with this is that the code they give you to test with is linked
to Windows Forms;

//display the UserName currently logged
Console.WriteLi ne(System.Envir onment.UserName );
Console.ReadLin e();

Even if I set this to Response.Write( ... ); System.Environm ent.UserName is
not available in a remote web environment.

The following is code from my web.config file. I have disguised my userID
and Password.

<system.net>
<mailSettings >
<smtp deliveryMethod= "Network">
<!-- The <networknode supports the following properties, but we won't
use all of them
<network host="127.0.0.1 " port="25" userName="myUse rName"
password="OpenS esame" defaultCredenti als="true" />
-->

<network host="mrelay.pe rfora.net" userName="in**@ mydomain.org"
password="mypwd " defaultCredenti als="true"/>
</smtp>
</mailSettings>
</system.net>

The following is the click event function that triggers the error code; it
is linked to a submit button on the form:

protected void contactUS_Click (object sender, EventArgs e)
{
//Things to Do:
// Validate form fields:
// Name field should be letters and spaces only. Can't be blank or
only spaces.
// Phone field may have () - spaces and digits
// email address must be properly formatted.
// Eventually I would like to add a check for domain validity
// After edits I need to create the message
//String from_addr = new String(FindCont rol("email").To String());
MailAddress _from = new MailAddress("in **@mydomain.org ");
MailAddress _sender = new MailAddress("in **@mydomain.org ");
MailAddress _to = new MailAddress("we *******@mydomai n.org");
MailMessage msg = new MailMessage(_fr om,_to);
msg.Sender = _sender;
msg.Subject = "Feedback from Comments Form";
StringBuilder sbuilder = new StringBuilder() ;
//Build string for message body
sbuilder.Append Line("Senders IP Address: " +
Request.ServerV ariables["remote_add r"].ToString());
sbuilder.Append Line("Name: " +
comments_form.C ontrols[0].ToString());
sbuilder.Append Line("Email: " +
comments_form.C ontrols[1].ToString());
sbuilder.Append Line("Phone: " +
comments_form.C ontrols[2].ToString());
sbuilder.Append Line("Message: " +
comments_form.C ontrols[3].ToString());
//assign string value to message body
msg.Body = sbuilder.ToStri ng();

//create the smtp client

SmtpClient _smtp = new SmtpClient("mre lay.perfora.net ");
_smtp.DeliveryM ethod = SmtpDeliveryMet hod.Network;

try
{
_smtp.Send(msg) ;

}
catch (ArgumentNullEx ception )
{
Response.Write( "Argument Null Exception");
}
catch (ArgumentOutOfR angeException )
{
Response.Write( "Argument out of Range Exception");
}
catch (SmtpException )
{
Response.Write( "SMTP Exception");
}
catch
{
Response.Write( "Something else happened");
}

}

The following is the web page form that I am dealing with:

<asp:panel id="panelSendEm ail" runat="server">
<form id="comments_fo rm" action="comment s.aspx" runat="server"
onsubmit="retur n jcap();" >

<p>If you would like to make a comment about the anything
that you see on our web site
or ask a question, then please feel free to use this
comments form.</p>

<b>Your Name :</b><br/>
<input id="name" type="text" size="30" maxlength="256"
name="Senders_N ame"/>
<p></p>

<b>Your E-Mail Address :</b><br/>
<input id="email" type="text" size="30" maxlength="80"
name="_reply"
onblur="return isValidEmail(em ail, true)"/>

<p></p>
<b>Your Phone Number:</b><br/>
<input id="phone"type= "text" size="15" maxlength="15"
name="Phone"/>
<p></p>

<b>Your Comments :</b><br/>
<textarea id="message" name="Senders_C omments" rows="6"
cols="60"></textarea>
<p></p>
<p>Please enter the word that you see in the image </p>
<p><script type="text/javascript">sjc ap();</script></p>
<noscript><p>[This resource requires a Javascript enabled
browser.]</p></noscript>
<asp:button runat="server" id="contact_us " Text="Submit"
OnClick="contac tUS_Click" />
<input type="reset" value="Reset"/>
</form>
</asp:panel>

<asp:panel id="panelMailSe nt" runat="server" Visible="False" >
Thank you for your comments.
</asp:panel>

Aug 16 '08 #1
5 8324
The worker process running your application does not have permission to read
the environment variable. I think you know this part. What I don't
understand is what it is happening A couple of questions. Is this a web app
or windows app? You can fix this easy with an appropriate CAS policy but I'd
recommend you figure out what is going wrong first.

--

Regards,
Alvin Bruney [MVP ASP.NET]

[Shameless Author plug]
Download OWC Black Book, 2nd Edition
Exclusively on www.lulu.com/owc $15.00
Need a free copy of VSTS 2008 w/ MSDN Premium?
http://msmvps.com/blogs/alvin/Default.aspx
-------------------------------------------------------
"Henry Stock" <he***@henry-stock.comwrote in message
news:#l******** ******@TK2MSFTN GP02.phx.gbl...
>
I am trying to understand the following error:
Any thing you can tell me about this is appreciated.

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.

Exception Details: for the permission of type
'System.Securit y.Permissions.E nvironmentPermi ssion, mscorlib,
Version=2.0.0.0 , Culture=neutral , PublicKeyToken= b77a5c561934e08 9' failed.

Source Error:
Line 45:
Line 46: //create the smtp client
Line 47: SmtpClient _smtp = new SmtpClient("mre lay.perfora.net ");
Line 48: _smtp.DeliveryM ethod = SmtpDeliveryMet hod.Network;
Line 49: try

Source File: e:\kunden\homep ages\34\d252335 749\comments.as px.cs Line:
47

Stack Trace:
[SecurityExcepti on: Request for the permission of type
'System.Securit y.Permissions.E nvironmentPermi ssion, mscorlib,
Version=2.0.0.0 , Culture=neutral , PublicKeyToken= b77a5c561934e08 9'
failed.]
System.Security .CodeAccessSecu rityEngine.Chec k(Object demand,
StackCrawlMark& stackMark, Boolean isPermSet) +0
System.Security .CodeAccessPerm ission.Demand() +59
System.Net.Cred entialCache.get _DefaultCredent ials() +59

System.Net.Conf iguration.SmtpN etworkElementIn ternal..ctor(Sm tpNetworkElemen t
element) +55
System.Net.Conf iguration.SmtpS ectionInternal. .ctor(SmtpSecti on section)
+65
System.Net.Conf iguration.SmtpS ectionInternal. GetSection() +173
System.Net.Mail .SmtpClient.get _MailConfigurat ion() +45
System.Net.Mail .SmtpClient.Ini tialize() +223
System.Net.Mail .SmtpClient..ct or(String host) +162
comments.contac tUS_Click(Objec t sender, EventArgs e) in
e:\kunden\homep ages\34\d252335 749\comments.as px.cs:47
System.Web.UI.W ebControls.Butt on.OnClick(Even tArgs e) +105
System.Web.UI.W ebControls.Butt on.RaisePostBac kEvent(String
eventArgument) +107

System.Web.UI.W ebControls.Butt on.System.Web.U I.IPostBackEven tHandler.RaiseP ostBackEvent(St ring
eventArgument) +7
System.Web.UI.P age.RaisePostBa ckEvent(IPostBa ckEventHandler
sourceControl, String eventArgument) +11
System.Web.UI.P age.RaisePostBa ckEvent(NameVal ueCollection postData) +33
System.Web.UI.P age.ProcessRequ estMain(Boolean
includeStagesBe foreAsyncPoint, Boolean includeStagesAf terAsyncPoint) +7350
System.Web.UI.P age.ProcessRequ est(Boolean includeStagesBe foreAsyncPoint,
Boolean includeStagesAf terAsyncPoint) +213
System.Web.UI.P age.ProcessRequ est() +86
System.Web.UI.P age.ProcessRequ estWithNoAssert (HttpContext context) +18
System.Web.UI.P age.ProcessRequ est(HttpContext context) +49
ASP.comments_as px.ProcessReque st(HttpContext context) in
App_Web_xytww0y g.0.cs:0

System.Web.Call HandlerExecutio nStep.System.We b.HttpApplicati on.IExecutionSt ep.Execute()
+358
System.Web.Http Application.Exe cuteStep(IExecu tionStep step, Boolean&
completedSynchr onously) +64
When I did a Google search I came up with the following url and
explanation:

http://support.microsoft.com/kb/814741

CAUSE
The System.Security .Permissions.En vironmentPermis sion class in
Mscorlib.dll controls the access to the user environment variables.
SystemInformati on.UserName in the application requests the permission from
the EnvironmentPerm ission class to access the UserName environment
variable. However, in the Local Intranet zone, System.Windows. Forms does
not have permissions to access the Windows user name, and the request is
not served by Mscorlib.dll. Therefore, a security exception is raised when
you run the application.

The problem with this is that the code they give you to test with is
linked to Windows Forms;

//display the UserName currently logged
Console.WriteLi ne(System.Envir onment.UserName );
Console.ReadLin e();

Even if I set this to Response.Write( ... ); System.Environm ent.UserName
is not available in a remote web environment.

The following is code from my web.config file. I have disguised my userID
and Password.

<system.net>
<mailSettings >
<smtp deliveryMethod= "Network">
<!-- The <networknode supports the following properties, but we won't
use all of them
<network host="127.0.0.1 " port="25" userName="myUse rName"
password="OpenS esame" defaultCredenti als="true" />
-->

<network host="mrelay.pe rfora.net" userName="in**@ mydomain.org"
password="mypwd " defaultCredenti als="true"/>
</smtp>
</mailSettings>
</system.net>

The following is the click event function that triggers the error code; it
is linked to a submit button on the form:

protected void contactUS_Click (object sender, EventArgs e)
{
//Things to Do:
// Validate form fields:
// Name field should be letters and spaces only. Can't be blank or
only spaces.
// Phone field may have () - spaces and digits
// email address must be properly formatted.
// Eventually I would like to add a check for domain validity
// After edits I need to create the message
//String from_addr = new String(FindCont rol("email").To String());
MailAddress _from = new MailAddress("in **@mydomain.org ");
MailAddress _sender = new MailAddress("in **@mydomain.org ");
MailAddress _to = new MailAddress("we *******@mydomai n.org");
MailMessage msg = new MailMessage(_fr om,_to);
msg.Sender = _sender;
msg.Subject = "Feedback from Comments Form";
StringBuilder sbuilder = new StringBuilder() ;
//Build string for message body
sbuilder.Append Line("Senders IP Address: " +
Request.ServerV ariables["remote_add r"].ToString());
sbuilder.Append Line("Name: " +
comments_form.C ontrols[0].ToString());
sbuilder.Append Line("Email: " +
comments_form.C ontrols[1].ToString());
sbuilder.Append Line("Phone: " +
comments_form.C ontrols[2].ToString());
sbuilder.Append Line("Message: " +
comments_form.C ontrols[3].ToString());
//assign string value to message body
msg.Body = sbuilder.ToStri ng();

//create the smtp client

SmtpClient _smtp = new SmtpClient("mre lay.perfora.net ");
_smtp.DeliveryM ethod = SmtpDeliveryMet hod.Network;

try
{
_smtp.Send(msg) ;

}
catch (ArgumentNullEx ception )
{
Response.Write( "Argument Null Exception");
}
catch (ArgumentOutOfR angeException )
{
Response.Write( "Argument out of Range Exception");
}
catch (SmtpException )
{
Response.Write( "SMTP Exception");
}
catch
{
Response.Write( "Something else happened");
}

}

The following is the web page form that I am dealing with:

<asp:panel id="panelSendEm ail" runat="server">
<form id="comments_fo rm" action="comment s.aspx" runat="server"
onsubmit="retur n jcap();" >

<p>If you would like to make a comment about the anything
that you see on our web site
or ask a question, then please feel free to use this
comments form.</p>

<b>Your Name :</b><br/>
<input id="name" type="text" size="30" maxlength="256"
name="Senders_N ame"/>
<p></p>

<b>Your E-Mail Address :</b><br/>
<input id="email" type="text" size="30" maxlength="80"
name="_reply"
onblur="return isValidEmail(em ail, true)"/>

<p></p>
<b>Your Phone Number:</b><br/>
<input id="phone"type= "text" size="15" maxlength="15"
name="Phone"/>
<p></p>

<b>Your Comments :</b><br/>
<textarea id="message" name="Senders_C omments" rows="6"
cols="60"></textarea>
<p></p>
<p>Please enter the word that you see in the image </p>
<p><script type="text/javascript">sjc ap();</script></p>
<noscript><p>[This resource requires a Javascript enabled
browser.]</p></noscript>
<asp:button runat="server" id="contact_us " Text="Submit"
OnClick="contac tUS_Click" />
<input type="reset" value="Reset"/>
</form>
</asp:panel>

<asp:panel id="panelMailSe nt" runat="server" Visible="False" >
Thank you for your comments.
</asp:panel>
Aug 16 '08 #2
This is definitely a web application and this is what I am getting on their
server.

In my test evironment I am getting an SmtpException at the point of sending
the message, but this may be because my local server does not have
appropriate access to the remote mail forwarder. I know that the user ID
and password I am using in my web.config are valid. My ISP told me to use
the server specified in code. The one I use for my normal mail client is
"smtp.1and1.com ".

What is a CAS policy? Can I set this up in code or do I have to get my ISP
involved?

"Alvin Bruney [ASP.NET MVP]" <vapor dan using hot male spam filterwrote in
message news:B4******** *************** ***********@mic rosoft.com...
The worker process running your application does not have permission to
read the environment variable. I think you know this part. What I don't
understand is what it is happening A couple of questions. Is this a web
app or windows app? You can fix this easy with an appropriate CAS policy
but I'd recommend you figure out what is going wrong first.

--

Regards,
Alvin Bruney [MVP ASP.NET]

[Shameless Author plug]
Download OWC Black Book, 2nd Edition
Exclusively on www.lulu.com/owc $15.00
Need a free copy of VSTS 2008 w/ MSDN Premium?
http://msmvps.com/blogs/alvin/Default.aspx
-------------------------------------------------------
"Henry Stock" <he***@henry-stock.comwrote in message
news:#l******** ******@TK2MSFTN GP02.phx.gbl...
>>
I am trying to understand the following error:
Any thing you can tell me about this is appreciated.

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.

Exception Details: for the permission of type
'System.Securi ty.Permissions. EnvironmentPerm ission, mscorlib,
Version=2.0.0. 0, Culture=neutral , PublicKeyToken= b77a5c561934e08 9'
failed.

Source Error:
Line 45:
Line 46: //create the smtp client
Line 47: SmtpClient _smtp = new SmtpClient("mre lay.perfora.net ");
Line 48: _smtp.DeliveryM ethod = SmtpDeliveryMet hod.Network;
Line 49: try

Source File: e:\kunden\homep ages\34\d252335 749\comments.as px.cs Line:
47

Stack Trace:
[SecurityExcepti on: Request for the permission of type
'System.Securi ty.Permissions. EnvironmentPerm ission, mscorlib,
Version=2.0.0. 0, Culture=neutral , PublicKeyToken= b77a5c561934e08 9'
failed.]
System.Security .CodeAccessSecu rityEngine.Chec k(Object demand,
StackCrawlMark & stackMark, Boolean isPermSet) +0
System.Security .CodeAccessPerm ission.Demand() +59
System.Net.Cred entialCache.get _DefaultCredent ials() +59

System.Net.Con figuration.Smtp NetworkElementI nternal..ctor(S mtpNetworkEleme nt
element) +55
System.Net.Conf iguration.SmtpS ectionInternal. .ctor(SmtpSecti on section)
+65
System.Net.Conf iguration.SmtpS ectionInternal. GetSection() +173
System.Net.Mail .SmtpClient.get _MailConfigurat ion() +45
System.Net.Mail .SmtpClient.Ini tialize() +223
System.Net.Mail .SmtpClient..ct or(String host) +162
comments.contac tUS_Click(Objec t sender, EventArgs e) in
e:\kunden\home pages\34\d25233 5749\comments.a spx.cs:47
System.Web.UI.W ebControls.Butt on.OnClick(Even tArgs e) +105
System.Web.UI.W ebControls.Butt on.RaisePostBac kEvent(String
eventArgumen t) +107

System.Web.UI. WebControls.But ton.System.Web. UI.IPostBackEve ntHandler.Raise PostBackEvent(S tring
eventArgumen t) +7
System.Web.UI.P age.RaisePostBa ckEvent(IPostBa ckEventHandler
sourceContro l, String eventArgument) +11
System.Web.UI.P age.RaisePostBa ckEvent(NameVal ueCollection postData) +33
System.Web.UI.P age.ProcessRequ estMain(Boolean
includeStagesB eforeAsyncPoint , Boolean includeStagesAf terAsyncPoint)
+7350
System.Web.UI.P age.ProcessRequ est(Boolean
includeStagesB eforeAsyncPoint , Boolean includeStagesAf terAsyncPoint) +213
System.Web.UI.P age.ProcessRequ est() +86
System.Web.UI.P age.ProcessRequ estWithNoAssert (HttpContext context) +18
System.Web.UI.P age.ProcessRequ est(HttpContext context) +49
ASP.comments_as px.ProcessReque st(HttpContext context) in
App_Web_xytww0 yg.0.cs:0

System.Web.Cal lHandlerExecuti onStep.System.W eb.HttpApplicat ion.IExecutionS tep.Execute()
+358
System.Web.Http Application.Exe cuteStep(IExecu tionStep step, Boolean&
completedSynch ronously) +64
When I did a Google search I came up with the following url and
explanation:

http://support.microsoft.com/kb/814741

CAUSE
The System.Security .Permissions.En vironmentPermis sion class in
Mscorlib.dll controls the access to the user environment variables.
SystemInformat ion.UserName in the application requests the permission
from the EnvironmentPerm ission class to access the UserName environment
variable. However, in the Local Intranet zone, System.Windows. Forms does
not have permissions to access the Windows user name, and the request is
not served by Mscorlib.dll. Therefore, a security exception is raised
when you run the application.

The problem with this is that the code they give you to test with is
linked to Windows Forms;

//display the UserName currently logged
Console.WriteLi ne(System.Envir onment.UserName );
Console.ReadLin e();

Even if I set this to Response.Write( ... ); System.Environm ent.UserName
is not available in a remote web environment.

The following is code from my web.config file. I have disguised my
userID and Password.

<system.net>
<mailSetting s>
<smtp deliveryMethod= "Network">
<!-- The <networknode supports the following properties, but we won't
use all of them
<network host="127.0.0.1 " port="25" userName="myUse rName"
password="Open Sesame" defaultCredenti als="true" />
-->

<network host="mrelay.pe rfora.net" userName="in**@ mydomain.org"
password="mypw d" defaultCredenti als="true"/>
</smtp>
</mailSettings>
</system.net>

The following is the click event function that triggers the error code;
it is linked to a submit button on the form:

protected void contactUS_Click (object sender, EventArgs e)
{
//Things to Do:
// Validate form fields:
// Name field should be letters and spaces only. Can't be blank or
only spaces.
// Phone field may have () - spaces and digits
// email address must be properly formatted.
// Eventually I would like to add a check for domain validity
// After edits I need to create the message
//String from_addr = new String(FindCont rol("email").To String());
MailAddress _from = new MailAddress("in **@mydomain.org ");
MailAddress _sender = new MailAddress("in **@mydomain.org ");
MailAddress _to = new MailAddress("we *******@mydomai n.org");
MailMessage msg = new MailMessage(_fr om,_to);
msg.Sender = _sender;
msg.Subject = "Feedback from Comments Form";
StringBuilder sbuilder = new StringBuilder() ;
//Build string for message body
sbuilder.Append Line("Senders IP Address: " +
Request.Server Variables["remote_add r"].ToString());
sbuilder.Append Line("Name: " +
comments_form. Controls[0].ToString());
sbuilder.Append Line("Email: " +
comments_form. Controls[1].ToString());
sbuilder.Append Line("Phone: " +
comments_form. Controls[2].ToString());
sbuilder.Append Line("Message: " +
comments_form. Controls[3].ToString());
//assign string value to message body
msg.Body = sbuilder.ToStri ng();

//create the smtp client

SmtpClient _smtp = new SmtpClient("mre lay.perfora.net ");
_smtp.DeliveryM ethod = SmtpDeliveryMet hod.Network;

try
{
_smtp.Send(msg) ;

}
catch (ArgumentNullEx ception )
{
Response.Write( "Argument Null Exception");
}
catch (ArgumentOutOfR angeException )
{
Response.Write( "Argument out of Range Exception");
}
catch (SmtpException )
{
Response.Write( "SMTP Exception");
}
catch
{
Response.Write( "Something else happened");
}

}

The following is the web page form that I am dealing with:

<asp:panel id="panelSendEm ail" runat="server">
<form id="comments_fo rm" action="comment s.aspx" runat="server"
onsubmit="retu rn jcap();" >

<p>If you would like to make a comment about the anything
that you see on our web site
or ask a question, then please feel free to use this
comments form.</p>

<b>Your Name :</b><br/>
<input id="name" type="text" size="30" maxlength="256"
name="Senders_ Name"/>
<p></p>

<b>Your E-Mail Address :</b><br/>
<input id="email" type="text" size="30" maxlength="80"
name="_reply "
onblur="return isValidEmail(em ail, true)"/>

<p></p>
<b>Your Phone Number:</b><br/>
<input id="phone"type= "text" size="15" maxlength="15"
name="Phone"/>
<p></p>

<b>Your Comments :</b><br/>
<textarea id="message" name="Senders_C omments" rows="6"
cols="60"></textarea>
<p></p>
<p>Please enter the word that you see in the image </p>
<p><script type="text/javascript">sjc ap();</script></p>
<noscript><p>[This resource requires a Javascript enabled
browser.]</p></noscript>
<asp:button runat="server" id="contact_us " Text="Submit"
OnClick="conta ctUS_Click" />
<input type="reset" value="Reset"/>
</form>
</asp:panel>

<asp:panel id="panelMailSe nt" runat="server" Visible="False" >
Thank you for your comments.
</asp:panel>

Aug 17 '08 #3
Get your ISP to turn CAS policy off on the server while you test. If the
problem goes away, you have to create a CAS policy to grant your code the
appropriate permission. If turning off CAS policy doesn't work, roll up your
sleeves because it will be a long night.

caspol -s off

I still don't really understand why you are getting this error. I also don't
recognize your code, smtp client takes two parameters, is this a custom smtp
class you created? In any case, you may be able to use this code to patch it
http://msdn.microsoft.com/en-us/libr...ermission.aspx
however, it leads me to believe something changed in the framework. Can you
tell me what version of the .net framework you are running? I'm missing
something here.
--

Regards,
Alvin Bruney [MVP ASP.NET]

[Shameless Author plug]
Download OWC Black Book, 2nd Edition
Exclusively on www.lulu.com/owc $15.00
Need a free copy of VSTS 2008 w/ MSDN Premium?
http://msmvps.com/blogs/alvin/Default.aspx
-------------------------------------------------------
"Henry Stock" <he***@henry-stock.comwrote in message
news:Oe******** ******@TK2MSFTN GP02.phx.gbl...
This is definitely a web application and this is what I am getting on
their server.

In my test evironment I am getting an SmtpException at the point of
sending the message, but this may be because my local server does not have
appropriate access to the remote mail forwarder. I know that the user ID
and password I am using in my web.config are valid. My ISP told me to use
the server specified in code. The one I use for my normal mail client is
"smtp.1and1.com ".

What is a CAS policy? Can I set this up in code or do I have to get my
ISP involved?

"Alvin Bruney [ASP.NET MVP]" <vapor dan using hot male spam filterwrote
in message news:B4******** *************** ***********@mic rosoft.com...
>The worker process running your application does not have permission to
read the environment variable. I think you know this part. What I don't
understand is what it is happening A couple of questions. Is this a web
app or windows app? You can fix this easy with an appropriate CAS policy
but I'd recommend you figure out what is going wrong first.

--

Regards,
Alvin Bruney [MVP ASP.NET]

[Shameless Author plug]
Download OWC Black Book, 2nd Edition
Exclusively on www.lulu.com/owc $15.00
Need a free copy of VSTS 2008 w/ MSDN Premium?
http://msmvps.com/blogs/alvin/Default.aspx
-------------------------------------------------------
"Henry Stock" <he***@henry-stock.comwrote in message
news:#l******* *******@TK2MSFT NGP02.phx.gbl.. .
>>>
I am trying to understand the following error:
Any thing you can tell me about this is appreciated.

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.

Exception Details: for the permission of type
'System.Secur ity.Permissions .EnvironmentPer mission, mscorlib,
Version=2.0.0 .0, Culture=neutral , PublicKeyToken= b77a5c561934e08 9'
failed.

Source Error:
Line 45:
Line 46: //create the smtp client
Line 47: SmtpClient _smtp = new
SmtpClient("m relay.perfora.n et");
Line 48: _smtp.DeliveryM ethod = SmtpDeliveryMet hod.Network;
Line 49: try

Source File: e:\kunden\homep ages\34\d252335 749\comments.as px.cs Line:
47

Stack Trace:
[SecurityExcepti on: Request for the permission of type
'System.Secur ity.Permissions .EnvironmentPer mission, mscorlib,
Version=2.0.0 .0, Culture=neutral , PublicKeyToken= b77a5c561934e08 9'
failed.]
System.Security .CodeAccessSecu rityEngine.Chec k(Object demand,
StackCrawlMar k& stackMark, Boolean isPermSet) +0
System.Security .CodeAccessPerm ission.Demand() +59
System.Net.Cred entialCache.get _DefaultCredent ials() +59

System.Net.Co nfiguration.Smt pNetworkElement Internal..ctor( SmtpNetworkElem ent
element) +55
System.Net.Conf iguration.SmtpS ectionInternal. .ctor(SmtpSecti on
section) +65
System.Net.Conf iguration.SmtpS ectionInternal. GetSection() +173
System.Net.Mail .SmtpClient.get _MailConfigurat ion() +45
System.Net.Mail .SmtpClient.Ini tialize() +223
System.Net.Mail .SmtpClient..ct or(String host) +162
comments.contac tUS_Click(Objec t sender, EventArgs e) in
e:\kunden\hom epages\34\d2523 35749\comments. aspx.cs:47
System.Web.UI.W ebControls.Butt on.OnClick(Even tArgs e) +105
System.Web.UI.W ebControls.Butt on.RaisePostBac kEvent(String
eventArgument ) +107

System.Web.UI .WebControls.Bu tton.System.Web .UI.IPostBackEv entHandler.Rais ePostBackEvent( String
eventArgument ) +7
System.Web.UI.P age.RaisePostBa ckEvent(IPostBa ckEventHandler
sourceControl , String eventArgument) +11
System.Web.UI.P age.RaisePostBa ckEvent(NameVal ueCollection postData)
+33
System.Web.UI.P age.ProcessRequ estMain(Boolean
includeStages BeforeAsyncPoin t, Boolean includeStagesAf terAsyncPoint)
+7350
System.Web.UI.P age.ProcessRequ est(Boolean
includeStages BeforeAsyncPoin t, Boolean includeStagesAf terAsyncPoint)
+213
System.Web.UI.P age.ProcessRequ est() +86
System.Web.UI.P age.ProcessRequ estWithNoAssert (HttpContext context) +18
System.Web.UI.P age.ProcessRequ est(HttpContext context) +49
ASP.comments_as px.ProcessReque st(HttpContext context) in
App_Web_xytww 0yg.0.cs:0

System.Web.Ca llHandlerExecut ionStep.System. Web.HttpApplica tion.IExecution Step.Execute()
+358
System.Web.Http Application.Exe cuteStep(IExecu tionStep step, Boolean&
completedSync hronously) +64
When I did a Google search I came up with the following url and
explanation :

http://support.microsoft.com/kb/814741

CAUSE
The System.Security .Permissions.En vironmentPermis sion class in
Mscorlib.dl l controls the access to the user environment variables.
SystemInforma tion.UserName in the application requests the permission
from the EnvironmentPerm ission class to access the UserName environment
variable. However, in the Local Intranet zone, System.Windows. Forms does
not have permissions to access the Windows user name, and the request is
not served by Mscorlib.dll. Therefore, a security exception is raised
when you run the application.

The problem with this is that the code they give you to test with is
linked to Windows Forms;

//display the UserName currently logged
Console.WriteLi ne(System.Envir onment.UserName );
Console.ReadLin e();

Even if I set this to Response.Write( ... );
System.Enviro nment.UserName is not available in a remote web
environment .

The following is code from my web.config file. I have disguised my
userID and Password.

<system.net >
<mailSettings >
<smtp deliveryMethod= "Network">
<!-- The <networknode supports the following properties, but we
won't use all of them
<network host="127.0.0.1 " port="25" userName="myUse rName"
password="Ope nSesame" defaultCredenti als="true" />
-->

<network host="mrelay.pe rfora.net" userName="in**@ mydomain.org"
password="myp wd" defaultCredenti als="true"/>
</smtp>
</mailSettings>
</system.net>

The following is the click event function that triggers the error code;
it is linked to a submit button on the form:

protected void contactUS_Click (object sender, EventArgs e)
{
//Things to Do:
// Validate form fields:
// Name field should be letters and spaces only. Can't be blank
or only spaces.
// Phone field may have () - spaces and digits
// email address must be properly formatted.
// Eventually I would like to add a check for domain validity
// After edits I need to create the message
//String from_addr = new String(FindCont rol("email").To String());
MailAddress _from = new MailAddress("in **@mydomain.org ");
MailAddress _sender = new MailAddress("in **@mydomain.org ");
MailAddress _to = new MailAddress("we *******@mydomai n.org");
MailMessage msg = new MailMessage(_fr om,_to);
msg.Sender = _sender;
msg.Subject = "Feedback from Comments Form";
StringBuilder sbuilder = new StringBuilder() ;
//Build string for message body
sbuilder.Append Line("Senders IP Address: " +
Request.Serve rVariables["remote_add r"].ToString());
sbuilder.Append Line("Name: " +
comments_form .Controls[0].ToString());
sbuilder.Append Line("Email: " +
comments_form .Controls[1].ToString());
sbuilder.Append Line("Phone: " +
comments_form .Controls[2].ToString());
sbuilder.Append Line("Message: " +
comments_form .Controls[3].ToString());
//assign string value to message body
msg.Body = sbuilder.ToStri ng();

//create the smtp client

SmtpClient _smtp = new SmtpClient("mre lay.perfora.net ");
_smtp.DeliveryM ethod = SmtpDeliveryMet hod.Network;

try
{
_smtp.Send(msg) ;

}
catch (ArgumentNullEx ception )
{
Response.Write( "Argument Null Exception");
}
catch (ArgumentOutOfR angeException )
{
Response.Write( "Argument out of Range Exception");
}
catch (SmtpException )
{
Response.Write( "SMTP Exception");
}
catch
{
Response.Write( "Something else happened");
}

}

The following is the web page form that I am dealing with:

<asp:panel id="panelSendEm ail" runat="server">
<form id="comments_fo rm" action="comment s.aspx" runat="server"
onsubmit="ret urn jcap();" >

<p>If you would like to make a comment about the anything
that you see on our web site
or ask a question, then please feel free to use this
comments form.</p>

<b>Your Name :</b><br/>
<input id="name" type="text" size="30" maxlength="256"
name="Senders _Name"/>
<p></p>

<b>Your E-Mail Address :</b><br/>
<input id="email" type="text" size="30" maxlength="80"
name="_repl y"
onblur="return isValidEmail(em ail, true)"/>

<p></p>
<b>Your Phone Number:</b><br/>
<input id="phone"type= "text" size="15" maxlength="15"
name="Phone "/>
<p></p>

<b>Your Comments :</b><br/>
<textarea id="message" name="Senders_C omments" rows="6"
cols="60"></textarea>
<p></p>
<p>Please enter the word that you see in the image </p>
<p><script type="text/javascript">sjc ap();</script></p>
<noscript><p>[This resource requires a Javascript enabled
browser.]</p></noscript>
<asp:button runat="server" id="contact_us " Text="Submit"
OnClick="cont actUS_Click" />
<input type="reset" value="Reset"/>
</form>
</asp:panel>

<asp:panel id="panelMailSe nt" runat="server" Visible="False" >
Thank you for your comments.
</asp:panel>

Aug 17 '08 #4
http://msdn.microsoft.com/en-us/libr...nt(VS.80).aspx

I was guided by a tech support person at 1and1 in using that constructor,
but the link above is a reference on the Microsoft site that says that is
legal.

I had already tried the constructor with two parameters, a server, and a
port number. I did not try the constructor without any parameters that
should take data directly from the web.config file, if I understand it
correctly.
"Alvin Bruney [ASP.NET MVP]" <vapor dan using hot male spam filterwrote in
message news:54******** *************** ***********@mic rosoft.com...
Get your ISP to turn CAS policy off on the server while you test. If the
problem goes away, you have to create a CAS policy to grant your code the
appropriate permission. If turning off CAS policy doesn't work, roll up
your sleeves because it will be a long night.

caspol -s off

I still don't really understand why you are getting this error. I also
don't recognize your code, smtp client takes two parameters, is this a
custom smtp class you created? In any case, you may be able to use this
code to patch it
http://msdn.microsoft.com/en-us/libr...ermission.aspx
however, it leads me to believe something changed in the framework. Can
you tell me what version of the .net framework you are running? I'm
missing something here.
--

Regards,
Alvin Bruney [MVP ASP.NET]

[Shameless Author plug]
Download OWC Black Book, 2nd Edition
Exclusively on www.lulu.com/owc $15.00
Need a free copy of VSTS 2008 w/ MSDN Premium?
http://msmvps.com/blogs/alvin/Default.aspx
-------------------------------------------------------
"Henry Stock" <he***@henry-stock.comwrote in message
news:Oe******** ******@TK2MSFTN GP02.phx.gbl...
>This is definitely a web application and this is what I am getting on
their server.

In my test evironment I am getting an SmtpException at the point of
sending the message, but this may be because my local server does not
have appropriate access to the remote mail forwarder. I know that the
user ID and password I am using in my web.config are valid. My ISP told
me to use the server specified in code. The one I use for my normal mail
client is "smtp.1and1.com ".

What is a CAS policy? Can I set this up in code or do I have to get my
ISP involved?

"Alvin Bruney [ASP.NET MVP]" <vapor dan using hot male spam filterwrote
in message news:B4******** *************** ***********@mic rosoft.com...
>>The worker process running your application does not have permission to
read the environment variable. I think you know this part. What I don't
understand is what it is happening A couple of questions. Is this a web
app or windows app? You can fix this easy with an appropriate CAS policy
but I'd recommend you figure out what is going wrong first.

--

Regards,
Alvin Bruney [MVP ASP.NET]

[Shameless Author plug]
Download OWC Black Book, 2nd Edition
Exclusively on www.lulu.com/owc $15.00
Need a free copy of VSTS 2008 w/ MSDN Premium?
http://msmvps.com/blogs/alvin/Default.aspx
-------------------------------------------------------
"Henry Stock" <he***@henry-stock.comwrote in message
news:#l****** ********@TK2MSF TNGP02.phx.gbl. ..

I am trying to understand the following error:
Any thing you can tell me about this is appreciated.

Security Exception
Descriptio n: 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.

Exception Details: for the permission of type
'System.Secu rity.Permission s.EnvironmentPe rmission, mscorlib,
Version=2.0. 0.0, Culture=neutral , PublicKeyToken= b77a5c561934e08 9'
failed.

Source Error:
Line 45:
Line 46: //create the smtp client
Line 47: SmtpClient _smtp = new
SmtpClient(" mrelay.perfora. net");
Line 48: _smtp.DeliveryM ethod = SmtpDeliveryMet hod.Network;
Line 49: try

Source File: e:\kunden\homep ages\34\d252335 749\comments.as px.cs
Line: 47

Stack Trace:
[SecurityExcepti on: Request for the permission of type
'System.Secu rity.Permission s.EnvironmentPe rmission, mscorlib,
Version=2.0. 0.0, Culture=neutral , PublicKeyToken= b77a5c561934e08 9'
failed.]
System.Security .CodeAccessSecu rityEngine.Chec k(Object demand,
StackCrawlMa rk& stackMark, Boolean isPermSet) +0
System.Security .CodeAccessPerm ission.Demand() +59
System.Net.Cred entialCache.get _DefaultCredent ials() +59

System.Net.C onfiguration.Sm tpNetworkElemen tInternal..ctor (SmtpNetworkEle ment
element) +55
System.Net.Conf iguration.SmtpS ectionInternal. .ctor(SmtpSecti on
section) +65
System.Net.Conf iguration.SmtpS ectionInternal. GetSection() +173
System.Net.Mail .SmtpClient.get _MailConfigurat ion() +45
System.Net.Mail .SmtpClient.Ini tialize() +223
System.Net.Mail .SmtpClient..ct or(String host) +162
comments.contac tUS_Click(Objec t sender, EventArgs e) in
e:\kunden\ho mepages\34\d252 335749\comments .aspx.cs:47
System.Web.UI.W ebControls.Butt on.OnClick(Even tArgs e) +105
System.Web.UI.W ebControls.Butt on.RaisePostBac kEvent(String
eventArgumen t) +107

System.Web.U I.WebControls.B utton.System.We b.UI.IPostBackE ventHandler.Rai sePostBackEvent (String
eventArgumen t) +7
System.Web.UI.P age.RaisePostBa ckEvent(IPostBa ckEventHandler
sourceContro l, String eventArgument) +11
System.Web.UI.P age.RaisePostBa ckEvent(NameVal ueCollection postData)
+33
System.Web.UI.P age.ProcessRequ estMain(Boolean
includeStage sBeforeAsyncPoi nt, Boolean includeStagesAf terAsyncPoint)
+7350
System.Web.UI.P age.ProcessRequ est(Boolean
includeStage sBeforeAsyncPoi nt, Boolean includeStagesAf terAsyncPoint)
+213
System.Web.UI.P age.ProcessRequ est() +86
System.Web.UI.P age.ProcessRequ estWithNoAssert (HttpContext context)
+18
System.Web.UI.P age.ProcessRequ est(HttpContext context) +49
ASP.comments_as px.ProcessReque st(HttpContext context) in
App_Web_xytw w0yg.0.cs:0

System.Web.C allHandlerExecu tionStep.System .Web.HttpApplic ation.IExecutio nStep.Execute()
+358
System.Web.Http Application.Exe cuteStep(IExecu tionStep step, Boolean&
completedSyn chronously) +64
When I did a Google search I came up with the following url and
explanatio n:

http://support.microsoft.com/kb/814741

CAUSE
The System.Security .Permissions.En vironmentPermis sion class in
Mscorlib.d ll controls the access to the user environment variables.
SystemInform ation.UserName in the application requests the permission
from the EnvironmentPerm ission class to access the UserName environment
variable. However, in the Local Intranet zone, System.Windows. Forms
does not have permissions to access the Windows user name, and the
request is not served by Mscorlib.dll. Therefore, a security exception
is raised when you run the application.

The problem with this is that the code they give you to test with is
linked to Windows Forms;

//display the UserName currently logged
Console.WriteLi ne(System.Envir onment.UserName );
Console.ReadLin e();

Even if I set this to Response.Write( ... );
System.Envir onment.UserName is not available in a remote web
environmen t.

The following is code from my web.config file. I have disguised my
userID and Password.

<system.ne t>
<mailSetting s>
<smtp deliveryMethod= "Network">
<!-- The <networknode supports the following properties, but we
won't use all of them
<network host="127.0.0.1 " port="25" userName="myUse rName"
password="Op enSesame" defaultCredenti als="true" />
-->

<network host="mrelay.pe rfora.net" userName="in**@ mydomain.org"
password="my pwd" defaultCredenti als="true"/>
</smtp>
</mailSettings>
</system.net>

The following is the click event function that triggers the error code;
it is linked to a submit button on the form:

protected void contactUS_Click (object sender, EventArgs e)
{
//Things to Do:
// Validate form fields:
// Name field should be letters and spaces only. Can't be blank
or only spaces.
// Phone field may have () - spaces and digits
// email address must be properly formatted.
// Eventually I would like to add a check for domain validity
// After edits I need to create the message
//String from_addr = new
String(FindC ontrol("email") .ToString());
MailAddress _from = new MailAddress("in **@mydomain.org ");
MailAddress _sender = new MailAddress("in **@mydomain.org ");
MailAddress _to = new MailAddress("we *******@mydomai n.org");
MailMessage msg = new MailMessage(_fr om,_to);
msg.Sender = _sender;
msg.Subject = "Feedback from Comments Form";
StringBuilder sbuilder = new StringBuilder() ;
//Build string for message body
sbuilder.Append Line("Senders IP Address: " +
Request.Serv erVariables["remote_add r"].ToString());
sbuilder.Append Line("Name: " +
comments_for m.Controls[0].ToString());
sbuilder.Append Line("Email: " +
comments_for m.Controls[1].ToString());
sbuilder.Append Line("Phone: " +
comments_for m.Controls[2].ToString());
sbuilder.Append Line("Message: " +
comments_for m.Controls[3].ToString());
//assign string value to message body
msg.Body = sbuilder.ToStri ng();

//create the smtp client

SmtpClient _smtp = new SmtpClient("mre lay.perfora.net ");
_smtp.DeliveryM ethod = SmtpDeliveryMet hod.Network;

try
{
_smtp.Send(msg) ;

}
catch (ArgumentNullEx ception )
{
Response.Write( "Argument Null Exception");
}
catch (ArgumentOutOfR angeException )
{
Response.Write( "Argument out of Range Exception");
}
catch (SmtpException )
{
Response.Write( "SMTP Exception");
}
catch
{
Response.Write( "Something else happened");
}

}

The following is the web page form that I am dealing with:

<asp:panel id="panelSendEm ail" runat="server">
<form id="comments_fo rm" action="comment s.aspx"
runat="serve r" onsubmit="retur n jcap();" >

<p>If you would like to make a comment about the
anything that you see on our web site
or ask a question, then please feel free to use this
comments form.</p>

<b>Your Name :</b><br/>
<input id="name" type="text" size="30" maxlength="256"
name="Sender s_Name"/>
<p></p>

<b>Your E-Mail Address :</b><br/>
<input id="email" type="text" size="30" maxlength="80"
name="_reply "
onblur="return isValidEmail(em ail, true)"/>

<p></p>
<b>Your Phone Number:</b><br/>
<input id="phone"type= "text" size="15" maxlength="15"
name="Phon e"/>
<p></p>

<b>Your Comments :</b><br/>
<textarea id="message" name="Senders_C omments" rows="6"
cols="60"> </textarea>
<p></p>
<p>Please enter the word that you see in the image </p>
<p><script type="text/javascript">sjc ap();</script></p>
<noscript><p>[This resource requires a Javascript
enabled browser.]</p></noscript>
<asp:button runat="server" id="contact_us " Text="Submit"
OnClick="con tactUS_Click" />
<input type="reset" value="Reset"/>
</form>
</asp:panel>

<asp:panel id="panelMailSe nt" runat="server" Visible="False" >
Thank you for your comments.
</asp:panel>


Aug 17 '08 #5
I read the stuff on this
SmtpPermission class but I am still not sure how to put it to use.

I used http://msdn.microsoft.com/en-us/libr...dw(VS.80).aspx

because I have the 2.0 framework. From the sample code I used the following

public static SmtpPermission CreateUnrestric tedPermission()

{

SmtpPermission allAccess = new

SmtpPermission( System.Security .Permissions.Pe rmissionState.U nrestricted);

return allAccess;

}

Not sure how to use this...

"Alvin Bruney [ASP.NET MVP]" <vapor dan using hot male spam filterwrote in
message news:54******** *************** ***********@mic rosoft.com...
Get your ISP to turn CAS policy off on the server while you test. If the
problem goes away, you have to create a CAS policy to grant your code the
appropriate permission. If turning off CAS policy doesn't work, roll up
your sleeves because it will be a long night.

caspol -s off

I still don't really understand why you are getting this error. I also
don't recognize your code, smtp client takes two parameters, is this a
custom smtp class you created? In any case, you may be able to use this
code to patch it
http://msdn.microsoft.com/en-us/libr...ermission.aspx
however, it leads me to believe something changed in the framework. Can
you tell me what version of the .net framework you are running? I'm
missing something here.
--

Regards,
Alvin Bruney [MVP ASP.NET]

[Shameless Author plug]
Download OWC Black Book, 2nd Edition
Exclusively on www.lulu.com/owc $15.00
Need a free copy of VSTS 2008 w/ MSDN Premium?
http://msmvps.com/blogs/alvin/Default.aspx
-------------------------------------------------------
"Henry Stock" <he***@henry-stock.comwrote in message
news:Oe******** ******@TK2MSFTN GP02.phx.gbl...
>This is definitely a web application and this is what I am getting on
their server.

In my test evironment I am getting an SmtpException at the point of
sending the message, but this may be because my local server does not
have appropriate access to the remote mail forwarder. I know that the
user ID and password I am using in my web.config are valid. My ISP told
me to use the server specified in code. The one I use for my normal mail
client is "smtp.1and1.com ".

What is a CAS policy? Can I set this up in code or do I have to get my
ISP involved?

"Alvin Bruney [ASP.NET MVP]" <vapor dan using hot male spam filterwrote
in message news:B4******** *************** ***********@mic rosoft.com...
>>The worker process running your application does not have permission to
read the environment variable. I think you know this part. What I don't
understand is what it is happening A couple of questions. Is this a web
app or windows app? You can fix this easy with an appropriate CAS policy
but I'd recommend you figure out what is going wrong first.

--

Regards,
Alvin Bruney [MVP ASP.NET]

[Shameless Author plug]
Download OWC Black Book, 2nd Edition
Exclusively on www.lulu.com/owc $15.00
Need a free copy of VSTS 2008 w/ MSDN Premium?
http://msmvps.com/blogs/alvin/Default.aspx
-------------------------------------------------------
"Henry Stock" <he***@henry-stock.comwrote in message
news:#l****** ********@TK2MSF TNGP02.phx.gbl. ..

I am trying to understand the following error:
Any thing you can tell me about this is appreciated.

Security Exception
Descriptio n: 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.

Exception Details: for the permission of type
'System.Secu rity.Permission s.EnvironmentPe rmission, mscorlib,
Version=2.0. 0.0, Culture=neutral , PublicKeyToken= b77a5c561934e08 9'
failed.

Source Error:
Line 45:
Line 46: //create the smtp client
Line 47: SmtpClient _smtp = new
SmtpClient(" mrelay.perfora. net");
Line 48: _smtp.DeliveryM ethod = SmtpDeliveryMet hod.Network;
Line 49: try

Source File: e:\kunden\homep ages\34\d252335 749\comments.as px.cs
Line: 47

Stack Trace:
[SecurityExcepti on: Request for the permission of type
'System.Secu rity.Permission s.EnvironmentPe rmission, mscorlib,
Version=2.0. 0.0, Culture=neutral , PublicKeyToken= b77a5c561934e08 9'
failed.]
System.Security .CodeAccessSecu rityEngine.Chec k(Object demand,
StackCrawlMa rk& stackMark, Boolean isPermSet) +0
System.Security .CodeAccessPerm ission.Demand() +59
System.Net.Cred entialCache.get _DefaultCredent ials() +59

System.Net.C onfiguration.Sm tpNetworkElemen tInternal..ctor (SmtpNetworkEle ment
element) +55
System.Net.Conf iguration.SmtpS ectionInternal. .ctor(SmtpSecti on
section) +65
System.Net.Conf iguration.SmtpS ectionInternal. GetSection() +173
System.Net.Mail .SmtpClient.get _MailConfigurat ion() +45
System.Net.Mail .SmtpClient.Ini tialize() +223
System.Net.Mail .SmtpClient..ct or(String host) +162
comments.contac tUS_Click(Objec t sender, EventArgs e) in
e:\kunden\ho mepages\34\d252 335749\comments .aspx.cs:47
System.Web.UI.W ebControls.Butt on.OnClick(Even tArgs e) +105
System.Web.UI.W ebControls.Butt on.RaisePostBac kEvent(String
eventArgumen t) +107

System.Web.U I.WebControls.B utton.System.We b.UI.IPostBackE ventHandler.Rai sePostBackEvent (String
eventArgumen t) +7
System.Web.UI.P age.RaisePostBa ckEvent(IPostBa ckEventHandler
sourceContro l, String eventArgument) +11
System.Web.UI.P age.RaisePostBa ckEvent(NameVal ueCollection postData)
+33
System.Web.UI.P age.ProcessRequ estMain(Boolean
includeStage sBeforeAsyncPoi nt, Boolean includeStagesAf terAsyncPoint)
+7350
System.Web.UI.P age.ProcessRequ est(Boolean
includeStage sBeforeAsyncPoi nt, Boolean includeStagesAf terAsyncPoint)
+213
System.Web.UI.P age.ProcessRequ est() +86
System.Web.UI.P age.ProcessRequ estWithNoAssert (HttpContext context)
+18
System.Web.UI.P age.ProcessRequ est(HttpContext context) +49
ASP.comments_as px.ProcessReque st(HttpContext context) in
App_Web_xytw w0yg.0.cs:0

System.Web.C allHandlerExecu tionStep.System .Web.HttpApplic ation.IExecutio nStep.Execute()
+358
System.Web.Http Application.Exe cuteStep(IExecu tionStep step, Boolean&
completedSyn chronously) +64
When I did a Google search I came up with the following url and
explanatio n:

http://support.microsoft.com/kb/814741

CAUSE
The System.Security .Permissions.En vironmentPermis sion class in
Mscorlib.d ll controls the access to the user environment variables.
SystemInform ation.UserName in the application requests the permission
from the EnvironmentPerm ission class to access the UserName environment
variable. However, in the Local Intranet zone, System.Windows. Forms
does not have permissions to access the Windows user name, and the
request is not served by Mscorlib.dll. Therefore, a security exception
is raised when you run the application.

The problem with this is that the code they give you to test with is
linked to Windows Forms;

//display the UserName currently logged
Console.WriteLi ne(System.Envir onment.UserName );
Console.ReadLin e();

Even if I set this to Response.Write( ... );
System.Envir onment.UserName is not available in a remote web
environmen t.

The following is code from my web.config file. I have disguised my
userID and Password.

<system.ne t>
<mailSetting s>
<smtp deliveryMethod= "Network">
<!-- The <networknode supports the following properties, but we
won't use all of them
<network host="127.0.0.1 " port="25" userName="myUse rName"
password="Op enSesame" defaultCredenti als="true" />
-->

<network host="mrelay.pe rfora.net" userName="in**@ mydomain.org"
password="my pwd" defaultCredenti als="true"/>
</smtp>
</mailSettings>
</system.net>

The following is the click event function that triggers the error code;
it is linked to a submit button on the form:

protected void contactUS_Click (object sender, EventArgs e)
{
//Things to Do:
// Validate form fields:
// Name field should be letters and spaces only. Can't be blank
or only spaces.
// Phone field may have () - spaces and digits
// email address must be properly formatted.
// Eventually I would like to add a check for domain validity
// After edits I need to create the message
//String from_addr = new
String(FindC ontrol("email") .ToString());
MailAddress _from = new MailAddress("in **@mydomain.org ");
MailAddress _sender = new MailAddress("in **@mydomain.org ");
MailAddress _to = new MailAddress("we *******@mydomai n.org");
MailMessage msg = new MailMessage(_fr om,_to);
msg.Sender = _sender;
msg.Subject = "Feedback from Comments Form";
StringBuilder sbuilder = new StringBuilder() ;
//Build string for message body
sbuilder.Append Line("Senders IP Address: " +
Request.Serv erVariables["remote_add r"].ToString());
sbuilder.Append Line("Name: " +
comments_for m.Controls[0].ToString());
sbuilder.Append Line("Email: " +
comments_for m.Controls[1].ToString());
sbuilder.Append Line("Phone: " +
comments_for m.Controls[2].ToString());
sbuilder.Append Line("Message: " +
comments_for m.Controls[3].ToString());
//assign string value to message body
msg.Body = sbuilder.ToStri ng();

//create the smtp client

SmtpClient _smtp = new SmtpClient("mre lay.perfora.net ");
_smtp.DeliveryM ethod = SmtpDeliveryMet hod.Network;

try
{
_smtp.Send(msg) ;

}
catch (ArgumentNullEx ception )
{
Response.Write( "Argument Null Exception");
}
catch (ArgumentOutOfR angeException )
{
Response.Write( "Argument out of Range Exception");
}
catch (SmtpException )
{
Response.Write( "SMTP Exception");
}
catch
{
Response.Write( "Something else happened");
}

}

The following is the web page form that I am dealing with:

<asp:panel id="panelSendEm ail" runat="server">
<form id="comments_fo rm" action="comment s.aspx"
runat="serve r" onsubmit="retur n jcap();" >

<p>If you would like to make a comment about the
anything that you see on our web site
or ask a question, then please feel free to use this
comments form.</p>

<b>Your Name :</b><br/>
<input id="name" type="text" size="30" maxlength="256"
name="Sender s_Name"/>
<p></p>

<b>Your E-Mail Address :</b><br/>
<input id="email" type="text" size="30" maxlength="80"
name="_reply "
onblur="return isValidEmail(em ail, true)"/>

<p></p>
<b>Your Phone Number:</b><br/>
<input id="phone"type= "text" size="15" maxlength="15"
name="Phon e"/>
<p></p>

<b>Your Comments :</b><br/>
<textarea id="message" name="Senders_C omments" rows="6"
cols="60"> </textarea>
<p></p>
<p>Please enter the word that you see in the image </p>
<p><script type="text/javascript">sjc ap();</script></p>
<noscript><p>[This resource requires a Javascript
enabled browser.]</p></noscript>
<asp:button runat="server" id="contact_us " Text="Submit"
OnClick="con tactUS_Click" />
<input type="reset" value="Reset"/>
</form>
</asp:panel>

<asp:panel id="panelMailSe nt" runat="server" Visible="False" >
Thank you for your comments.
</asp:panel>


Aug 17 '08 #6

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

Similar topics

0
3358
by: James B | last post by:
OMA (Outlook Mobile Access) under Exchange is giving me a System.Security.SecurityException error. From what I gather this is a problem with the security level under which the OMA application is trying to run. Anyone got some ideas on where I can start in getting this worked out? I've posted in Exchange but I'm thinking this group may actually have more ideas. Security Exception Description: The application attempted to perform an...
2
3151
by: MFRASER | last post by:
I am getting a System.Security.SecurityException when I try and laod a dataset from an xml file any ideas on what would cause this? System.Security.SecurityException' occurred in mscorlib.dll Additional information: Request for the permission of type System.Security.Permissions.StrongNameIdentityPermission, mscorlib, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 failed.
0
4885
by: Luis Esteban Valencia | last post by:
Hello I have this error when I submit the page with the following code. I had never see this kind of errors. Thanks private void btnsubmit_Click(object sender, System.EventArgs e) { string requestLocation = "http://estacion15/HTTPSender/BTSHTTPReceive.dll";
1
5248
by: Adrian | last post by:
hi I'm attempting to build an app then sends a request to a URL and reads the response it works fine on my test site but when I connect to a real system I get the text below, I guess its responding to a request from the remote end if so how do I either tell it to ignore the request and just send the url or set what its looking for?
3
10441
by: steveeisen | last post by:
microsoft.public.dotnet.languages.vb.upgrade isn't getting much traffic, so I'll try here. Ours is a VB6 shop exploring .NET. First I wrote a new application with VB .NET 2003 to create a pdf Crystal Report from a SQL Server 2000 table. I followed advice from the MSDN article "Add Professional Quality Reports to Your Application with Visual Studio .NET"
0
1423
by: Lambuz | last post by:
Hi all, I've got this problem. I've to implemente a solution like the example in http://support.microsoft.com/default.aspx?scid=kb;EN-US;313891, but I can't configure correctly the example. Every time I click on the textBox a System.Security.SecurityException is raised.
0
2145
by: Siberwulf | last post by:
I'm trying to fetch an XML document from a website through an assembly i built and attached to SQL 2005. Here is my code for the SPROC assembly: public static void fetch_FeedData(string m_URL) { SqlPipe p = SqlContext.Pipe;
3
12057
by: Mike | last post by:
Hi I have problem as folow: Caught Exception: System.Configuration.ConfigurationErrorsException: An error occurred loading a configuration file: Request for the permission of type 'System.Security.Permissions.FileIOPermission, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed. (machine.config) ---> System.Security.SecurityException: Request for the permission of type
4
1772
by: confused1234 | last post by:
I get the error at the bottom of this post when i click on a linkbutton, javascript:__doPostBack('ctl00$Main$btnchangeemail','') The problem is intermitant, sometimes it works and sometimes it errors, The page is running in an iframe if this makes a difference. ANY IDEAS??? Thanks
0
9232
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9084
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8978
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8936
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7827
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6573
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5905
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
2
2399
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2036
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.