ASP.NET - 为每次登录创建子域 [英] ASP.NET - Create Sub domain for each login

查看:48
本文介绍了ASP.NET - 为每次登录创建子域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我需要一个紧急的想法/如何为在我的应用程序中注册的每个用户创建子域名..



例如)我在www.xyz.com上托管我的申请

如果名为bsoft的用户在我的应用程序中注册了。

我需要为用户创建一个网站bsoft.xyz.com

简单来说,如何为每个用户重写网址?



请帮助..

Hi,

I need an urgent idea/ how to create sub-domain for each user who is registering in my application..

For eg) I hosted my application in www.xyz.com
If a user with name "bsoft" is registered in my app.
I need to create a site for the user as "bsoft.xyz.com"
In simple words how to rewrite the url for each user??

Please help..

推荐答案

您好

尝试使用以下代码可能会解决您的问题



Hi
try with below code it may solve your problem

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Administration;
using Microsoft.Web.Administration;
using System.IO;


public partial class _Default : System.Web.UI.Page
{

    private const string SERVER_IP = "192.168.111.112";// put your ip address
  private const int PORT = 80;
  private const string WEB_DOMAIN_PATH = @"F:\\web\domains\{0}\";

    //Live server
    //private const string SERVER_IP = "192.168.111.111";

    protected void Page_Load(object sender, EventArgs e)
    {
   if (!string.IsNullOrEmpty(Request.QueryString["user"]))
   {

    try
    {
     string username = Request.QueryString["user"];
     string status = CreateUserSite(username, "codeproject.com");//change your Domain id

     Response.Write(status);
    }
    catch(Exception ex)
    {
     Response.Write(ex.Message); 
    }
   }
   else
   {
    Response.Write("user parameter not supplied"); 
   }

   
    }


    private string CreateUserSite(string user, string domain)
    {


   string path = string.Format(WEB_DOMAIN_PATH, domain);

        string userpath = path + user;

        string userUrl = user + "." + domain;

        using (ServerManager serverManager = new ServerManager())
        {

            bool siteExists = false;
            int number = serverManager.Sites.Where(p => p.Name.ToLower().Equals(userUrl.ToLower())).Count();

            if (number == 0)
            {
                siteExists = false;
            }
            else
            {
                siteExists = true;
            }

            if (!siteExists)
            {

                //create user directory
                Directory.CreateDirectory(userpath);

                //copy every files from a-base to a new created folder
                FileInfo[] d = new DirectoryInfo(path + @"\a-base").GetFiles();
                foreach (FileInfo fi in d)
                {
                    File.Copy(fi.FullName, userpath + @"\" + fi.Name, true);
                }

                //create a directory
                Directory.CreateDirectory(userpath + @"\swfobject");

                FileInfo[] d1 = new DirectoryInfo(path + @"\a-base\swfobject").GetFiles();
                foreach (FileInfo fi in d1)
                {
                    File.Copy(fi.FullName, userpath + @"\swfobject\" + fi.Name, true);
                }



                //create site
                Site mySite = serverManager.Sites.Add(userUrl, path + user, PORT);
                mySite.ServerAutoStart = true;
                mySite.Applications[0].ApplicationPoolName = domain;

                //create bindings
                mySite.Bindings.Clear();
                mySite.Bindings.Add(string.Format("{0}:{2}:{1}", SERVER_IP, userUrl, PORT ), "http");
                mySite.Bindings.Add(string.Format("{0}:{2}:www.{1}", SERVER_IP, userUrl, PORT), "http");


                Configuration config = serverManager.GetApplicationHostConfiguration();
                ConfigurationSection httpLoggingSection = config.GetSection("system.webServer/httpLogging", userUrl);
                httpLoggingSection["dontLog"] = true;

                serverManager.CommitChanges();

              //  ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "success", "alert(''" + userUrl + " created'');", true);

            }
            else
            {
                //ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "error", "alert(''user exists. Please use other name'');", true);
       throw new Exception("user exists. Please use other name");
      }


      return userUrl + " has been successfully created"; 
        }
    }
}


嗨srimathi

据我说你必须联系与您的托管服务器供应商可能会为您提供API。
Hi srimathi
According to me you have to contact with your Hosting Server Vendor may they will provide you API for this.


这篇关于ASP.NET - 为每次登录创建子域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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