System.Security.SecurityException: on calendar - asp.net

Hello I am receiving this error message when my application is being viewed on the remote machine(internet)
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: System.Security.SecurityException: Request for the permission of type 'System.Security.Permissions.EnvironmentPermission, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed.
Source Error:
Line 7: Dim oBF As New BinaryFormatter()
Line 8: Dim oFS As FileStream
Line 9: Dim strPath As String = Path.GetTempPath & "schedule.Bin"
Line 10:
Line 11: Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Source File: D:\Hosting\4423045\html\please-god\appointmentscheduler.aspx.vb Line: 9
Stack Trace:
Here is the full codes for the application:
Imports System.IO
Imports System.Runtime.Serialization.Formatters.Binary
Partial Class appointmentscheduler
Inherits System.Web.UI.Page
Dim arrCalendar(12, 31) As String
Dim oBF As New BinaryFormatter()
Dim oFS As FileStream
Dim strPath As String = Path.GetTempPath & "schedule.Bin"
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If (Cache("arrCalendar") Is Nothing) Then
If (File.Exists(strPath)) Then
oFS = New FileStream(strPath, FileMode.Open)
arrCalendar = DirectCast(oBF.Deserialize(oFS), Array)
oFS.Close()
Cache("arrCalendar") = arrCalendar
End If
Else
arrCalendar = Cache("arrCalendar")
End If
End Sub
Protected Sub btnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSave.Click
arrCalendar(Me.myCalendar.SelectedDate.Month, Me.myCalendar.SelectedDate.Day) = Me.myNotes.Text
oFS = New FileStream(strPath, FileMode.Create)
oBF.Serialize(oFS, arrCalendar)
oFS.Close()
Cache("arrCalendar") = arrCalendar
End Sub
Protected Sub btnDelete_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnDelete.Click
arrCalendar(Me.myCalendar.SelectedDate.Month, Me.myCalendar.SelectedDate.Day) = ""
oFS = New FileStream(strPath, FileMode.Create)
oBF.Serialize(oFS, arrCalendar)
oFS.Close()
Cache("arrCalendar") = arrCalendar
Me.myNotes.Text = ""
End Sub
Protected Sub myCalendar_DayRender(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DayRenderEventArgs) Handles myCalendar.DayRender
If arrCalendar(e.Day.Date.Month, e.Day.Date.Day) <> "" Then
e.Cell.BackColor = Drawing.Color.Red
End If
End Sub
Protected Sub myCalendar_SelectionChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles myCalendar.SelectionChanged
Me.myNotes.Text = ""
If arrCalendar(Me.myCalendar.SelectedDate.Month, Me.myCalendar.SelectedDate.Day) <> "" Then
Me.myNotes.Text = arrCalendar(Me.myCalendar.SelectedDate.Month, Me.myCalendar.SelectedDate.Day)
End If
End Sub
End Class
Can someone help me fixt or tell me what to do.

The user that the application is running under doesn't have access to the temporary folder or the specific file(schedule.Bin).
This may be of some help:
How Do I Determine the Security Account that IIS Uses to Run My Web Site?

Related

Call a function from the Login Control in vb.net

Trying to call a function in code behind but not working - one suggestion was to use the OnAuthenticate instead of the OnClick however this requires a rewrite of the entire authentication process.
<asp:Login ID="Login1" runat="server" OnClick="MyButton" DestinationPageUrl="~/Receipt.aspx"
UserNameLabelText="User Name (From: mysite.com):"
PasswordRecoveryText="Forgot User Name or Password?"
PasswordRecoveryUrl="~/GetPassword.aspx">
</asp:Login>
vb code:
Protected Sub MyButton(ByVal sender As Object, ByVal e As System.EventArgs)
Dim username As String = Login1.UserName
Dim currentDateTime As DateTime = DateTime.Now.AddHours(3)
Dim filepath As String = Server.MapPath("~") + "\debug.txt"
Using writer As StreamWriter = File.AppendText(filepath)
writer.WriteLine(username)
writer.WriteLine(currentDateTime)
writer.WriteLine("")
End Using
End Sub
Revised Code:
Imports System.IO
Imports System.Data
Partial Class Login
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim xUserName As String = User.Identity.Name()
'UpdateLastLoginDate(xUserName)
End Sub
Protected Sub MyButton(ByVal sender As Object, ByVal e As System.EventArgs)
Dim username As String = Login1.UserName
Dim pw As String = Login1.Password
Dim currentDateTime As DateTime = DateTime.Now.AddHours(3)
Dim filepath As String = Server.MapPath("~") + "\debug.txt"
Using writer As StreamWriter = File.AppendText(filepath)
writer.WriteLine(username)
writer.WriteLine(pw)
writer.WriteLine(currentDateTime)
writer.WriteLine("REMOTE_ADDR: " + Request.Servervariables("REMOTE_ADDR"))
writer.WriteLine("USER_AGENT: " + Request.Servervariables("HTTP_USER_AGENT"))
writer.WriteLine("LOCAL_ADDR: " + Request.Servervariables("LOCAL_ADDR"))
writer.WriteLine("LOGON_USER: " + Request.Servervariables("LOGON_USER"))
'writer.WriteLine(Request.Servervariables("ALL_HTTP"))
writer.WriteLine("")
End Using
End Sub
Protected Sub Page_PreInit(sender As Object, e As EventArgs)
WireLoginControlButtonClickEvent(Login1)
End Sub
Private Sub WireLoginControlButtonClickEvent(parent As Control)
For Each ctrl As Control In parent.Controls
If TypeOf ctrl Is Button Then
AddHandler DirectCast(ctrl, Button).Click, Function() (MyFunction())
ElseIf ctrl.HasControls() Then
WireLoginControlButtonClickEvent(ctrl)
End If
Next
End Sub
Private Function MyFunction() As String
Response.Write("Function Called")
Return Nothing
End Function
End Class
Give this a try:
Protected Sub Page_PreInit(sender As Object, e As EventArgs)
WireLoginControlButtonClickEvent(Login1)
End Sub
Private Sub WireLoginControlButtonClickEvent(parent As Control)
For Each ctrl As Control In parent.Controls
If TypeOf ctrl Is Button Then
AddHandler DirectCast(ctrl, Button).Click, AddressOf MyFunction
ElseIf ctrl.HasControls() Then
WireLoginControlButtonClickEvent(ctrl)
End If
Next
End Sub
Private Sub MyFunction(sender As Object, e As EventArgs)
Response.Write("Function Called")
End Sub

how can we access form2 listbox (lb1) in form3

Imports System.Windows.Forms.ListBox
Public Class Form2
Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim mf1 As New Form3()
Form3.Visible = True
Me.Hide()
End Sub
Imports System.Data.OleDb
Public Class Form3
Private Class dataaccess
Public Shared Function getconnection() As OleDbConnection
'string constr = #"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\database\Database2007.accdb";
Dim constr1 As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Suman\Desktop\vs ws\vb\project_sample1\Database1.accdb"
Return New OleDbConnection(constr1)
End Function
End Class
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim con As OleDbConnection = dataaccess.getconnection()
Dim query As String = "SELECT * FROM Burgers"
Dim cmd As New OleDbCommand(query, con)
Dim da As New OleDbDataAdapter(cmd)
Dim ds As New DataSet()
' Dim x As Integer
da.Fill(ds)
**lb1**.Items.Add(ds.Tables(0).Rows(0).ItemArray(0).ToString())
End Sub
I want to store the form 3 data into lb1 listbox which is declared in the form 2
You can pass a reference to that specific listbox or the entire form2 to form3. the easy way is to write it into the constructor of the form.
In Form3:
private _TargetListBox as ListBox
public sub New(ByRef TargetListBox as ListBox)
_TargetListBox = TargetListBox
end sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim con As OleDbConnection = dataaccess.getconnection()
Dim query As String = "SELECT * FROM Burgers"
Dim cmd As New OleDbCommand(query, con)
Dim da As New OleDbDataAdapter(cmd)
Dim ds As New DataSet()
' Dim x As Integer
da.Fill(ds)
_TargetListBox.Items.Add(ds.Tables(0).Rows(0).ItemArray(0).ToString())
End Sub
In Form2:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim mf1 As New Form3(Me.lb1)
mf1.Visible = True
Me.Hide()
End Sub

how to make dir for fileuploader on the site folders

Partial Class admin_upload
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim UpPath As String
Dim UpName As String
UpPath = "/images"
UpName = Dir(UpPath, vbDirectory)
If (UpName = "") Then
MkDir("/images")
End If
End Sub
Protected Sub uplodto_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles uplodto.Click
FileName.InnerHtml = FileFiled.PostedFile.FileName
FileContent.InnerHtml = FileFiled.PostedFile.ContentType
FileSize.InnerHtml = FileFiled.PostedFile.ContentLength
UploadDetails.Visible = True
Dim myfilename As String
myfilename = FileFiled.PostedFile.FileName
Dim c As String = System.IO.Path.GetFileName(myfilename)
Try
FileFiled.PostedFile.SaveAs("images\" + c)
Span1.InnerHtml = "File uploaded successfuly"
Catch ex As Exception
Span1.InnerHtml = "faild"
UploadDetails.Visible = False
End Try
End Sub
End Class
i want to make the direction of files on the web site folders & server
If Not System.IO.Directory.Exists(Server.MapPath("~/images/")) Then
System.IO.Directory.CreateDirectory(Server.MapPath("~/images/"))
End If

how to configure smtp settings

Imports System.Net.Mail
Partial Class ContactUs
Inherits System.Web.UI.Page
Protected Sub LinkButton1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles LinkButton1.Click
Dim username = User.Identity.Name.ToString
Response.Redirect("~/ViewCart_aspx/ViewCart.aspx?userID=" & username)
End Sub
Protected Sub CustomValidator1_ServerValidate(ByVal source As Object, ByVal args As System.Web.UI.WebControls.ServerValidateEventArgs) Handles CustomValidator1.ServerValidate
If txtComments.Text.Length > 200 Then
args.IsValid = False
Else
args.IsValid = True
End If
End Sub
Protected Sub WizardStep3_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles WizardStep3.Load
lblName.text = txtName.Text
lblEmail.Text = txtEmail.Text
lblComments.Text = txtComments.Text
lblRating.Text = txtRatings.Text
End Sub
Protected Sub Wizard1_FinishButtonClick(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.WizardNavigationEventArgs) Handles Wizard1.FinishButtonClick
SendMail(txtEmail.Text, txtComments.Text)
'MsgBox("Feedback Sent")
Response.Redirect("homepage_aspx/homepage.aspx")
End Sub
Private Sub SendMail(ByVal from As String, ByVal body As String)
Dim mMailSettings As System.Net.Configuration.MailSettingsSectionGroup
Dim mPort As Integer = mMailSettings.Smtp.Network.Port
Dim mHost As String = mMailSettings.Smtp.Network.Host
Dim mPassword As String = mMailSettings.Smtp.Network.Password
Dim mUsername As String = mMailSettings.Smtp.Network.UserName
'Dim mailServerName As String = "smtp.tricedeals.com"
'Dim message As MailMessage = New MailMessage(from, "admin#tricedeals.com", "feedback", body)
'Dim mailClient As SmtpClient = New SmtpClient
'mailClient.Host = mailServerName
'mailClient.Send(message)
'message.Dispose()
End Sub
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Response.Cache.SetCacheability(HttpCacheability.NoCache)
End Sub
End Class
what is wrong with my smtp settings??i cant send emails.please help me.
you can't send email because the send command is commented out. since you don't give more details or better explanation or exception message that's the only thing I can assume.

ASP NET Stop other sub proc from executing after error

I am having a problem with my try catch block runs in the error:
database is not available
My problem is the try catch runs and catches the error, but it will not redirect to my error page. The code continues to execute the other subs. I have tried adding: exit, return, response.end. None of them worked. Thank you for your help.
Imports System.Data
Imports EUC
Imports System.Threading
Imports System.Data.SqlClient
Partial Class mpMain
Inherits System.Web.UI.MasterPage
Dim strSQL As String
Dim objData As clsDataAccess = New clsDataAccess()
Dim ds As New D
ataSet
Dim t1 As DataTable
Dim intSecurityLevel As String = 0
Dim strUser As String = UCase(Right(HttpContext.Current.User.Identity.Name.ToString(), 4))
Protected Sub ExpandNode(ByVal NodeName As String, ByVal PageName As String)
'More Code
End Sub
Protected Sub ExpandNode2(ByVal NodeNameMain As String, ByVal NodeName As String, ByVal PageName As String)
'More Code
End Sub
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not (IsPostBack) Then
Try
strSQL = "Select SecurityLevel from tblSecurity where SFID = #SFID order by SecurityLevel"
Dim MyParameters1 As SqlParameter() = { _
New SqlParameter("#SFID", SqlDbType.VarChar) _
}
MyParameters1(0).Value = UCase(Right(HttpContext.Current.User.Identity.Name.ToString(), 4))
ds = objData.SQLExecuteDataset(strSQL, CommandType.Text, MyParameters1)
t1 = ds.Tables(0)
Catch ex As Exception
Dim sendError As New clsExceptionMessage
sendError.sendMessage(ex.Message, Request.Path, strSQL)
Response.Redirect("ErrorMessage.aspx", False)
End Try
End If
End Sub
Protected Sub TreeView1_TreeNodeDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.TreeNodeEventArgs) Handles TreeView1.TreeNodeDataBound
'More Code
End Sub
Protected Sub TreeView1_DataBound(ByVal sender As Object, ByVal e As System.EventArgs) Handles TreeView1.DataBound
'More Code
End Sub
End Class
Firstly I would catch SQLException, you rarely want to catch a general exception.
In the catch block, set a boolean parameter such as
hasError = True
and once outside the try..catch block check for this to be true and redirect from there.

Resources