如何在.net core 2.0类库项目中使用会话 [英] How to use sessions in a .net core 2.0 class library project

查看:332
本文介绍了如何在.net core 2.0类库项目中使用会话的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一个.net框架(4.5.1)asp.net网站,该网站对用户进行身份验证并将身份验证详细信息存储在.net框架类库中.当在目标asp.net .net框架(4.5.x)网站中引用该类库时,可以访问原始网站传递的所有变量.

We have a .net framework (4.5.1) asp.net website which authenticates users and and stores authentication details in a .net framework class library. This class library when referenced in a the target asp.net .net framework (4.5.x) website can access all the variables which were passed by the originating web site.

我们想要将目标应用程序更改为.net core 2.0 mvc.我正在尝试做的是编写一个.net核心(2.0)类库,该库将在目标应用程序中被引用,并使用源应用程序传递的会话变量数据(仍将保留在.net Framework 4.5.1中)

We want to change the target applications to be in .net core 2.0 mvc. What I am trying to do is write a .net core (2.0) class library which will be referenced in the target app and consume the session variables data passed on by the originating app (which will still stay in .net framework 4.5.1)

原始类库使用HttpContext.Current.Session,我现在发现它在.net core 2.0中未使用.我要做的就是让.net 4.5 Web应用程序将会话传递到.net core 2.0类库,并将其分配给将由.net core 2.0 mvc应用程序访问的属性.

The original class library uses HttpContext.Current.Session which I now find is not used in .net core 2.0. All i want to do is have a .net 4.5 web app pass a session to a .net core 2.0 class library and assign it to a property which will be accessed by a .net core 2.0 mvc app.

在下面的代码中,始终存在null异常.我该如何实现我想做的事情,还是应该只使用.net framework mvc项目,因为一切都很好就可以了.简直不敢相信像HttpContext.Current.Session这样普遍的东西,真是一场噩梦.

In the code below there is always a null exception. How do i acheive what i want to do or should i just use a .net framework mvc project as everything worjks fine in that. Cannot believe something as common as HttpContext.Current.Session is turning out to be such a nightmare.

在此先感谢您的帮助.

Thanks in advance for any help on this.

`

using System;
using System.Web;
using System.Threading;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.Features;
using System.Text;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.AspNetCore.Builder;

namespace CLStandardCSharp1
{
    //this is a .net core 2.0 class library project 
    public class HttpStateDataFactory
    {
        static byte[] toBytes = Encoding.ASCII.GetBytes("Some test value");
        static byte[] outToBytes;

        private static IHttpContextAccessor _httpContextAccessor;
        private static ISession _sessionContext => _httpContextAccessor.HttpContext.Session;
        public HttpStateDataFactory(IHttpContextAccessor httpContextAccessor)
        {
           _httpContextAccessor = httpContextAccessor;
        }

        public static void SetToSession()
        {
            _sessionContext.Set("SessionName", toBytes);
        }

        public static string GetFromSession()
        {
            string data = _sessionContext.TryGetValue("SessionName", out outToBytes).ToString();
            return data;
        }

        public static string SessionNameValueForClient = "N/A";
        public static string SomeProperty
        {
            get
            {
                return SessionNameValueForClient= GetFromSession();
            }
            set
            {
                SessionNameValueForClient = value;
            }
        }
    }
}

`

推荐答案

在项目中的任何地方使用HttpContex并从HttpContex中删除函数:

Use HttpContex and deleted functions from HttpContex anywhere in the project:

using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.Features;

namespace MVC_CoreWebApp.Models
{
    public static class HttpContext2
    {
        public static HttpContext Current => new HttpContextAccessor().HttpContext;

        public static string UserHostAddress => Current.Features.Get<IHttpConnectionFeature>().RemoteIpAddress.ToString();
        public static string UserAgent => Current.Request.Headers["User-Agent"].ToString();
    }
}

在ASP.net core 2.1上成功测试

Successful testing on ASP.net core 2.1

这篇关于如何在.net core 2.0类库项目中使用会话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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