Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-core/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在Asp.net Core 3.1应用程序中处理子域_C#_Asp.net Core_Subdomain_Multi Tenant - Fatal编程技术网

C# 在Asp.net Core 3.1应用程序中处理子域

C# 在Asp.net Core 3.1应用程序中处理子域,c#,asp.net-core,subdomain,multi-tenant,C#,Asp.net Core,Subdomain,Multi Tenant,我正在开发一个asp.NETCore3.1应用程序(MVC),根据要求,每个帐户都应该有自己的子域(例如:mystore.domain.com)和数据。因此,我试图找出如何在路由模式中添加子域部分,并在我的控制器中捕获它,以便获取用户数据,并在视图中返回它 我做了一些研究,找到了asp.net核心版本2的解决方案,不幸的是,它在版本3上不起作用(变化太大了) 摘要: 用户类型:mystore.domain.com或mystore.domain.com\store 我捕获主“mystore”,搜索

我正在开发一个asp.NETCore3.1应用程序(MVC),根据要求,每个帐户都应该有自己的子域(例如:mystore.domain.com)和数据。因此,我试图找出如何在路由模式中添加子域部分,并在我的控制器中捕获它,以便获取用户数据,并在视图中返回它

我做了一些研究,找到了asp.net核心版本2的解决方案,不幸的是,它在版本3上不起作用(变化太大了)

摘要:

  • 用户类型:mystore.domain.com或mystore.domain.com\store
  • 我捕获主“mystore”,搜索数据库中的用户数据,并呈现一个视图
  • 您可以使用操作过滤器,具体来说,它可以:

    • 在调用操作方法之前和之后立即运行代码
    • 可以更改传递到操作中的参数
    • 可以更改操作返回的结果
    • 在Razor页面中不支持
    例如

    public class MySampleActionFilter : IActionFilter 
    {
        public void OnActionExecuting(ActionExecutingContext context)
        {
            // Do something before the action executes.
            MyDebug.Write(MethodBase.GetCurrentMethod(), context.HttpContext.Request.Path);
        }
    
        public void OnActionExecuted(ActionExecutedContext context)
        {
            // Do something after the action executes.
            MyDebug.Write(MethodBase.GetCurrentMethod(), context.HttpContext.Request.Path);
        }
    }
    
    在这里,您可以准备一个作用域服务,基于该服务加载用户,然后在需要该数据的任何服务中重用它

    即使没有过滤器,您也可以简单地创建一个具有作用域生存期的UserService,在那里加载用户并在服务中的任何位置使用它

    在我们的系统中,我们正在做类似的事情:

    用于加载会话数据的服务:

    public class ClientTokenService
    {
        private readonly IHttpContextAccessor _httpContextAccessor;
    
        public ClientTokenService(
            IHttpContextAccessor httpContextAccessor)
        {
            _httpContextAccessor = httpContextAccessor;
        }
    
        public Profile LoadProfile()
        {
            if (_httpContextAccessor.HttpContext.User == null)
            {
                throw new Exception("No user claims found to load Profile");
            }
    
            var user = _httpContextAccessor.HttpContext.User;
    
            var numberType = (NumberType)int.Parse(user.FindFirst("numberType").Value);
            var profileType = (PackagePlan)int.Parse(user.FindFirst("profileType").Value);
            var lineOfBusiness = (LineOfBusiness)int.Parse(user.FindFirst("lineOfBusiness").Value);
    
            // More stuff
    
            // Prepare the profile data
            return new Profile(
                user.FindFirst("number").Value,
                user.FindFirst("contractId").Value,
                numberType,
                profileType,
                user.FindFirst("cc")?.Value,
                user.FindFirst("app").Value,
                user.FindFirst("clickId")?.Value,
                user.FindFirst("wifi") != null,
                lineOfBusiness
            );
        }
    }
    
    此服务可以是暂时的,然后是保存数据的作用域服务

    public class ClientSessionContext
    {
        public Profile Profile { get; }
    
        public ClientSessionContext(
            ClientTokenService sessionService)
        {
            Profile = sessionService.LoadProfile();
        }
    }
    
    将此服务声明为作用域,因此每个请求只初始化一次此类

    副学士

    services.AddScoped<ClientSessionContext>();
    
    services.addScope();
    

    然后,只要将此服务注入到需要访问用户数据的任何位置。

    您有没有找到有效的解决方案?