delete or block cookie in web asp.net application

I have web page that has a form to request materials for engineering. The problem is that when this form is submitted creates a cookie in browser and may different users in windows computer log in  and never log off, so this cookie still gets saved when they select an option from a drop down menu and get to see the form, the form auto populates from the cookie .. I dont know if there is away to solve this problem programatically .I am thinking on a function , server side code to delete the cookie once the submit button is pressed, or in the load event. Sorry , my english is not good, but I want to prevent a created cookie to automatically fill the form
LVL 9
TonyRebaAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Anil GolamariCommented:
protected void submitbutton_Click(object sender, EventArgs e)
    {
        Response.Cookies[txtCookieName.Text].Expires = DateTime.Now.AddDays(-1);
    }


Here you can give in what time you want your cookie to be expire. Just set the time and you should be good.
0
TonyRebaAuthor Commented:
what cookie txtCookieName refers to???
0
Anil GolamariCommented:
It referees to cookie name. Take a look at the link below

http://www.codeproject.com/KB/aspnet/cookies_in_c_.aspx
0
Learn Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

TonyRebaAuthor Commented:
the problem is I dont created thiis form , so I dont know the cookie name or how is generated!! is from a company website
0
YiogiCommented:
If it's on your website you can get the cookie from the HttpContext.Current.Request.Cookies on any other page and delete it. Run a page of your own in debug mode or simply put a Response.Write in your own page to output all cookies and their names. Then once you know the cookie name you can set it to expired or remove it.

If the cookie was set from another website and not your own then you can't delete it from your website as it will not be accessible from it.
0
TonyRebaAuthor Commented:
Is on my server but application was built from another company and I dont see the cookie name on the code or how they are creating it. But cookie gets stored pn my web server folder ,
so what options do I have to delete a cookie , meabe a bash program?
0
r3nderCommented:
System.Security.Principal.WindowsPrincipal p = System.Threading.Thread.CurrentPrincipal as System.Security.Principal.WindowsPrincipal;
        string user = p.Identity.Name;
        char[] delimt = {'\\'};
        string [] username = user.Split(delimt);
        string path = "c:\\Documents and Settings\\" + username[1] + "\\cookies";
        System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo(path);
        int count = dir.GetFiles().Length;
        string[] fileList = System.IO.Directory.GetFiles(path , "*.txt");
        int cn = 0;
        foreach (string fl in fileList)
        {
            FileInfo fInfo = new FileInfo(fl);
            if (fInfo.LastWriteTime < DateTime.Now.AddMinutes(-3))
            {
                fInfo.Delete();
             }
        }
This will delete the files in the server not the client.

Remember, ASP.NET is server side code, so the code posted executes on the web server not the client.


0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
TonyRebaAuthor Commented:
Wow thanks, Do I add this script in the web form page that creates the cookie right?


Do I enclose this in <script runat server tags>

or how do Implement it?
0
r3nderCommented:
I would  add it to the page and then calling it on an event like page load or something
ASPX
Call it---

CODE BEHIND
protected void Page_Load(object sender, EventArgs e)
{
CODE
}
0
TonyRebaAuthor Commented:
I dont have the code behind .cs page , how would i do this directly into the aspx?
0
r3nderCommented:
create a new page put the code in it
call it from the ASPX page onload
0
r3nderCommented:
here is vb.net
Dim p As System.Security.Principal.WindowsPrincipal = TryCast(System.Threading.Thread.CurrentPrincipal, System.Security.Principal.WindowsPrincipal)
Dim user As String = p.Identity.Name
Dim delimt As Char() = {"\"C}
Dim username As String() = user.Split(delimt)
Dim path As String = "c:\Documents and Settings\" & username(1) & "\cookies"
Dim dir As New System.IO.DirectoryInfo(path)
Dim count As Integer = dir.GetFiles().Length
Dim fileList As String() = System.IO.Directory.GetFiles(path, "*.txt")
Dim cn As Integer = 0
For Each fl As String In fileList
      Dim fInfo As New FileInfo(fl)
      If fInfo.LastWriteTime < DateTime.Now.AddMinutes(-3) Then
            fInfo.Delete()
      End If
Next
0
TonyRebaAuthor Commented:
Is there any way I can Embed this code on the page aspx without the code behind
0
r3nderCommented:
Embedded code blocks are supported in ASP.NET Web pages primarily to preserve backward compatibility with older ASP technology. In general, using embedded code blocks for complex programming logic is not a best practice, because when the code is mixed on the page with markup, it can be difficult to debug and maintain. In addition, because the code is executed only during the page's render phase, you have substantially less flexibility than with code-behind or script-block code in scoping your code to the appropriate stage of page processing. That being said here is what you asked for.


<%@ Page Language="C#" %>
<script runat=server>
System.Security.Principal.WindowsPrincipal p = System.Threading.Thread.CurrentPrincipal as System.Security.Principal.WindowsPrincipal;
        string user = p.Identity.Name;
        char[] delimt = {'\\'};
        string [] username = user.Split(delimt);
        string path = "c:\\Documents and Settings\\" + username[1] + "\\cookies";
        System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo(path);
        int count = dir.GetFiles().Length;
        string[] fileList = System.IO.Directory.GetFiles(path , "*.txt");
        int cn = 0;
        foreach (string fl in fileList)
        {
            FileInfo fInfo = new FileInfo(fl);
            if (fInfo.LastWriteTime < DateTime.Now.AddMinutes(-3))
            {
                fInfo.Delete();
             }
        }
</script>
<html>
<body>
</body>

Open in new window

0
TonyRebaAuthor Commented:
Hi r3nder , I appreciate your help so far!

I added a new page , and in the code behind page i added your script but doesnt seem to be deleting the

.txt files generated, do you what the issue might be?
0
r3nderCommented:
did you call the page you created from the existing page?
0
r3nderCommented:
or just embed it in your origional page - like I gave you the code for in id# 33445619
0
TonyRebaAuthor Commented:
I copy the aspx and made a new one with code behind, and paste the code into my page load event
0
r3nderCommented:
k
that should work - trouble shoot it by putting a MessageBox in there and getting the file name to see what is found
0
TonyRebaAuthor Commented:
Yes I did that it does get the file name ,, now is deleting, but user have to close the  tab or browser..
0
r3nderCommented:
->use this
close();
->the tab is open browser is implied (JAVASCRIPT)
->confirmed close on a tab
function close_window() {
  if (confirm("Close Window?")) {
    close();
  }
}
->OR
window.close();
->OR
->HTML
<a href="javascript:close_window();">close</a>
->OR
<a href="#" onclick="close_window();return false;">close</a>
OR
C#
void page_load(object s, EventArgs e)  
{  
if (<myMeathodExcecutes>)  
{  
button1.Click=true;  
// or alert user  
//button1.Attributes.Add("onclick", "alert('You clicked the button')");  
// or whatever  
//button1.Attributes.Add("onclick", "anyJavaScriptFunction");  
}  
}  

0
TonyRebaAuthor Commented:
Thank you .Your suggestion worked perfectly
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.