Link to home
Start Free TrialLog in
Avatar of TonyReba
TonyRebaFlag for United States of America

asked on

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
Avatar of Anil Golamari
Anil Golamari
Flag of United States of America image

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.
Avatar of TonyReba

ASKER

what cookie txtCookieName refers to???
It referees to cookie name. Take a look at the link below

http://www.codeproject.com/KB/aspnet/cookies_in_c_.aspx
the problem is I dont created thiis form , so I dont know the cookie name or how is generated!! is from a company website
Avatar of Yiogi
Yiogi

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.
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?
ASKER CERTIFIED SOLUTION
Avatar of r3nder
r3nder
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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?
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
}
I dont have the code behind .cs page , how would i do this directly into the aspx?
create a new page put the code in it
call it from the ASPX page onload
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
Is there any way I can Embed this code on the page aspx without the code behind
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

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?
did you call the page you created from the existing page?
or just embed it in your origional page - like I gave you the code for in id# 33445619
I copy the aspx and made a new one with code behind, and paste the code into my page load event
k
that should work - trouble shoot it by putting a MessageBox in there and getting the file name to see what is found
Yes I did that it does get the file name ,, now is deleting, but user have to close the  tab or browser..
->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");  
}  
}  

Thank you .Your suggestion worked perfectly