How to enable browser setting to get or set cookie using JavaScript or C# code?

Posted by lakhansin-22735 under ASP.NET on 7/1/2013 | Points: 10 | Views : 4615 | Status : [Member] | Replies : 2
Hi Everyone,

Here, I have required JavaScript or server side code by which we can enable browser settings to get or set cookies.

I have JS code by which we can identify that browser supports cookie or not? but I want how to enable this browser setting using JS or C#?
<script type="text/javascript">
var cookieEnabled=(navigator.cookieEnabled)? true : false
//if not IE4+ nor NS6+
if (typeof navigator.cookieEnabled=="undefined" && !cookieEnabled){
document.cookie="testcookie"
cookieEnabled=(document.cookie.indexOf("testcookie")!=-1)? true : false
}
//if (cookieEnabled) //if cookies are enabled on client's browser
//do whatever
</script>


Thanks in Advance

Lakhan Singh
Tech Lead
BeyondKey System Pvt. Ltd.
Indore, M.P.
India



Responses

Posted by: lakhansin-22735 on: 7/4/2013 [Member] Starter | Points: 25

Up
0
Down
Hi Jitendra,

Thanks for the response.

But, as per my question "I want how to enable this browser setting for cookie using JS or C#?"
and the code you have provided is only check whether the coookie is enabled or not.
Request.Browser.Cookies

Above, statement only return true or false and yes, it is readonly property.

Thanks,



Lakhan Singh
Tech Lead
BeyondKey System Pvt. Ltd.
Indore, M.P.
India

lakhansin-22735, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: lakhansin-22735 on: 7/13/2013 [Member] Starter | Points: 25

Up
0
Down
Hi Jitendra,

Thanks for the reply.

But the code you have provided is ok, please see the below code snippet with my question

protected void Page_Load(object sender, EventArgs e)

{
if (Request.Browser.Cookies) /*Lakhan --> It return True or False right, but How can I manually set True or False that is my Question*/
{
//supports the cookies
string cookieName = "loginidcookie";
if (Request.Cookies[cookieName ] != null)
{
var myCookie = new HttpCookie(cookieName);
myCookie.Expires = DateTime.Now.AddDays(-1d);
Response.Cookies.Add(myCookie);
}
else
{
Response.Cookies.Remove("cookieName");
}
}
else
{
//not supports the cookies
//redirect user on specific page
//for this or show messages
}
}


Thanks,

Lakhan Singh
Tech Lead
BeyondKey System Pvt. Ltd.
Indore, M.P.
India

lakhansin-22735, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response