Is there a way to nullify cookie in Asp.Net - asp.net

I tried
if (Request.Cookies["IsGuest"] != null)
{
Response.Cookies["IsGuest"].Expires = DateTime.Now.AddDays(-1);
//HttpCookie myCookie = new HttpCookie("IsGuest");
//myCookie.Expires = DateTime.Now.AddDays(-1d);
//Response.Cookies.Add(myCookie);
}
string a = Request.Cookies["IsGuest"].Value;
But Request.Cookies["IsGuest"] is never null.
Commenting uncommented code and doing vice versa also did not help.

Try this:
if (Request.Cookies["IsGuest"] != null || Request.Cookies["IsGuest"].Value != "")
{
Response.Cookies["IsGuest"].Expires = DateTime.Now.AddDays(-1);
//HttpCookie myCookie = new HttpCookie("IsGuest");
//myCookie.Expires = DateTime.Now.AddDays(-1d);
//Response.Cookies.Add(myCookie);
}
string a = Request.Cookies["IsGuest"].Value;
I think your Cookie exists but it has no value.
If it's not work, look whats in string a and than you can compare it in the first row.

Related

unable to delete or remove single sign on cookie in asp.net mvc

string[] myCookies = Request.Cookies.AllKeys;
foreach (string cookie in myCookies)
{
Response.Cookies[cookie].Expires = DateTime.Now.AddDays(-1);
}
if (Request.Cookies["UserSettings"] != null)
{
HttpCookie myCookie = new HttpCookie("UserSettings");
myCookie.Expires = DateTime.Now.AddDays(-1d);
Response.Cookies.Add(myCookie);
}

ASP.NET cookie not written? Redirect issue?

I have the following code:
var serializer = new XmlSerializer(typeof(classes.Member));
TextWriter writer = new StringWriter();
serializer.Serialize(writer, active_resident);
HttpCookie cookie = Request.Cookies["ActiveUser"];
if (cookie == null)
{
cookie = new HttpCookie("ActiveUser");
}
cookie.Expires = DateTime.Now.AddHours(1);
cookie.Value = Genesis.Encryption.EncryptAES(writer.ToString(), Genesis.Generic.ReadAppSettingsValue("GenesisEncryptionKey"));
Response.Cookies.Add(cookie);
writer.Close();
// Redirect to Secure Area
Response.Redirect("secure/dashboard.aspx", false);
Strangely, the cookie is not written? However, the ASP.Net Session cookie is? Any ideas?

DataTable Retrieves another DataTable Information?

I Have an Exception in my Live Application..DataTable Retrives another DataTable information..
if (HttpContext.Current.User != null)
{
if (Session["username"] != null)
{
string pageName = Page.Page.AppRelativeVirtualPath.Substring(Page.Page.AppRelativeVirtualPath.LastIndexOf('/') + 1);
DataTable dtFeatures2 = new DataTable();
dtFeatures2.Clear();
objMatermenuPL.usertype = Session["UserType"].ToString();
dtFeatures2 = objMastermenuBAL.GetMastermenu(objMatermenuPL);
DataView dt = new DataView(dtFeatures2);
dt.Sort = "fld_feature ASC";
if (dtFeatures2 != null)
{
foreach (Control control in leftpanel.Controls)
{
Type ty = control.GetType();
if (ty.Name.ToUpper() == "HTMLANCHOR")
{
int i = dt.Find(control.ID.Substring(3));
if (i < 0 && control.ID.Contains("lnk"))
control.Visible = false;
if (control.ID.Contains("lnk") && control.ID.Substring(3) + ".aspx" == pageName)
{
HtmlAnchor a = (HtmlAnchor)control;
a.Attributes.Add("class", "active");
}
}
}
}
}
}
else
{
Response.Redirect("~/Login.aspx");
}
This code we use All most all master pages...If an exception Raise,then it comes directly
this line..
dtFeatures2 = objMastermenuBAL.GetMastermenu(objMatermenuPL);
Message like : Cannot find column fld_feature.
"dtFeatures2" is Filled With Before Opened Page DataTable Information...
This exception gone after some..It's Working Fine 100%....Some times only shows these exception...What happend Here........

Where to put general language adjustment?

Well, the title says it all.
I do the following now in my LanguageFilterAttribute class:
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
var request = filterContext.HttpContext.Request;
string currentUrl = request.RawUrl;
var urlHelper = new UrlHelper(request.RequestContext);
string baseurl = urlHelper.Content("~");
string currentLanguageFromUrl = currentUrl.Split('/')[1];
string currentLanguageFromCulture = CultureHelper.CheckCulture();
var currentLanguageFromCookie = request.Cookies["_culture"];
var possibleCultures = UnitOfWork.CulturesRepository.GetListOfCultureNames();
if (possibleCultures.All(culture => currentLanguageFromUrl != culture))
{
string cultureName;
string newUrl;
if (currentLanguageFromCookie != null)
{
cultureName = currentLanguageFromCookie.Value;
CultureHelper.SetCulture(cultureName);
newUrl = baseurl + cultureName;
filterContext.Result = new RedirectResult(newUrl);
return;
}
if (currentLanguageFromCulture != null)
{
cultureName = currentLanguageFromCulture;
CultureHelper.SetCulture(cultureName);
newUrl = baseurl + cultureName;
filterContext.Result = new RedirectResult(newUrl);
return;
}
cultureName = possibleCultures[0];
CultureHelper.SetCulture(cultureName);
newUrl = baseurl + cultureName;
filterContext.Result = new RedirectResult(newUrl);
return;
}
CultureHelper.SetCulture(currentLanguageFromUrl);
base.OnActionExecuting(filterContext);
};
Which sets the language when you select a new one from the dopdown on the shared Layout page (this works btw, selecting a different language triggers respectively, the above and below class correctly).
public static void SetCulture(string culture)
{
var cultureCookie = HttpContext.Current.Request.Cookies["_culture"] ?? new HttpCookie("_culture");
cultureCookie.Value = culture;
var request = HttpContext.Current.Request;
cultureCookie.Domain = request.Url.Host;
cultureCookie.Expires = DateTime.Now.AddYears(1);
cultureCookie.Path = "/";
HttpContext.Current.Response.Cookies.Add(cultureCookie);
CultureInfo info = CultureInfo.CreateSpecificCulture(culture.ToString());
Thread.CurrentThread.CurrentCulture = info;
Thread.CurrentThread.CurrentUICulture = info;
}
The problem with this is, as you can guess, I will have to apply the [LanguageFilter] attribute on all my controllers.
Isn't there a file where I can place this that will change my language every time I go to another page?
http://msdn.microsoft.com/en-us/library/system.globalization.cultureinfo.aspx
When a new application thread is started, its current culture and
current UI culture are defined by the current system culture, and not
by the current thread culture.
Isn't this your case?

Cookie is the same from two different browser machine clients

I have two browsers from two different machines hitting a test page. The test page is retrieving a cookie. If one is not there, it creates a cookie.
public string GetUserCookieId()
{
string cookieName = "CookieId3";
HttpCookie userInfoCookies = Request.Cookies[cookieName];
string cookieId = "";
if (userInfoCookies != null)
cookieId = userInfoCookies.Value;
if (string.IsNullOrEmpty(cookieId))
{
cookieId = Guid.NewGuid().ToString();
HttpCookie cookie = new HttpCookie(cookieName);
cookie.Value = cookieId;
cookie.Expires = DateTime.Now.AddDays(90);
Response.SetCookie(cookie);
}
return cookieId;
}
Both browser machines are displaying the same cookie value on the page. This even once I have renamed the cookie to "CookieId3". Please tell me where i am going wrong.
As you can see, the method is not static. Thank you
The issue appears to be the difference between Request and HttpContext.Current.Request when the code is accessed from a ClassLibrary.
Is anyone is able to shed some light on this?!
The following works.
public string GetUserCookieId()
{
string cookieName = "CookieId18";
HttpCookie userInfoCookies = HttpContext.Current.Request.Cookies[cookieName];
string cookieId = "";
if (userInfoCookies != null)
cookieId = userInfoCookies.Value;
if (string.IsNullOrEmpty(cookieId))
{
cookieId = Guid.NewGuid().ToString();
HttpCookie cookie = new HttpCookie(cookieName);
cookie.Value = cookieId;
cookie.Expires = DateTime.Now.AddDays(90);
HttpContext.Current.Response.SetCookie(cookie);
}
return cookieId;
}

Resources