python ruby-programming-language Smalltalk Vue.js Knockoutjs TypeScript JavaScript, OOPs JavaScript Angular 8,7,6,5,4 and 2 Interview Questions and Answers Angular 8,7,6,5,4 and 2 Interview Questions and Answers Angular 4 Angular 2 AngularJs 1.x NodeJs Perl Programming R Programming RequireJs Rust Programming Backbonejs closure programming language go-programming-language kotlin-programming-language
ASP.NET MVC Cookie Implementation

How to create cookies in MVC 5 with C# .net?

We are using Request.Cookies for get the values of cookies and the Respone.Cookies are use to add the cookies.

In the below example, used to Respone.Cookies for create/ add new cookies. i.e.


01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
//This method id used to Create the cookies and assign the values in cookies.
private static void SetCookies(UserSession userSession)
{
    HttpCookie cookie = HttpContext.Current.Request.Cookies["GlobalCookieName"] ??
    new HttpCookie("GlobalCookieName ");
    cookie.Values["CompanyID"] = Convert.ToString(userSession.CompanyID);
    cookie.Values["TenantId"] = userSession.CompanyName;
    cookie.Values["EmailID"] = userSession.EmailID;
    cookie.Values["UserType"] = Convert.ToString(userSession.UserType);
    cookie.Expires = DateTime.Now.AddDays(365);
    HttpContext.Current.Response.Cookies.Add(cookie);
}
//This method id used to Create the cookies and assign the values in cookies.

private static void SetCookies(UserSession userSession)
{
    HttpCookie cookie = HttpContext.Current.Request.Cookies["GlobalCookieName"] ??
    new HttpCookie("GlobalCookieName ");

    cookie.Values["CompanyID"] = Convert.ToString(userSession.CompanyID);
    cookie.Values["TenantId"] = userSession.CompanyName;
    cookie.Values["EmailID"] = userSession.EmailID;
    cookie.Values["UserType"] = Convert.ToString(userSession.UserType);

    cookie.Expires = DateTime.Now.AddDays(365);
    HttpContext.Current.Response.Cookies.Add(cookie);
}
1
2
3
4
5
6
7
8
9
//This is the property class.
public class UserSession
{
     public string EmailID { get; set; }
     public int TenantID { get; set; }
     public int CompanyID { get; set; }
     public string UserType { get; set; }
}
//This is the property class.

public class UserSession
{
     public string EmailID { get; set; }
     public int TenantID { get; set; }
     public int CompanyID { get; set; }
     public string UserType { get; set; }
}
ANIL SINGH

Hi there! Welcome to my blog. My name is Anil Singh. I'm a author of this blog. For more detail about me, kindly refer to this link..
My Blogs - http://code-sample.com and http://code-sample.xyz
My Books - Interview Questions and Answers Books- Get Your Book in 15+ Digital Stores Worldwide..

You Might Also Like
www.code-sample.com/. Powered by Blogger.
ASK Questions