nemi-chand / cookiemanager Goto Github PK
View Code? Open in Web Editor NEWCookieManager is an ASP.NET Core Abstraction layer on top of Cookie
License: Apache License 2.0
CookieManager is an ASP.NET Core Abstraction layer on top of Cookie
License: Apache License 2.0
Suggest changing the csproj targetframework from netcoreapp1.0
to netstandard1.6
. I can do a PR if you want but it is a pretty minor change.
Hi,
I'm using this package in a net6 code and was getting the error "IFeatureCollection has been disposed. Object name: 'Collection'."
After some investigation, I found that the issue was occurring in the HttpCookie class. The problem was that the _httpContext private field was storing the _httpAccessor.HttpContext is in the construction of the class, but the HttpContext is only valid during the request lifetime, so we can't cache it in a private field.
To fix that, all that I did was change the _httpContext to be an arrow function with will always get the HttpContext from _httpAccessor.HttpContext (private HttpContext _httpContext => _httpAccessor.HttpContext)
` public class HttpCookie : ICookie
{
private readonly IDataProtector _dataProtector;
private static readonly string Purpose = "CookieManager.Token.v1";
private readonly CookieManagerOptions _cookieManagerOptions;
private readonly ChunkingHttpCookie _chunkingHttpCookie;
private IHttpContextAccessor _httpAccessor;
/// <summary>
/// An HttpContext is the context of a single request. It provides access to the request, response properties etc of that single request. You can't cache it, it becomes invalid once that request ends.
/// See more: https://stackoverflow.com/questions/59963383/session-setstring-method-throws-exception-ifeaturecollection-has-been-disposed
/// </summary>
private HttpContext _httpContext => _httpAccessor.HttpContext;
/// <summary>
/// External depedenacy of <see cref="IHttpContextAccessor" />
/// </summary>
/// <param name="httpAccessor">IHttpAccessor</param>
/// <param name="dataProtectionProvider">data protection provider</param>
/// <param name="optionAccessor">cookie manager option accessor</param>
public HttpCookie(IHttpContextAccessor httpAccessor,
IDataProtectionProvider dataProtectionProvider,
IOptions<CookieManagerOptions> optionAccessor)
{
_httpAccessor = httpAccessor;
//_httpContext = httpAccessor.HttpContext; //NOT NEEDED ANYMORE
_dataProtector = dataProtectionProvider.CreateProtector(Purpose);
_cookieManagerOptions = optionAccessor.Value;
_chunkingHttpCookie = new ChunkingHttpCookie(optionAccessor);
}`
Now it is working very well.
It would be good to add this fix to this package.
Thanks
I've tried operations one-to-one,
_cookie.Set ("Key", "value here", 10);
string keystr = _cookie.Get ("Key"); // result empty is coming.
with the order of transactions,
nuget package download,
Including Icookiemanager and Icookie from the project
then middleware
services.addcookiemanag is ();
I added the code.
but the value is empty.
Hello,
I am getting this exception: Method not found: 'Void Microsoft.Net.Http.Headers.SetCookieHeaderValue..ctor(System.String)'. anytime I try to set a cookie.
Code
cookie.Remove(cookiekey); cookie.Set(cookiekey, langKey, new CookieOptions() { HttpOnly = true, Expires = DateTime.Now.AddDays(30) });
Platform used: .net core 2.0
Hello,
When getting a cookie containing a complex object, an exception is thrown :
Newtonsoft.Json.JsonReaderException: Unexpected character encountered while parsing value: Q. Path '', line 0, position 0. at Newtonsoft.Json.JsonTextReader.ParseValue() at Newtonsoft.Json.JsonReader.ReadForType(JsonContract contract, Boolean hasConverter) at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize(JsonReader reader, Type objectType, Boolean checkAdditionalContent) at Newtonsoft.Json.JsonSerializer.DeserializeInternal(JsonReader reader, Type objectType) at Newtonsoft.Json.JsonConvert.DeserializeObject(String value, Type type, JsonSerializerSettings settings) at Newtonsoft.Json.JsonConvert.DeserializeObject[T](String value, JsonSerializerSettings settings) at CookieManager.DefaultCookieManager.GetExisting[T](String key) at SecretariatMedical.Controllers.LoginController.Index(LoginViewModel model, Nullable
1 OuvreNouvelleConnexion) in C:\Users\SylvainB\Desktop\aspnetcore\Projet Clinique Charles\SecretariatMedical\SecretariatMedical\Controllers\LoginController.cs:line 164
at Microsoft.AspNetCore.Mvc.Internal.ActionMethodExecutor.TaskOfIActionResultExecutor.Execute(IActionResultTypeMapper mapper, ObjectMethodExecutor executor, Object controller, Object[] arguments)
at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.InvokeActionMethodAsync()
at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.InvokeNextActionFilterAsync()
at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Rethrow(ActionExecutedContext context)
at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.InvokeInnerFilterAsync()
at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeNextResourceFilter()
at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Rethrow(ResourceExecutedContext context)
at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeFilterPipelineAsync()
at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeAsync()
at Microsoft.AspNetCore.Routing.EndpointMiddleware.Invoke(HttpContext httpContext)
at Microsoft.AspNetCore.Routing.EndpointRoutingMiddleware.Invoke(HttpContext httpContext)
at Microsoft.AspNetCore.Session.SessionMiddleware.Invoke(HttpContext context)
at Microsoft.AspNetCore.Session.SessionMiddleware.Invoke(HttpContext context)
at Microsoft.AspNetCore.Cors.Infrastructure.CorsMiddleware.InvokeCore(HttpContext context)
at Microsoft.AspNetCore.Builder.RouterMiddleware.Invoke(HttpContext httpContext)
at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context)
at Microsoft.AspNetCore.StaticFiles.StaticFileMiddleware.Invoke(HttpContext context)
at Microsoft.AspNetCore.StaticFiles.StaticFileMiddleware.Invoke(HttpContext context)
at Microsoft.AspNetCore.StaticFiles.StaticFileMiddleware.Invoke(HttpContext context)
at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)`
Here is the line where the problem happens :
Dictionary<string, Dictionary<string, ClientSiteIdentifier>> alreadyExistingList = _cookieManager.Get<Dictionary<string, Dictionary<string, ClientSiteIdentifier>>>("ListeConnexionsMultiSiteParUtilisateur");
Here is the line where I store the object in the cookie :
_cookieManager.Set("ListeConnexionsMultiSiteParUtilisateur", alreadyExistingList, new CookieOptions() { HttpOnly = true, Expires = DateTime.Now.AddDays(30) });
Do you have any idea what could cause this problem ?
Thanks for your help !
Sylvain
Hi
I cannot seem to get the cookie expires to accept my value, I have tried all three ways:
startup DefaultExpireTimeInDays
controller Set with new CookieOptions()
controller Set with minutes
But it always defaults to 6 hours and 5 minutes no matter what, is this a bug or am I missing something?
Thanks
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.