In my webapp (work in progress) I now try to allow new customers register to my webapp. I am deploying in Azure btw.
在我的webapp(正在进行中)中,我现在尝试允许新客户注册我的webapp。我在Azure btw中部署。
The idea I am working on:
我正在研究的想法:
www.[mysite].com/Register
万维网。[mysite的]的.com /注册
This register page allows the "new" user to register his a new tenant.
该注册页面允许“新”用户注册他的新租户。
customer.[mysite].com/Register
顾客。[mysite的]的.com /注册
This pretty much looks as follows in code
这几乎在代码中看起来如下
public ActionResult Register()
{
var url = Request.ServerVariables["HTTP_HOST"];
var tenantNameParser = new TenantNameParser(url);
if (!TenantRepository.Exists(tenantNameParser.TenantName))
{
return RedirectToAction("NewCustomer");
}
return View();
}
In the above snippet I check if tenant exists, if not, redirect to new customer.
在上面的代码片段中,我检查租户是否存在,如果没有,则重定向到新客户。
[HttpPost]
public ActionResult NewCustomer(Customer model)
{
if (ModelState.IsValid)
{
var tenant = new Tenant
{
Name = model.TenantName,
Guid = Guid.NewGuid().ToString()
};
TenantRepository.Add(tenant);
// TODO create a new user new tenant repository to do this stuff as 1 atomic action
if (!UserRepository.Exists(model.AccountableEmailAddress))
{
// Attempt to register the user
var createStatus = MembershipService.CreateUser(
model.AccountableUser,
model.Password,
model.AccountableEmailAddress);
if (createStatus == MembershipCreateStatus.Success)
{
FormsService.SignIn(model.AccountableUser, false);
return RedirectToAction("Index", "Home");
}
throw new Exception("crappy implemenation, improve");
}
FormsService.SignIn(model.AccountableUser, false);
// TODO figure out how to redirect to tenant.site.com
return RedirectToAction("Index", "Home");
}
return View(model);
}
The code above creates a new customer, and in the end my //todo is what this question is about. How to redirect to an URL that includes the actual tenant in the form of tenant.domain.com?
上面的代码创建了一个新客户,最后我的// todo就是这个问题的内容。如何以tenant.domain.com的形式重定向到包含实际租户的URL?
I don't know if this "way of handling tenants" is a bad practice, if so please feel free to say so.
我不知道这种“处理租户的方式”是不是一种坏习惯,如果是这样,请随意说出来。
Question: How to redirect to a tenant URL ([tenantname.[mysite].com/Index/Home). Something like: Redirect("tenantname", "Index", "Home") would be great, but of course doesn't exist. I google'd it for a bit, but didn't run into helpful links (that's mainly the reason why I think I am designing "the wrong thing").
问题:如何重定向到租户URL([tenantname。[mysite] .com / Index / Home)。类似的东西:重定向(“租户”,“索引”,“主页”)会很棒,但当然不存在。我谷歌了一下,但没有遇到有用的链接(这主要是我认为我设计“错误的东西”的原因)。
Advice would be awesome! Thx a lot for your considerations in advance!
建议太棒了!很多事先考虑你的考虑因素!
If I need to rephrase stuff because it's unclear what I ask for then please let me know.
如果我需要改写一些东西,因为我不清楚我的要求,请告诉我。
2 个解决方案
#1
1
Just my 2 cents, please correct me if it's wrong.
只是我的2美分,请纠正我,如果它是错的。
My understanding is, you cannot control the tenant name in [tenant name].yoursite.com in your code, as it's out of the scope of ASP.NET MVC. It's a CNAME of the domain yoursite.com. So some thoughts from my end below.
我的理解是,您无法在代码中控制[租户名称] .yoursite.com中的租户名称,因为它超出了ASP.NET MVC的范围。它是域名yoursite.com的CNAME。所以下面我的一些想法。
-
I guess you need a program-able domain provider, which means you can create CNAME from your code. Then when a new customer registered, the actual URL might be yoursite.com/[tenant name]/. Then you map it to a new CNAME [tenant name].yoursite.com.
我猜您需要一个可编程的域提供程序,这意味着您可以从代码中创建CNAME。然后,当新客户注册时,实际URL可能是yoursite.com/ [title name] /。然后将其映射到新的CNAME [租户名称] .yoursite.com。
-
I'm not sure an URL Rewrite can help you. Which means you create a new rule and if the incoming URL was [tenant name].yoursite.com, you map it to yoursite.com/[tenant name]/
我不确定URL Rewrite可以帮到你。这意味着您创建了一个新规则,如果传入的URL是[租户名称] .yoursite.com,则将其映射到yoursite.com/ [title name] /
#2
1
To accomplish this, you need to look at Domain Routing for ASP .NET MVC.
要实现此目的,您需要查看ASP.NET MVC的域路由。
#1
1
Just my 2 cents, please correct me if it's wrong.
只是我的2美分,请纠正我,如果它是错的。
My understanding is, you cannot control the tenant name in [tenant name].yoursite.com in your code, as it's out of the scope of ASP.NET MVC. It's a CNAME of the domain yoursite.com. So some thoughts from my end below.
我的理解是,您无法在代码中控制[租户名称] .yoursite.com中的租户名称,因为它超出了ASP.NET MVC的范围。它是域名yoursite.com的CNAME。所以下面我的一些想法。
-
I guess you need a program-able domain provider, which means you can create CNAME from your code. Then when a new customer registered, the actual URL might be yoursite.com/[tenant name]/. Then you map it to a new CNAME [tenant name].yoursite.com.
我猜您需要一个可编程的域提供程序,这意味着您可以从代码中创建CNAME。然后,当新客户注册时,实际URL可能是yoursite.com/ [title name] /。然后将其映射到新的CNAME [租户名称] .yoursite.com。
-
I'm not sure an URL Rewrite can help you. Which means you create a new rule and if the incoming URL was [tenant name].yoursite.com, you map it to yoursite.com/[tenant name]/
我不确定URL Rewrite可以帮到你。这意味着您创建了一个新规则,如果传入的URL是[租户名称] .yoursite.com,则将其映射到yoursite.com/ [title name] /
#2
1
To accomplish this, you need to look at Domain Routing for ASP .NET MVC.
要实现此目的,您需要查看ASP.NET MVC的域路由。