How do you simply implement ITicketStore?

  • Thread starter Thread starter j4ck50nD
  • Start date Start date
J

j4ck50nD

Guest
I am having problems with **ITicketStore**. My original problem was this. Because I am using Asp.Net.Identity role/claims, however, when the user is stored the cookie information is too big and gives me an error `HTTP Error 400. The size of the request headers is too long.`. When you try to log in again and once logged in you get the same problem.

Previously (before Identity) I used to roll-my-own security. It looked a lot like Identity with Annotations etc. But the only thing passed around was a cookie which only contained your encrypted user Id. I had action filters that would manage this cookie and retrieve your permissions (roles/claims) and decide what you could and couldn't access. It also supported single logon and remote log out.

Looking around it was suggested I use ITicketStore (looks like others had the same problem). Knowing nothing about `ITicketStore` I did some investigation, but I couldn't find any tutorials about this and all the implementations I could find (here and elsewhere) would not run as in gave me 'red wavy' lines that would not resolve or run-time errors.

Eventually, I managed to cobble together a working version (i.e. no red wavies and no run-time errors), but now the whole thing doesn't work properly. When you close the browser the session is lost, e.g. you have to log in again and the user picks up no roles when you use `.IsInRole` or when you try


public async Task<List<string>> GetUserRoles(int id)
{
var user = await _userManager.FindByIdAsync(id.ToString());

var roles = await _userManager.GetRolesAsync(user);

return roles.ToList();
}
I'm obviously doing something wrong but I have no idea what. I'll continue to investigate, but I have a vain hope that the community here can help me out. Like I say all the implementations I found here and elsewhere don't work for one reason or another and I can't find any basic information to help me understand (er) the basics.

I'm using VS2019 and Asp.Net.Core 2.2

In my Startup.cs, I have this


services.ConfigureApplicationCookie(options =>
{
options.ExpireTimeSpan = TimeSpan.FromDays(14);
options.SlidingExpiration = true;
options.SessionStore = services.BuildServiceProvider().GetService<ITicketStore>();
});




I have an ITicketStore.cs class

using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.Extensions.Caching.Memory;
using System;
using System.Threading.Tasks;

namespace OnsidacaWeb.Models
{
public class TicketStore : ITicketStore
{
private IMemoryCache _cache;
private const string KeyPrefix = "AuthSessionStore-";

public TicketStore(IMemoryCache cache)
{
_cache = cache;
}

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

public Task RenewAsync(string key, AuthenticationTicket ticket)
{
var options = new MemoryCacheEntryOptions
{
Priority = CacheItemPriority.NeverRemove
};
var expiresUtc = ticket.Properties.ExpiresUtc;

if (expiresUtc.HasValue)
{
options.SetAbsoluteExpiration(expiresUtc.Value);
}

options.SetSlidingExpiration(TimeSpan.FromMinutes(60));

_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 async Task<string> StoreAsync(AuthenticationTicket ticket)
{
var key = KeyPrefix + Guid.NewGuid();
await RenewAsync(key, ticket);
return key;
}
}
}

Continue reading...
 
Back
Top