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.
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.
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.
There are two type of cookies in Asp.Net: Persistent cookies. Non-persistent cookies.
You may use Request. Cookies collection to read the cookies. Show activity on this post. HttpContext.
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);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With