lundi 8 avril 2019

.NET Core 2.2 Cookie authentication does not remember user

hello guys i got a problem with Cookie Authentication. When i run the application and log in user with following code everything works fine but when i close my project (to add some code etc) and run it again i have to log in again. I Use cookie authentication and i thought system should remember that cookie and keep me logen after project close. Am i wrong ?

i use asp.core 2.2

                await HttpContext.SignInAsync(
                        CookieAuthenticationDefaults.AuthenticationScheme,
                        principal, new AuthenticationProperties()
                        {
                            AllowRefresh = true,
                            IsPersistent = true,
                            ExpiresUtc = DateTime.UtcNow.AddDays(7)
                        });

this is how i setup it in Startup class

        services.AddAuthentication(options =>
        {
            options.DefaultAuthenticateScheme = CookieAuthenticationDefaults.AuthenticationScheme;
            options.DefaultSignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
            options.DefaultChallengeScheme = CookieAuthenticationDefaults.AuthenticationScheme;
        }).AddCookie(options =>
            {
                options.LoginPath = new PathString("/Account/LogOn");
                options.LogoutPath = new PathString("/Account/LogOut");
                options.Cookie.SecurePolicy = CookieSecurePolicy.SameAsRequest;
                options.SessionStore = new MemoryCacheTicketStore();
            });

Maybe its because this MemoryCacheTickerStore class ? i had to implement that because without it after singing in i had an error that request is too long and adding that fixed it.

public class MemoryCacheTicketStore : ITicketStore
{
    private const string KeyPrefix = "AuthSessionStore-";
    private IMemoryCache _cache;

    public MemoryCacheTicketStore()
    {
        _cache = new MemoryCache(new MemoryCacheOptions());
    }

    public async Task<string> StoreAsync(AuthenticationTicket ticket)
    {
        var guid = Guid.NewGuid();
        var key = KeyPrefix + guid.ToString();
        await RenewAsync(key, ticket);
        return key;
    }

    public Task RenewAsync(string key, AuthenticationTicket ticket)
    {
        var options = new MemoryCacheEntryOptions();
        var expiresUtc = ticket.Properties.ExpiresUtc;
        if (expiresUtc.HasValue)
        {
            options.SetAbsoluteExpiration(expiresUtc.Value);
        }
        options.SetSlidingExpiration(TimeSpan.FromHours(1)); // TODO: configurable.

        _cache.Set(key, ticket, options);

        return Task.FromResult(0);
    }

    public Task<AuthenticationTicket> RetrieveAsync(string key)
    {
        AuthenticationTicket ticket;
        _cache.TryGetValue(key, out ticket);
        return Task.FromResult(ticket);
    }

    public Task RemoveAsync(string key)
    {
        _cache.Remove(key);
        return Task.FromResult(0);
    }
}




Aucun commentaire:

Enregistrer un commentaire