TonyReba
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
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
http://www.codeproject.com/KB/aspnet/cookies_in_c_.aspx
ASKER
the problem is I dont created thiis form , so I dont know the cookie name or how is generated!! is from a company website
If it's on your website you can get the cookie from the HttpContext.Current.Reques t.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.
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.
ASKER
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?
so what options do I have to delete a cookie , meabe a bash program?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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?
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
}
ASPX
Call it---
CODE BEHIND
protected void Page_Load(object sender, EventArgs e)
{
CODE
}
ASKER
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
call it from the ASPX page onload
here is vb.net
Dim p As System.Security.Principal. WindowsPri ncipal = TryCast(System.Threading.T hread.Curr entPrincip al, System.Security.Principal. WindowsPri ncipal)
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(pa th)
Dim count As Integer = dir.GetFiles().Length
Dim fileList As String() = System.IO.Directory.GetFil es(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
Dim p As System.Security.Principal.
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(pa
Dim count As Integer = dir.GetFiles().Length
Dim fileList As String() = System.IO.Directory.GetFil
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
fInfo.Delete()
End If
Next
ASKER
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>
ASKER
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?
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
ASKER
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
that should work - trouble shoot it by putting a MessageBox in there and getting the file name to see what is found
ASKER
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_win dow();">cl ose</a>
->OR
<a href="#" onclick="close_window();re turn 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");
}
}
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_win
->OR
<a href="#" onclick="close_window();re
OR
C#
void page_load(object s, EventArgs e)
{
if (<myMeathodExcecutes>)
{
button1.Click=true;
// or alert user
//button1.Attributes.Add("
// or whatever
//button1.Attributes.Add("
}
}
ASKER
Thank you .Your suggestion worked perfectly
{
Response.Cookies[txtCookie
}
Here you can give in what time you want your cookie to be expire. Just set the time and you should be good.