ASP.NET loses session data when delete directory on WebApplications - asp.net

I'm creating a web application in asp.net mvc2 and I'm having a little problem in an interesting situation that I assume is a default behaviour on webapplications. (sessionstate= inproc)
When i delete a directory inside the webapplication root Directory, my session data is cleaned.
I've read some questions from people asking for a solution, but not much about the problem, does anyone know how to prevent this behaviour?
Note: I don't want to move directory for other "subdomain" served by other instance of IIS, and don't want to change sessionstate for sqlserver
Thanks in advance for your help.
THANKS GUYS, FOUND THE SOLUTION SOMEWHERE ON A LINK OF THAT ARTICLE.
i've added on my global.asax Application_Start the folowing code:
Dim p As System.Reflection.PropertyInfo = GetType(HttpRuntime).GetProperty("FileChangesMonitor", Reflection.BindingFlags.Public Or Reflection.BindingFlags.NonPublic Or Reflection.BindingFlags.Static)
Dim o As Object = p.GetValue(Nothing, Nothing)
Dim f As System.Reflection.FieldInfo = o.GetType.GetField("_dirMonSubdirs", Reflection.BindingFlags.Instance Or Reflection.BindingFlags.NonPublic Or Reflection.BindingFlags.IgnoreCase)
Dim monitor As Object = f.GetValue(o)
Dim m As System.Reflection.MethodInfo = monitor.GetType.GetMethod("StopMonitoring", Reflection.BindingFlags.Instance Or Reflection.BindingFlags.NonPublic)
m.Invoke(monitor, New Object() {})
THANKS.
http://dotnetslackers.com/Community/blogs/haissam/archive/2008/11/12/disable-session-expiration-when-using-directory-delete.aspx

Most likely relates to this one: ASP.NET Deleting a directory results in application restart.
this blog post might clarify things a bit for you: Deleting Directory in ASP.net 2.0
My suggestion would be to create a virtual directory and manipulate the contents of that directory (instead of touching app directory itself).

Related

virtual directory - How to load image files for asp.net viewing?

There are so many similar questions around the web, but none of them have helped me with this.
I have an asp.net application on a company intranet server. It is used to access a database on another file server, which also contains a number of images relevant to each record in the database.
I am a developer, but not a network person. I know a little bit about IIS, and using the IIS Manager, I created a virtual directory inside the asp.net application folder that maps to the UNC address on the other server where the images are. Using IIS Manager, I can explore the virtual directory and see all the files there. The problem is getting them to show up in my asp.net application.
I've tried setting permissions on the folder for Network Service, IUSR_aspnetserver, and my own credentials; I've tried various permutations of the path string, with / or // or \ or \, etc. I've tried impersonation, but I don't know if I am doing it right. I've placed a "debug" label on the page that tells me the path of the image while the app is running, and it is correct. Nothing seems to work.
Obviously there's something I am missing, but my network comprehension is around zero, my IIS experience is hardly any better, and I can't seem to get our staff of IT "experts" to help. And before you jump to any conclusions, I have a great personality.
Any ideas would be appreciated.
Thanks!
EDIT for #MohsinMehmood:
(I hope this is not too convoluted)
The page has a mapped drive in IIS to UNC share \\OtherServer\AppName\Images to a folder named ReceiptImages.
The web.config file has this entry:
<appSettings>
<add key="ImageFilePath" value="ReceiptImages/"/>
</appSettings>
And in the PageLoad event I have
ViewState("ImagesPath") = ConfigurationManager.AppSettings("ImageFilePath")
To create a global path string.
Then in code (VB) it's:
Dim btnReceiptThumbnailButton As New ImageButton
With btnReceiptThumbnailButton
.ID = "btn" & LineItem
.CssClass = "thumbnails"
.ImageAlign = ImageAlign.AbsMiddle
.Visible = True
.BackColor = Drawing.Color.White
.BorderColor = Drawing.Color.Black
.BorderWidth = 1
.ImageUrl = ViewState("ImagesPath") & LineItem
.CommandName = "GetReceiptImage"
.CommandArgument = LineItem
End With
I don't know if it was important enough to mention that the image is being placed in a button control to make it a thumbnail.

Understanding Directory Securities with an ASP.NET Web Application

I have a requirement to create a directory on my applications server which is secure to all, but the application itself.
I have read this article
System.IO.Directory.CreateDirectory with permissions for only this current user?
Converting to VB, the code it suggests is:
Dim ds As New DirectorySecurity()
Dim sid As New SecurityIdentifier(WellKnownSidType.CreatorOwnerSid, Nothing)
Dim ace As New FileSystemAccessRule(sid,
FileSystemRights.FullControl,
AccessControlType.Allow)
ds.AddAccessRule(ace)
Directory.CreateDirectory(Dir, ds)
But when I follow that code instruction there, I cannot add files to the directory I have created.
I am guessing I should change the value of WellKnownSidType but I do not know what to!
To recap - what I need is a directory which my application can read and write to, but a user cannot access from a web browser to download any content.
Any help much appreciated!

Cannot call Request.PhysicalApplicationPath on VB.NET

I'm trying to request the path of my application to save new files on it, I know I should do something like this:
Dim mypath As String = Request.PhysicalApplicationPath
But for some reason I cannot even find the Request class.
I read it belongs to the System.Web namespace, I added it and still not working. Any ideas?
You can use Server.MapPath("~/logfiles") to get the path of the subdirectory logfiles in the directory where your web pages are.
Is this in an actual ASP.NET web page, or in a handler?
EDIT
Another way is to use Path.Combine(HttpRuntime.AppDomainAppPath, "logfiles")

ASP.NET Generating .pdf files, UnauthorizedAccessException on Save()

I have a list of jobs and i give users the option to get job description in form of a pdf file. I use MigraDoc/PDFsharp to generate pdf files.
The problem is, after i render the pdf document and want to save it somewhere on the DevServer i get UnauthorizedAccessException on creating FileStream in PDFsharp PdfDocument.Save() method.
I never really used Windows for anything more advanced than playing games and i'm not sure why would i get this exception since i'm logged in as Administrator user and i guess that my ASP.NET application is running with Administrator privilleges and should be able to write files pretty much anywhere on filesystem.
The Code.
GridViewRow jobRow = (GridViewRow)(sender as Control).Parent.Parent;
Document jobDocument = new Document();
Section xyz = jobDocument.AddSection();
xyz.AddParagraph("Wonderfull job");
PdfDocumentRenderer pdfRenderer = new PdfDocumentRenderer(true);
pdfRenderer.Document = jobDocument;
pdfRenderer.RenderDocument();
string filename = "Job_" + jobRow["3"] + ".pdf"; // Job_[title].pdf
pdfRenderer.PdfDocument.Save(filename);
Last line is the line that causes the exception.
Any suggestions? I'm not an ASP.NET developer and I'm forced to use ASP.NET for my school project so this may be a very simple problem but i really don't know what to do and what to search. Thanks for answering!
Administrator account =! IIS user.
The IIS User needs to have writing privileges, too!
On most machines it is know as "IIS_User" or maybe "network service". First you can grant writing permissions to every User. If this works for you, you know what to do.
First try to set an absolute path as filename, maybe a directory outside of "C:\inetpub\wwwroot"?
Hope this helps!

Parameter is not valid error when creating new bitmap

I am displaying images on my page and then attempting to find the current height of the images in order to vertically position them. I am doing this by creating a new bitmap and then getting the height of the bitmap, as follows:
Dim sampleImg As New System.Drawing.Bitmap(Server.MapPath(String.Format("/Uploads/Products/Images/w100h100/{0}", e.Item.DataItem("Filename"))))
Dim imgHeight As Integer = sampleImg.Height
When I run this on my local machine, all works fine. However, I have recently uploaded the site so far to my development server and when I run the same code from there, I get this error message:
Parameter is not valid
I have been searching forums to try to find a solution but I have ended up a bit unsure where else to look because the file definitely exists on the development server and the code works fine on my local machine. The server I am using is a VPS and I am quite a beginner with working with servers so I don't know if there is anything I haven't set up on there which is stopping it from working?
Whilst trying to find a solution, I have also tried the following code but I get a System.IO.FileNotFoundException error message:
Dim sampleImg As System.Drawing.Image = System.Drawing.Image.FromFile(Server.MapPath(String.Format("/Uploads/Products/Images/w100h100/{0}", e.Item.DataItem("Filename"))))
I am wondering if anyone can help me find a solution to this, or alternatively is there a different method to acheiving the same end?
All help is very much appreciated. Thanks.
It may be a file permissions issue, you need to ensure that the IUSR account for your site has read access to the image file. To do this -
R-click the file, Properties, Select the "Security" tab - you will have a list of users who have access to the file. A quick check is to add "Everyone" with read priviliges. To keep it locked to your ASP.Net instance only you should start IIS Manager, r-click your app pool and the Identity is the user your app runs under.
If you are running through IIS it may be that the file type isn't set up in the available MIME types for the site.
http://technet.microsoft.com/en-us/library/bb742440.aspx
With IIS7 there is a module for each Site called "MIME Types". Check whether the File Extension is in the list, and if not, add it with the appropriate MIME Type (list of mime types in link above)
I think you have to use ~ root operator to resolve the path issue.
Dim sampleImg As System.Drawing.Image =
System.Drawing.Image.FromFile(Server.MapPath(
String.Format("~/Uploads/Products/Images/w100h100/{0}",
e.Item.DataItem("Filename"))))
OR the specified file is not a valid image file.

Resources