一个人如何在abp框架中使用自定义域名 [英] How would one use a custom domain name with abp framework

查看:113
本文介绍了一个人如何在abp框架中使用自定义域名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我确定这并不是什么新鲜事物,但是也许我不知道要输入google的正确方法...本质上,我的目标是使用abp.io创建具有以下要求的多租户应用程序.

I'm sure this is nothing new, but perhaps I do not know the right thing to type into google... essentially my goal is to create a multi tenant application using abp.io with the following requirement.

  1. 新用户注册为租户.
  2. 然后,用户将其域名dns记录指向我的应用程序.
  3. 用户输入要用于其租户的自定义域.
  4. 用户登录名和页面将在其所选域上解析.

例如,我的应用程序名称托管在winemaker.com上.酿酒的人想要使用我的应用程序并使用JoesFootWine的租户名称(winemaker.com/JoesFootWine)进行注册...但是Joe已经拥有域名JoesFootWine.com,并希望将其用于此应用程序.我需要为Joe设置一种轻松设置租户的方法,以便在JoesFootWine.com上解决winemaker.com/JoesFootWine.

For example, my applications name is hosted on winemaker.com. Someone who makes wine wants to use my application and signs up with the tenant name of JoesFootWine (winemaker.com/JoesFootWine)... But Joe already owns the domain JoesFootWine.com and would like to use that for this application. I need to set up a way for Joe to easily set up their tenant such that winemaker.com/JoesFootWine is resolved on JoesFootWine.com.

这将是一个额外的好处,但不是硬性要求,即要在JoesFootWine.com/{login-route}上设置登录页面,并且租户已经设置且不可更改.

It would be an added bonus but not a hard requirement that a login page would be set up on JoesFootWine.com/{login-route} and the tenant is already set and not changeable.

推荐答案

此处最棘手的事情是根据域解析租户.我之前已经使用ABP做到了这一点,但无法记住所有详细信息,但是您可以使用ITenantResolveContributor界面创建自己的自定义租户解析器.然后可以使用您的解析器,而不使用默认解析器.

The trickiest thing to do here will be resolving the tenant based on the domain. I've done this before with ABP but cannot remember all of the details but you can create your own custom Tenant resolver using the ITenantResolveContributor interface. Then your resolver can be used instead of the default one.

此处的文档 https://docs.abp.io/zh/abp/latest/Multi-Tenancy

在自定义租户解析器中,您需要获取当前正在使用的域,然后使用该域在数据库中查找租户.如果您想允许租户拥有多个域名等,则可以将它们存储在单独的表中,或者它可能只是租户表中的一列.与MVC形式或其他形式相比,从内存中从Aspnet核心中的api调用获取域有点棘手.

In your custom tenant resolver you need to get the domain currently being used and then use that domain to look up the tenant in the database. You might store those in a seperate table in-case you want to allow the tenant to have multiple domain names etc or it could just be a column in the Tenant table. From memory it's a bit tricky to get the domain from an api call in Aspnet core compared to MVC forms or something.

如果是api调用,则需要解决租户问题,然后,我认为您应该能够从引荐来源网址获取域名

If it's an api call you need to resolve the tenant for then I think you should be able to get the domain from the referrer

实际上我只是挖了这个.我认为最可能有更好的方法来完成此操作,但希望它可以帮助您入门.

Actually I just dug up this. I think there is most likely a better way to do this but hope it might help get you started.

        //first try the referrer to see if we can resolve the tenant
        //this will be the referrer from the client calling the api
        var referer = httpContext.Request.Headers.Any(x => x.Key.Equals("referer", StringComparison.InvariantCultureIgnoreCase)) ? httpContext.Request.Headers["Referer"].ToString() : "";
        if (!string.IsNullOrEmpty(referer))
        {
            Uri myUri = new Uri(referer);
            referer = myUri.Host;
        }

        //if no referrer then resort to the host
        //currently this will not change based on tenant
        //but could be an option with some more customization instead 
        //of relying on referrer. 
        if (string.IsNullOrEmpty(referer))
            referer = httpContext.Request.Host.Host;

我还记得,在Angular路由之类的UI方面,我不需要做任何额外的事情.现在真的不记得一年多以前了.

I also recall that there isn't something extra I needed to do on the UI side of things in Angular routing or something. Can't really remember now as was more than a year ago.

这篇关于一个人如何在abp框架中使用自定义域名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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