Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cookies in ASP.Net MVC 5

I am developing an application in which users are SignUp or SignIn by External Identity Providers like AAD, Google, WS-Federated Authentication etc. Now I want to create cookies on a user machine to logged in until user SignOut. Give me some thought and guide me how I can overcome it. thanks in advance.

like image 824
Malik Kashmiri Avatar asked Jun 14 '16 04:06

Malik Kashmiri


People also ask

What is cookies in ASP NET MVC?

Cookies are small files that are created in the web browser's memory (if they're temporary) or on the client's hard drive (if they're permanent). CookiesExample.zip. Cookies are one of the State Management techniques, so that we can store information for later use.

What is cookies in asp net?

A cookie is a small bit of text that accompanies requests and pages as they go between the Web server and browser. The cookie contains information the Web application can read whenever the user visits the site.

What are the two types of cookies in asp net?

There are two type of cookies in Asp.Net: Persistent cookies. Non-persistent cookies.

How do I access cookies in asp net?

You may use Request. Cookies collection to read the cookies. Show activity on this post. HttpContext.


1 Answers

Use Request.Cookies and Response.Cookies to handle your situation. once user coming back from third party authorization create cookie and store it in browser and once user Logout clear the cookie.

 string cookievalue ;
 if ( Request.Cookies["cookie"] != null )
 {
    cookievalue = Request.Cookies["cookie"].Value.ToString();
 }
 else
 {
    Response.Cookies["cookie"].Value = "cookie value";
 }

For removing cookie use following code

if (Request.Cookies["cookie"] != null)
{
    Response.Cookies["cookie"].Expires = DateTime.Now.AddDays(-1);   
}
like image 143
Nikhil.Patel Avatar answered Oct 23 '22 03:10

Nikhil.Patel