Solved

delete or block cookie in web asp.net application

Posted on 2010-08-13
22
444 Views
Last Modified: 2013-11-08
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
0
Comment
Question by:TonyReba
  • 10
  • 9
  • 2
  • +1
22 Comments
 
LVL 18

Expert Comment

by:Anil Golamari
ID: 33433699
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
 
LVL 9

Author Comment

by:TonyReba
ID: 33433845
what cookie txtCookieName refers to???
0
 
LVL 18

Expert Comment

by:Anil Golamari
ID: 33434302
It referees to cookie name. Take a look at the link below

http://www.codeproject.com/KB/aspnet/cookies_in_c_.aspx
0
 
LVL 9

Author Comment

by:TonyReba
ID: 33434481
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
 
LVL 8

Expert Comment

by:Yiogi
ID: 33436044
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
 
LVL 9

Author Comment

by:TonyReba
ID: 33437877
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
 
LVL 6

Accepted Solution

by:
r3nder earned 500 total points
ID: 33439313
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
 
LVL 9

Author Comment

by:TonyReba
ID: 33440687
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
 
LVL 6

Expert Comment

by:r3nder
ID: 33440855
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
 
LVL 9

Author Comment

by:TonyReba
ID: 33441214
I dont have the code behind .cs page , how would i do this directly into the aspx?
0
 
LVL 6

Expert Comment

by:r3nder
ID: 33441351
create a new page put the code in it
call it from the ASPX page onload
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 6

Expert Comment

by:r3nder
ID: 33441401
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
 
LVL 9

Author Comment

by:TonyReba
ID: 33445544
Is there any way I can Embed this code on the page aspx without the code behind
0
 
LVL 6

Expert Comment

by:r3nder
ID: 33445619
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
 
LVL 9

Author Comment

by:TonyReba
ID: 33445747
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
 
LVL 6

Expert Comment

by:r3nder
ID: 33445929
did you call the page you created from the existing page?
0
 
LVL 6

Expert Comment

by:r3nder
ID: 33445948
or just embed it in your origional page - like I gave you the code for in id# 33445619
0
 
LVL 9

Author Comment

by:TonyReba
ID: 33445974
I copy the aspx and made a new one with code behind, and paste the code into my page load event
0
 
LVL 6

Expert Comment

by:r3nder
ID: 33446009
k
that should work - trouble shoot it by putting a MessageBox in there and getting the file name to see what is found
0
 
LVL 9

Author Comment

by:TonyReba
ID: 33446108
Yes I did that it does get the file name ,, now is deleting, but user have to close the  tab or browser..
0
 
LVL 6

Expert Comment

by:r3nder
ID: 33446262
->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
 
LVL 9

Author Closing Comment

by:TonyReba
ID: 33446317
Thank you .Your suggestion worked perfectly
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Use Wufoo, an online form creation tool, to make powerful forms. Learn how to selectively show certain fields based on user input using rules to gather relevant information and data from your forms. The rules feature provides you with an opportunity…
Use Wufoo, an online form creation tool, to make powerful forms. Learn how to choose which pages of your form are visible to your users based on their inputs. The page rules feature provides you with an opportunity to create if:then statements for y…

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now