(Author: Shanthi Karthika)
Session Management is the major piece of application development. Maintaining a session will enable the user to login without giving their credentials for the second time. If we want to avoid the user re-login with same credentials we have to clear the session where the user credentials are present which enable the user to login again and again.
Recently I was in need to access each social medias (gmail,twitter and facebook) login page to get the user credentials. Here each social medias will maintain a session each time I make a request to access the page. Because of this, I faced a problem once a user provides their credential to the twitter or facebook a session was maintained, so that when I passed the request for the second time the credentials of the same user who have logged before were passed. Because of this the next users credentials or the different credentials of the same user were not able to pass. I struck upon with this half past a day. I tried deleting the cookies and Internet cache of my browser that i used, but nothing gave me a hand. Then I came to know each media maintains a session so that it can get the credentials of the user first logged in.
To delete the session data following code can be followed.
private const int INTERNET_OPTION_END_BROWSER_SESSION = 42;
DLL for deleting session data:
[DllImport("wininet.dll", SetLastError = true)]
private static extern bool InternetSetOption(IntPtr hInternet, int dwOption, IntPtr lpBuffer, int lpdwBufferLength);
Use this method wherever need to clear session.
InternetSetOption(IntPtr.Zero, INTERNET_OPTION_END_BROWSER_SESSION, IntPtr.Zero, 0);
I used this method to clear the session datas and that worked fine. Now i am able to pass the request and get the different credentials.
On trying to delete the Internet cache and the cookies.I found many samples stating the same process of getting all the files in which cookie were present. But I found a difficulty in deleting index.dat file. I searched for half past day for deleting the cookies and cache. Finally I got up with the following solution
System.Diagnostics.Process.Start("rundll32.exe", "InetCpl.cpl,ClearMyTracksByProcess 8"); // Clear Temporary Files
System.Diagnostics.Process.Start("rundll32.exe", "InetCpl.cpl,ClearMyTracksByProcess 2"); // Clear Cookies
with this it attempts to open a seperate window which is not needed for a user. To rectify this new window opening,following can be done which runs as a background process.
System.Diagnostics.ProcessStartInfo processInfo = new System.Diagnostics.ProcessStartInfo("rundll32.exe", "InetCpl.cpl,ClearMyTracksByProcess 8");
processInfo.CreateNoWindow = true;
processInfo.WindowStyle = ProcessWindowStyle.Hidden;
processInfo.UseShellExecute = true;
processInfo.RedirectStandardOutput = false;
System.Diagnostics.ProcessStartInfo newprocessInfo = new System.Diagnostics.ProcessStartInfo("rundll32.exe", "InetCpl.cpl,ClearMyTracksByProcess 2");
newprocessInfo.CreateNoWindow = true;
newprocessInfo.WindowStyle = ProcessWindowStyle.Hidden;
newprocessInfo.UseShellExecute = true;
newprocessInfo.RedirectStandardOutput = false;
you cannot see any window but the cache and cookies will be deleted.
The above code will delete all the cookies and Temporary files but the code below can be used for deleting particular cookies. Here I have given about deleting only gmail cookies.
foreach (string currentFile in gmailCookies)
using (StreamReader streamreader = File.OpenText(currentFile))
string readContent = streamreader.ReadToEnd();
if (readContent.Contains(“google.com”) ||readContent.Contains(“google.co.in”))