Is it possible to persist cookies between visual s

2020-03-03 05:31发布

I have an authentication cookie that gets set after I hit a login screen in my local environment. That cookie has been set to be persistent and has been given a timeout period of 7 days in the future.

enter image description here

When I end my debug session and start debugging after another build the cookie is not present. This happens for every browser. Is there a way to get Visual Studio to remember the persistent cookie after a debug session completes?

3条回答
来,给爷笑一个
2楼-- · 2020-03-03 05:48

Assuming you are using VS and ASPNet 4.5 or core 1.0/2.0 under IIS, check your debug output on start up and you might see :

“Neither user profile nor HKLM registry available. Using an ephemeral key repository. Protected data will be unavailable when application exits.”

This is caused by the DataProtection keys used by IIS. Follow this short blog post to resolve

查看更多
我想做一个坏孩纸
3楼-- · 2020-03-03 05:56

Let’s have a quick look how to make cookies as persistent

    //Creting a Cookie Object
 HttpCookie _userInfoCookies = new HttpCookie("UserInfo");

//Setting values inside it
 _userInfoCookies["UserName"] = "Abhijit";
 _userInfoCookies["UserColor"] = "Red";
 _userInfoCookies["Expire"] = "5 Days";

 //Adding Expire Time of cookies
  _userInfoCookies.Expires = DateTime.Now.AddDays(5);

 //Adding cookies to current web response
 Response.Cookies.Add(_userInfoCookies);

Now once you have set with the Cookies expires time , it will be stored in hard drive until expires or user manually delete or clear all the cookies. If you want your cookies need to be expires before the expiration time that you have mentioned earlier, you just need to override the cookies information.

HttpCookie _userInfoCookies = new HttpCookie("UserInfo");
   //Adding Expire Time of cookies before existing cookies time
   _userInfoCookies.Expires = DateTime.Now.AddDays(-1);
   //Adding cookies to current web response
   Response.Cookies.Add(_userInfoCookies);

So Just Work on Expiration. and take a look at This

查看更多
对你真心纯属浪费
4楼-- · 2020-03-03 06:05

The solution I found was to make it so that new instances of .NET Core MVC would not open up in a brand new window, but an existing one. I changed one setting

1)Tools menu 2)Options... 3)Debugging > General 4)Uncheck "Enable JavaScript debugging for ASP.NET"

And when I run the app with F5 an instance fires up in an existing instance of chrome and I can reuse the cookies that are already in existence. With that box checked it always opens into a new instance of chrome and cookies are not present.

查看更多
登录 后发表回答