ساخت زیر دامنه بوسیله کدهای C# در asp.net

تازه ها

ساخت زیر دامنه بوسیله کدهای C# در asp.net

نظرات ()

می خواهیم توی وبسایت که با دات نت نوشته شده است قسمتی داشته باشم که کاربران بتونن زیر دامنه SUB Domain بسازن و یه صفحه مخصوص خودشون داشته باشند. دقیقا مثل ساختن یک وب لاگ .


به دو طریق می تونید ساب دومین ایجاد كنید: مجازی و حقیقی


در حالت حقیقی شما باید با استفاده از Shell و Commandهای اون، اسكریپتهای تولید SubDomain رو روی IIS اجرا كنید. طبیعتا نیاز دارید كه Permission های لازم رو به فولدرها بدید. چون این روش ممكنه موجب ضعف امنیتی سیستم سرور بشه مسلما هیچ هاستی این اجازه رو به شما نمی ده مگر اینكه هاستتون Dedicated باشه كه باز ملاحظات امنیتی برقرارند. بنابر این دور این موضوع رو خط قرمز بكشید.


در حالت مجازی در واقع SubDomain ای وجود نداره و شما Requestها رو كه به صورت Subdomain به سرور ارسال شدند رو می گیرید و اونها رو به فولدرهای داخلی ارجاع میدید. این كار می تونه به راحتی از طریق URLRewrite تو Global.asax.cs تو رویداد Application_BeginRequest انجام بشه. طبیعتا این روش رو می تونید روی هر سرور و هاستی بدون اعمال تغییرات فیزیكی و بدون اینكه حتی مدیر هاست خبردار بشه پیاده سازی كنید.

URLRewrite ابزار قدرتمندیه كه می تونه حتی در حالت عادی كاری كنه صفحات ظاهرا از جایی خونده بشند كه اصلا وجود فیزیكی ندارند.


نهایتا اینكه همین پروسه رو می تونید به نحوی به اطلاعات كاربر جاری متصل كنید. دقت كنید كه می تونید مدیریت بخش رو به صورت صفحات عادی داخل سایت و نمایش اونها رو به صورت ساب دومین پیاده سازی كنید یعنی دقیقا همین كاری كه بلاگر ها می كنند.


جهت اطلاعات بیشتر و نحوه انجام کار :

Create Unlimited Subdomains with HTTP Modules

I recently took the hundred or so top search words for CodeBetter.Com and created an “Explore CodeBetter” control with links to pages about those search words.  I wanted the links to be to a subdomain with the keyword as the first label of the subdomain.  So, agile would link to agile.codebetter.com, which internally would be forwarded to a handler that hooks into Community Server’s search engine and presents a list of pages containing the word agile.  As implemented, this control currently looks like this:

Setting up DNS

If your DNS supports wildcard syntax, you’ll be able to do this.   Basically you just want to forward all requests to *.yourdomain.com to yourdomain.com.  If your ISP’s dns doesn’t support the wildcard syntax, you could handle your own DNS on your server.

Setting up IIS

If you use host headers and you’ve got a bunch of different sites configured, you’ll need to add a blank host header, so that any requests for subdomains not listed  get forwarded to the proper website.   If you want to set this up for multiple websites per server, I’m not sure how to do this, perhaps someone out there knows how.  Basically the hostheader configuration won’t accept *.yourdomain.com, so I’m not sure how to set this up, except to have all un-configured domains going to one website using the blank hostheader technique.

Creating the HTTP Module

Next, you’ll need to intercept the request, inspect the requested URL and forward requests appropriately.  A good way to do this is by creating an IHttpModule and plugging it into the pipeline.  For forwarding, all you’ll need to do is handle the OnBeginRequest event, and then use Server.Transfer to forward the request.  The nice side-effect of this is that the URL in the browser doesn’t change when using Server.Transfer (as opposed to Response.Redirect), leaving your nice clean URL in the browser.  Here’s the code for my handler.  I also do some checking to make sure that the homepage (default.aspx) was requested, because I don’t want to do this on sub-page requests. 

   public class SubdomainModule : IHttpModule

    {

        public void Init(HttpApplication app)

        {

            // register for pipeline events

            app.BeginRequest += new EventHandler(this.OnBeginRequest);

        }

 

        public void Dispose() {}

 

        public void OnBeginRequest(object o, EventArgs args)

        {

 

            // get access to app and context

            HttpApplication app = (HttpApplication)o;

            HttpContext ctx = app.Context;

 

            if (!(app.Context.Request.Url.Host.StartsWith(“codebetter”) || app.Context.Request.Url.Host.StartsWith(“www”)))

            {

                if (ctx.Request.Path.ToUpper() == “/DEFAULT.ASPX”)

                {

                    string[] domainParts = app.Context.Request.Url.Host.Split(“.”.ToCharArray());

                    if (domainParts.Length > 2)

                        ctx.Server.Transfer(“/explore/explore.aspx?q=” + domainParts[0]);

                }

            }

        }

    }

Registering the Module in the pipeline

This is simple, you can just add the following to your web.config file:

<httpModules>   <add name=”SubdomainModule” type=”Tompkins.Cs.Subdomains.SubdomainModule, Tompkins.Cs.SubdomainHandler” /> httpModules>

That’s it!  Any requests will be forwarded.  You can now simply request any subdomain on codebetter, like http://monkeys.codebetter.com or http://baseball.codebetter.com.  Need advice on relationships or read that funny post about my ex-girlfriend finding my blog?  Just type http://girlfriend.codebetter.com.  Heck, you could even use us as a help system!  If you need to know about ADO, just type http://ado.codebetter.com. Good luck!





الکسا