在 ASP.NET Core 3.1 中创建会话 [英] Creating a session in ASP.NET Core 3.1

查看:28
本文介绍了在 ASP.NET Core 3.1 中创建会话的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是 ASP.NET Core 3.1.我需要在控制器的操作中设置会话.我在 ConfigureServices() 方法中添加了会话:

I am using the ASP.NET Core 3.1. I need to set a session inside a action in a controller. I have Added the session in the ConfigureServices() method as:

services.AddSession(options => {
                options.IdleTimeout = System.TimeSpan.FromSeconds(3600);
            });

另外,我在 Configure() 方法中添加了使用 session 的代码:

Also I have add code to use session in the Configure() method as:

app.UseSession();

在控制器类中我试图创建一个会话

In the controller class I am trying to create a session

HttpContext.Session.Set("sessionuser",xxx);

所需的 xxx 是一个字节[],与 .NET CORE 2.2 中 SetString() 所需的字符串不同如何为此创建字节 []?在创建会话方面还有其他更好的选择吗?

The xxx required is a byte[] unlike the string required in SetString() in .NET CORE 2.2 How to create the byte[] for this? Are there other better options in create sessions ?

推荐答案

GetString/SetString 仍然存在于 .NET Core 3.1 中.它们是扩展(一如既往)并位于 Microsoft.AspNetCore.Http 命名空间中.也许你只需要添加一个 using ?

GetString/SetString still exist in .NET Core 3.1. They're extensions (as they always have been) and live in the Microsoft.AspNetCore.Http namespace. Perhaps you just need to add a using?

您没有在此处提供大量有关您的情况的信息,但您可能正在从类库中执行此操作.在这种情况下,事情变得有点棘手.ASP.NET Core 3.X 转向对大部分 ASP.NET Core 代码使用框架引用(而不是对 Microsoft.AspNetCore.App 元包的包引用.但是,该框架引用只能在面向 netcoreapp3.0 或更高版本时使用.如果您面向 .NET Standard,则只能引用旧的 2.X 包.

You haven't given a ton of info about your situation here, but it's possible you might be doing this from a class library. In which case, things become a bit trickier. ASP.NET Core 3.X moved to using a framework reference for the bulk of the ASP.NET Core code (instead of a package reference to the Microsoft.AspNetCore.App metapackage. However, that framework reference can only be utilized when targeting netcoreapp3.0 or above. If you're targeting .NET Standard, you can only reference the old 2.X packages.

如果您只支持 ASP.NET Core 3.X 应用程序,那么您只需将类库的目标更改为 netcoreapp3.1 并添加框架引用.如果您还需要支持 ASP.NET Core 2.X 应用程序,那么您必须将您的类库同时针对 netcoreapp3.1netstandard2.0,并在您的项目文件中使用条件来根据目标添加框架引用或单独的 2.X 包.Andrew Lock 有一个很好的指南和更多信息:https://andrewlock.net/converting-a-netstandard-2-library-to-netcore-3/

If you are only supporting ASP.NET Core 3.X apps, then you should simply change the class library's target to netcoreapp3.1 and add the framework reference. If you need to also support ASP.NET Core 2.X apps, then you'll have to multi-target your class library to both netcoreapp3.1 and netstandard2.0, and use conditionals in your project file to either add the framework reference or individual 2.X packages depending on the target. Andrew Lock has an excellent guide and more information at: https://andrewlock.net/converting-a-netstandard-2-library-to-netcore-3/

还值得一提的是,System.Text.Json 支持直接与 byte[] 进行序列化.因此,如果您已经在使用或打算使用 ISession 的泛型类型扩展来支持比简单字符串更复杂的类型或可以强制转换为字符串的值类型,那么您可以只依赖相反:

It's also worth mentioning that System.Text.Json supports serializing directly to/from byte[]. As such, if you're already using or intend to use generic type extensions for ISession to support types more complex than simple strings or value types that can be coerced to a string, then you can just rely on that instead:

public static class SessionExtensions
{
    public static void Set<T>(this ISession session, string key, T value)
    {
        session.Set(key, JsonSerializer.SerializeToUtf8Bytes(value));
    }

    public static T Get<T>(this ISession session, string key)
    {
        var value = session.Get(key);

        return value == null ? default(T) : 
            JsonSerializer.Deserialize<T>(value);
    }
}

这篇关于在 ASP.NET Core 3.1 中创建会话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆