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 : 3859 | Status : [Member] | Replies : 5
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: Jitendrasoft09 on: 7/2/2013 [Member] [MVP] Starter | Points: 25

Up
0
Down
Hi Lakhan,
You can get and set your cookies as below code-

protected void Page_Load(object sender, EventArgs e)

{
if (Request.Browser.Cookies)
{
//supports the cookies
}
else
{
//not supports the cookies
//redirect user on specific page
//for this or show messages
}
}


Refer below link to get more details
http://www.codeproject.com/Articles/244904/Cookies-in-ASP-NET

Jitendra Kumar
If my post helps you, plz mark as an answer.

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

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: Jitendrasoft09 on: 7/4/2013 [Member] [MVP] Starter | Points: 25

Up
0
Down
Hi Lakhan,

As you are right, you can write separate method to add or remove cookies or you can create javascript to handle to add or remove browser cookies.

There is need to add code for adding and removing cookies, in that place you can change cookies settings.

protected void Page_Load(object sender, EventArgs e)

{
if (Request.Browser.Cookies)
{
//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
}
}




Jitendra Kumar
If my post helps you, plz mark as an answer.

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

Posted by: Jitendrasoft09 on: 7/13/2013 [Member] [MVP] Starter | Points: 25

Up
0
Down
Hi Lakhan,

As above code is just a sample, need to further coding. There is no need to set true or false manually there. As you are right If condition says browser support cookies or not. If you do not want delete that condition. You can make true or false for support cookies or not with below code-

HttpCookie c = new HttpCookie("SupportCookies", "true");


Please refer below link to understand cookies process-
http://bytes.com/topic/asp-net/answers/836320-how-check-cookie-enabled-browser-not

Jitendra Kumar
If my post helps you, plz mark as an answer.

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

Login to post response