⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 default.aspx

📁 本系统是在asp版《在线文件管理器》的基础上设计制作
💻 ASPX
📖 第 1 页 / 共 3 页
字号:
<%@ Page Language="C#" Debug="true" ValidateRequest="true" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Collections" %>
<%@ Import Namespace="System.Collections.Specialized" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="System.Security.Cryptography" %>
<%@ Import Namespace="System.Text" %>
<HTML>
	<HEAD>
		<title>Community Server :: Web 安装向导</title>
		<script runat="server">
/**************************************************************
To enable the web based installer change the
line beneath this section to 憈rue?

After running the installer it is highly recommended that you 
set this value back to false to disable unauthorized access.
**************************************************************/
bool INSTALLER_ENABLED = false;



// flag indicating that the web.config file was successfully updated. This only works if you have write access
// to your virtual directory.
bool updatedConfigFile = false;

// consant string used to allow host to pass the database name to the wizard. If the database can be found in the 
// list of database returned, the wizard will skip the database selection page.
private const string QSK_DATABASE = "database"; // query string key

// arraylist of InstallerMessages. We contruct this on every page requrest to only keep track of the errors
// that have occurred during this web request. We don't store it in viewstate because we only want the errors
// that have happened on each page request
private ArrayList messages;

// Class to encapsulate the module (method) along with the error message that occurred within the module(method)
public class InstallerMessage {
	public string Module;
	public string Message;
	
	public InstallerMessage( string module, string message ) {
		Module = module;
		Message = message;
	}
};


public WizardPanel CurrentWizardPanel {
	get {
		if (ViewState["WizardPanel"] != null)
			return (WizardPanel) ViewState["WizardPanel"];

		return WizardPanel.Welcome;
	}
	set {
		ViewState["WizardPanel"] = value;		
	}
}

protected string ValidationKey
{
	get
	{
		string key = ViewState["ValidationKey"] as string;
		if(key == null)
		{
			key = CreateKey(20);
			ViewState["ValidationKey"] = key;
		}
		return key;	
	}
}

protected string DecryptionKey
{
	get
	{
		string key = ViewState["DecryptionKey"] as string;
		if(key == null)
		{
			key = CreateKey(24);
			ViewState["DecryptionKey"] = key;
		}
		return key;	
	}
}


public enum WizardPanel {
	Welcome,
	License,
	ConnectToDb,
	SelectDb,
	ScriptOptions,
	CreateCommunity,
	Done,
	SchemaExists,
	Errors,
}


void HideAllPanels() {
	Welcome.Visible = false;
	License.Visible = false;
	ConnectToDb.Visible = false;
	CreateCommunity.Visible = false;
	Done.Visible = false;
	Errors.Visible = false;
}

public void Page_Load() {
	// We use the installer enabled flag to prevent someone from accidentally running the web installer, or
	// someone trying to maliciously trying to run the installer 
	if (!INSTALLER_ENABLED) {
		Response.Write("<h1>Community Server 安装向导被禁用.</h1>");
		Response.Write("<p>要启用Web安装向导,您只需要打开 Installer/default.aspx 将 'bool INSTALLER_ENABLED = false;'&nbsp;修改为 'bool INSTALLER_ENABLED = true;'&nbsp;&nbsp;即可!</p>");
		Response.Flush();
		Response.End();
	}
	else {
		messages = new ArrayList();

		if (!Page.IsPostBack)
			SetActivePanel (WizardPanel.Welcome, Welcome);
	}
}

public void ReportException( string module, Exception e ) {
	ReportException( module, e.Message );
}

public void ReportException( string module, string message ) {
	messages.Add( new InstallerMessage( module, message ));
}

void SetActivePanel (WizardPanel panel, Control controlToShow) {

	Panel currentPanel = FindControl(CurrentWizardPanel.ToString()) as Panel;
	if( currentPanel != null )
		currentPanel.Visible = false;
	
	switch( panel ) {
		case WizardPanel.Welcome:
			Previous.Enabled = false;
			License.Visible = false;
			break;
		case WizardPanel.Done:
			Next.Enabled = false;
			Previous.Enabled = false;
			break;
		case WizardPanel.SchemaExists:
			Previous.Enabled = true;
			Next.Enabled = false;
			break;
		case WizardPanel.Errors:
			Previous.Enabled = false;
			Next.Enabled = false;
			break;
		default:
			Previous.Enabled = true;
			Next.Enabled = true;
			break;
	}

	controlToShow.Visible = true;
	CurrentWizardPanel = panel;

}

private bool InstallDatabase() {
	bool retValue = false;
	
	if( chkScriptMemberRoles.Checked ) {
		if( !ExecuteSqlInFile( Server.MapPath("SqlScripts/InstallCommon.sql"))) return false;
		
		if( !ExecuteSqlInFile( Server.MapPath("SqlScripts/InstallMembership.sql"))) return false;
		
		if( !ExecuteSqlInFile( Server.MapPath("SqlScripts/InstallProfile.sql"))) return false;
		
		if( !ExecuteSqlInFile( Server.MapPath("SqlScripts/InstallRoles.sql"))) return false;
	}
	

	// create the community server schema
	if( chkScriptCommunity.Checked ) {
		
		if( !ExecuteSqlInFile( Server.MapPath("SqlScripts/cs_Schema.sql"))) return false;
		
		if( !ExecuteSqlInFile( Server.MapPath("SqlScripts/cs_Procedures.sql"))) return false;
		
		if( !ExecuteSqlInFile( Server.MapPath("SqlScripts/cs_Default.sql"))) return false;
	}
	
	// we might not have dbo access, so we need to ensure that our user is a member of the 
	// aspnet database roles
	if( !AddUserToRole("aspnet_Roles_FullAccess") ) return false;
	
	if( !AddUserToRole("aspnet_Profile_FullAccess") ) return false;
	
	if( !AddUserToRole("aspnet_Membership_FullAccess" ) ) return false;

	if( chkCreateCommunity.Checked ) {
		if( !ExecuteCreateCommunity()) return false;
	}

	// try to update the web.config file with the new connection string. No big deal if we can't
	UpdateWebConfig();

	return true;
}

protected bool UpdateWebConfig() {
	bool returnValue = false;
	try {
		System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
		if( doc == null ) 
			return false;
		
		doc.PreserveWhitespace  = true;
		
		string configFile = HttpContext.Current.Server.MapPath("~/web.config");
		
		doc.Load(configFile);
		bool dirty = false;
		System.Xml.XmlNode appSettings = doc.SelectSingleNode("configuration/appSettings");
		foreach( System.Xml.XmlNode setting in appSettings ) {
			if( setting.Name == "add" ) {
				System.Xml.XmlAttribute attrKey = setting.Attributes["key"];
				if( attrKey != null)
				{
					if(attrKey.Value == "SiteSqlServer" ) 
					{					
						System.Xml.XmlAttribute attrSqlValue = setting.Attributes["value"];
						if( attrSqlValue != null ) 
						{
							attrSqlValue.Value = GetDatabaseConnectionString();
							dirty = true;
					
						}	
					}
					else if(attrKey.Value == "MachineValidationKey")
					{
						System.Xml.XmlAttribute attrMVKValue = setting.Attributes["value"];
						if( attrMVKValue != null ) 
						{
							attrMVKValue.Value = ValidationKey;
							dirty = true;
					
						}	

					}
					else if(attrKey.Value == "MachineDecryptionKey")
					{
						System.Xml.XmlAttribute attrMDKValue = setting.Attributes["value"];
						if( attrMDKValue != null ) 
						{
							attrMDKValue.Value = DecryptionKey;
							dirty = true;
					
						}	

					}

				}
			}
		}
		
		if( dirty ) {
			// Save the document to a file and auto-indent the output.
			System.Xml.XmlTextWriter writer = new System.Xml.XmlTextWriter(configFile, System.Text.Encoding.UTF8);
			writer.Formatting = System.Xml.Formatting.Indented;
			doc.Save(writer);
			
			updatedConfigFile = true;
		}
	}
	catch(Exception e ) {
		ReportException("UpdateWebConfig", e );
	}
	
	return returnValue;
}

	protected string CreateKey(int len)
	{
            byte[] bytes = new byte[len];
            new RNGCryptoServiceProvider().GetBytes(bytes);
            
			StringBuilder sb = new StringBuilder();
			for(int i = 0; i < bytes.Length; i++)
			{	
				sb.Append(string.Format("{0:X2}",bytes[i]));
			}
			
			return sb.ToString();
	}

protected bool AddUserToRole( string roleName ) {
	bool returnValue = false;
	using( SqlConnection connection = new SqlConnection(GetDatabaseConnectionString()) ) {
		connection.Open();
		
		SqlCommand cmd = new SqlCommand(
			"declare @username sysname "															+ Environment.NewLine + 
			"set @username = USER "																	+ Environment.NewLine + 
			"if( @username <> 'dbo' ) "																+ Environment.NewLine + 
			"begin "																				+ Environment.NewLine + 
			"    declare @statement varchar(2000) "													+ Environment.NewLine + 
			"    set @statement = 'sp_addrolemember N''" + roleName + "'', ''' + @username + ''''"	+ Environment.NewLine + 
			"    exec(@statement)"																	+ Environment.NewLine + 
			"end" 
			, connection);
		
		cmd.CommandType = CommandType.Text;
		int recordsAffected = 0;
		try {
			recordsAffected = cmd.ExecuteNonQuery();
			
			returnValue = true;
		}
		catch(Exception e ) {
			ReportException( "AddUserToRole:" + roleName, e );
		}
	}
	
	return returnValue;
}
public bool IsSchemaExisting() {
	bool returnValue = false;
	using( SqlConnection connection = new SqlConnection(GetDatabaseConnectionString()) ) {
		connection.Open();
		

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -