find and solve || findandsolve.com
Please wait.....

Welcome back.






Before Read Terms of use

How does the MVC Application get the Cookies from the Client


Definition of Cookie


Cookie is A small text file grater then or up to 4kb created by a website with different progarammint language that is stored in the user's computer either temporarily or or permanently.Cookies provide a way for the website to recognize you and keep track of your preferences.Cookies are messages that web servers pass to your web browser when you visit website.Our browser stores each temporary message in a small file, called cookie.txt.A website might also use cookies to keep a record of our most recent visit or visited to record our login information.

Who Can Access Cookies?

When a cookie is created then possible to control its visibility by setting its 'root domain'.Cookie will then be accessible to any URL belonging to that root. For Eg. your can find  the root could be set to "findandsolve.com" and the cookie would then be available to sites in "www.findandsolve.com" or "abc.findandsolve.com" or "findandsolve.com". This might be used to allow related pages to each page communication with each other. It is not possible to set the root domain to 'top level' domains such as '.com' or '.co.uk' since this would allow widespread access to the cookie..Cookies are mostly  used to track website activity.when you visit some website or web application, the server gives you a cookie that acts as your identification card.Cookies are also used for online shopping.For default cookies are available  to all paths in their domains.


What are Tracking Cookies?

There are some  commercial and advertising  websites are include embedded ads material which is served from a third party sites.adverts possible to store a cookie for that third-party sites and containing information fed to it from the containing site and information might include the as a name of the site, particular products being viewed, pages visited, etc. When the user are  later visits another site containing as s  similar to embedded advert from the same third-party site, The advertiser able to read the information from cookie and use some  information by user's  browsing history. This enables publishers to serve adverts targeted at a user's interests, so in theory having a greater chance of being relevant to the user. 


ASP.NET MVC Add Cookies

                   var loginCookie = new HttpCookie("UserCookies");
                    loginCookie.Values.Add("UserId",120);
                    loginCookie.Values.Add("UserName","Admin");
                    loginCookie.Values.Add("Gender", "Male");
                    loginCookie.Values.Add("FullName", "Devin Codefi");
                    loginCookie.Expires = DateTime.Now.AddHours(1);
                    Response.Cookies.Add(loginCookie);

ASP.NET MVC Retrieve Cookies

First create UserInfo View model like given below

public class UserInfoViewModel
    {
        public int UserId { get; set; }
        public string UserName{ get; set; }
        public string Gander { get; set; }
        public string FullName{ get; set; }
    }


  //Get Cookies record by create function give below
 public static UserInfoViewModel GetUserDetails()
        {
            HttpCookie cookie =  HttpContext.Current.Request.Cookies["UserCookies"];
            UserInfoViewModel userInfo = new UserInfoViewModel();
            if (cookie != null)
            {             
                userInfo.UserId = Convert.ToInt32(cookie["UserId"].ToString());
                userInfo.UserName = cookie["Username"].ToString();
                userInfo.Gender = cookie["Gender"].ToString();
                userInfo.FullName = cookie["FullName"].ToString();
            }
            return userInfo;           
        }


Security concerns


There is a lot of concern about privacy and security on the internet.Cookies do not in themselves present a threat to privacy, since they can only be used to store information that the user has volunteered or that the web server already has.Only one the website that  can creates a cookie, therefore other servers can not access to your information.Additionally, web servers can use only information that you provide or choices that we make while visiting the website as content in cookies.Webmasters have always been able to track access to their sites.Accepting a cookie does not give a server access to your computer or any of your personal information (except for any information that you may have purposely given, as with online shopping).

Sundar  Neupane

Sundar Neupane

I like working on projects with a team that cares about creating beautiful and usable interfaces.

Comments



Report Response