如何在_Layout中访问会话而不必担心它已过期



我正在使用 ASP.NET Core & Identity 3。

登录时,我读取了当前选择用户的 UI 模板,并在_Layout.cshml文件中基于此模板加载css

用户可以更改他的主题,我通过控制器将其存储在会话变量中

public IActionResult ChangeTheme(int id, string returnUrl)
{
    HttpContext.Session.SetInt32("Template", (id));
    return Redirect(returnUrl);
}

我没有在每次加载cshtml时查询数据库,而是将模板放在一个会话变量中,在我的Layout.cshtml中,我根据模板渲染不同的 css

        switch (template)
        {
            case (int)TemplateEnum.Template2:
                <text>
                <link rel="stylesheet" href="~/css/template1.css" />
                </text>
                break;
            case (int)TemplateEnum.Template2:
                <text>
                <link rel="stylesheet" href="~/css/template2.css" />
                </text>
                break;
        {

我想知道如果会话过期会发生什么。

  1. 考虑到我访问我_Layout.cshtml的值,如果它变为 null 并立即从数据库加载它,则无论如何都可以捕获它,然后再呈现新页面。

  2. 由于我使用标识 3,声明可能是更好的选择吗?我以前没有用过它。我上面的例子的代码是什么

  3. 另一个更适合我的方案的选项?

我不是在每次加载 cshtml 时查询数据库,而是将模板放在会话变量中,在我的 Layout.cshtml 中,我根据模板呈现不同的 css

如果您

唯一关心的是命中数据库,并且您已经抽象了存储库(或用户存储,如果您将其存储在身份类型上),则可以使用装饰器模式来实现本地缓存。

public interface IUserRepository
{
    string GetUserTheme(int userId);
    void SetUserTheme(int userId, string theme);
}
public class CachedUserRepository : IUserRepository
{
    private readonly IMemoryCache cache;
    private readonly IUserRepository userRepository;
    // Cache Expire duration
    private static TimeSpan CacheDuration = TimeSpan.FromMinutes(5);
    public CachedUserRepository(IUserRepository userRepository, IMemoryCache memoryCache)
    {
        if (userRepository == null)
            throw new ArgumentNullException(nameof(userRepository));
        if (memoryCache == null)
            throw new ArgumentNullException(nameof(memoryCache));
        this.userRepository = userRepository;
        this.cache = memoryCache;
    }
    public string GetUserTheme(int userId)
    {
        string theme;
        // adding a prefix to make the key unique
        if (cache.TryGetValue($"usertheme-{userId}", out theme))
        {
            // found in cache
            return theme;
        };
        // fetch from database
        theme = userRepository.GetUserTheme(userId);
        // put it into the cache, expires in 5 minutes
        cache.Set($"usertheme-{userId}", theme, new MemoryCacheEntryOptions { AbsoluteExpirationRelativeToNow = CacheDuration });
        return theme;
    }
    public void SetUserTheme(int userId, string theme)
    {
        // persist it
        userRepository.SetUserTheme(userId, theme);

        // put it into the cache, expires in 5 minutes
        cache.Set($"usertheme-{userId}", theme, new MemoryCacheEntryOptions { AbsoluteExpirationRelativeToNow = CacheDuration });
    }
}

问题是,默认的 ASP.NET Core DI系统中没有对装饰器的内置支持。您必须使用第三方IoC容器(Autofac,StructureMap等)。

您当然可以像这样注册

    services.AddScoped<IUserRepository>(container => {
        return new CachedUserRepository(container.GetService<UserRepository>(), container.GetServices<IMemoryCache>());
    });

但这有点麻烦。否则将其存储在长期存在的cookie中,它的优点是当用户未登录时主题仍将处于活动状态,并且您可以在用户登录时设置cookie。

如果您愿意,您当然可以将主题存储在用户的身份中,但是每当更新主题时,您都必须将用户重新登录......

你可以做这样的事情:

userManager.AddClaimAsync(user, new Claim("Template", id+""));
signInManager.SignInAsync(user);

最新更新