Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Account based lookup in ASP.NET

I'm looking at using ASP.NET for a new SaaS service, but for the love of me I can't seem to figure out how to do account lookups based on subdomains like most SaaS applications (e.g. 37Signals) do.

For example, if I offer yourname.mysite.com, then how would I use ASP.NET (MVC specifically) to extract the subdomain so I can load the right template (displaying your company's name and the like)? Can it be done with regular routing?

This seems to be a common thing in SaaS so there has to be an easy way to do it in ASP.NET; I know there are plugins that do it for other frameworks like Ruby on Rails.

like image 768
Wayne Molina Avatar asked Dec 09 '08 16:12

Wayne Molina


People also ask

What is LookUp in ASP net?

Background. This Lookup Control is a simple web user control. The basic idea of this web control is to provide the ability to bind data from a database table to a web user control for user friendly data access and selection with fast data loading, sorting, pagination and many more capabilities.

How can add search box in ASP NET MVC?

Right-click on the controller folder then select Add -> controller. Select MVC 5 Controller with read/write actions and click Add. Provide the controller a name. Click Add.


3 Answers

You should be able to pick this up from the ServerVariables collection, but first you need to configure IIS and DNS to work correctly. So you know 37Signals probably use Apache or another open source, unix web server. On Apache this is referred to as VirtualHosting.

To do this with IIS you would need to create a new DNS entry (create a CNAME yourname.mysite.com to application.mysite.com) for each domain that points to your application in IIS (application.mysite.com).

You then create a host header entry in the IIS application (application.mysite.com) that will accept the header yourname.mysite.com. Users will actually hit application.mysite,com but the address is the custom subdomain. You then access the ServerVariables collection to get the value to decide on how to customize the site.

Note: there are several alternative implementations you could follow depending on requirements.

  • Handle the host header processing at a hardware load balancer (more likely 37Signals do this, than rely on the web server), and create a custom HTTP header to pass to the web application.
  • Create a new web application and host header for each individual application. This is probably an inefficient implementation for a large number of users, but could offer better isolation and security for some people.
like image 36
Brian Lyttle Avatar answered Nov 08 '22 05:11

Brian Lyttle


This works for me:

    //--------------------------------------------------------------------------------------------------------------------------
    public string GetSubDomain()
    {
        string SubDomain = "";

        if (Request.Url.HostNameType == UriHostNameType.Dns)
            SubDomain = Regex.Replace(Request.Url.Host, "((.*)(\\..*){2})|(.*)", "$2");
        if (SubDomain.Length == 0)
            SubDomain = "www";
        return SubDomain;
    }

I'm assuming that you would like to handle multiple accounts within the same web application rather than building separate sites using the tools in IIS. In our work, we started out creating a new web site for each subdomain but have found that this approach doesn't scale well - especially when you release an update and then have to modify dozens of sites! Thus, I do recommend this approach rather than the server-oriented techniques suggested above based on several years worth of experience doing exactly what you propose.

The code above just makes sure that this is a fully formed URL (rather, say, than an IP address) and returns the subdomain. It has worked well for us in a fairly high-volume environment.

like image 50
Mark Brittingham Avatar answered Nov 08 '22 07:11

Mark Brittingham


You need to configure your DNS to support wildcard subdomains. It can be done by adding an A record pointing to your IP address, like this:

* A 1.2.3.4

Once its done, whatever you type before your domain will be sent to your root domain, where you can get by splitting the HTTP_HOST server variable, like the user buggs said above:

 string user = HttpContext.Request.ServerVariables["HTTP_HOST"].Split(".")

//use the user variable to query the database for specific data

PS. If you are using a shared hosting you're probably going to have to by a Unique IP addon from them, since it's mandatory for the wildcard domains to work. If you're using a dedicated hosting you already have your own IP.

like image 1
holiveira Avatar answered Nov 08 '22 05:11

holiveira