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:
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="info@ 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 fo@mydomain.org ");
MailAddress _sender = new MailAddress("in fo@mydomain.org ");
MailAddress _to = new MailAddress("we bmaster@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>
Comment