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

📄 webadminpage.cs

📁 《圣殿祭司的ASP.NET 2.0开发详解——使用C#》光盘内容.包含了书籍所含的源代码.非常经典的一本asp.net2.0的书籍
💻 CS
📖 第 1 页 / 共 4 页
字号:
            if (userRadio.Checked && userName.Text.Trim().Length == 0) {
                placeHolderValidator.ErrorMessage = ((string)GetGlobalResourceObject("GlobalResources", "NonemptyUser"));
                placeHolderValidator.IsValid = false;
                return false;
            }
            if (roleRadio.Checked && roles.SelectedItem == null) {
                placeHolderValidator.ErrorMessage = ((string)GetGlobalResourceObject("GlobalResources", "NonemptyRole"));
                placeHolderValidator.IsValid = false;
                return false;
            }
            if (userRadio.Checked) {
                string userNameString = userName.Text.Trim();
                if (-1 != userNameString.IndexOf('*')) {
                    placeHolderValidator.ErrorMessage = ((string)GetGlobalResourceObject("GlobalResources", "InvalidRuleName"));
                    placeHolderValidator.IsValid = false;
                    return false;
                }
            }
            return true;
        }

        public bool IsWindowsAuth() {
            Configuration config = OpenWebConfiguration(ApplicationPath);
            AuthenticationSection auth = (AuthenticationSection)config.GetSection("system.web/authentication");
            return auth.Mode == AuthenticationMode.Windows;
        }

        protected void ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e) {
            ListItemType itemType = e.Item.ItemType;
            if ((itemType == ListItemType.Pager) ||
                (itemType == ListItemType.Header) ||
                (itemType == ListItemType.Footer)) {
                return;
            }
            foreach (Control c in e.Item.Cells[0].Controls) {
                LinkButton button = c as LinkButton;
                if (button == null) {
                    continue;
                }
                e.Item.Attributes["onclick"] = GetPostBackClientHyperlink(button, String.Empty);
           }
        }

        public Configuration OpenWebConfiguration(string path) {
            return OpenWebConfiguration(path, false);
        }

        public Configuration OpenWebConfiguration(string path, bool getWebConfigForSubDir) {
            string appPhysPath = (string)Session[APP_PHYSICAL_PATH];
            return OpenWebConfiguration(path, appPhysPath, getWebConfigForSubDir);
        }

        private Configuration OpenWebConfiguration(string path, string appPhysPath, bool getWebConfigForSubDir) {
            // check if session expired
            if (String.IsNullOrEmpty(ApplicationPath) || String.IsNullOrEmpty((string)Session[APP_PHYSICAL_PATH])) {
                Server.Transfer("~/home2.aspx");
            }

            if (String.IsNullOrEmpty(path)) {
                return WebConfigurationManager.OpenWebConfiguration(null);
            }

            string appVPath = (string)Session[APP_PATH];
            if (!getWebConfigForSubDir) {
                appVPath = path;
            }

            WebConfigurationFileMap fileMap = new WebConfigurationFileMap();
            fileMap.VirtualDirectories.Add(appVPath, new VirtualDirectoryMapping(appPhysPath, true));
            return WebConfigurationManager.OpenMappedWebConfiguration(fileMap, path);
        }

        protected override void OnInit(EventArgs e) {
            string applicationPath = ApplicationPath;
            string queryStringAppPath = GetQueryStringAppPath();
            string applicationPhysicalPath = GetQueryStringPhysicalAppPath();
            string requestAppPath = HttpContext.Current.Request.ApplicationPath;
            string currExFilePath = Request.CurrentExecutionFilePath;

            if (applicationPhysicalPath != null) {
                string webAdminVersion = "aspnet_webadmin\\" + UnderscoreProductVersion + "\\";
                if (applicationPhysicalPath.EndsWith(webAdminVersion)) {
                    queryStringAppPath = requestAppPath;
                }
            }

            SetApplicationPath();
            
            if (string.Compare(CurrentRequestUrl, Request.CurrentExecutionFilePath) != 0) {
                PushRequestUrl(Request.CurrentExecutionFilePath);
            }

            base.OnInit(e);
        }

        protected void PopulateRepeaterDataSource(Repeater repeater) {
            // display alphabet row only if language is has Alphabet resource
            ArrayList arr = new ArrayList();
            String chars = ((string)GetGlobalResourceObject("GlobalResources", "Alphabet"));
            foreach (String s in chars.Split(';')) {
                arr.Add(s);
            }
            if (arr.Count == 0) {
                repeater.Visible = false;
            } else {
                arr.Add((string)GetGlobalResourceObject("GlobalResources", "All"));
                repeater.DataSource = arr;
                repeater.Visible = true;
            }
        }

        protected string PopPrevRequestUrl() {
            Stack stack = (Stack) Session[URL_STACK];
            if (stack == null || stack.Count < 2) {
                return string.Empty;
            }
            stack.Pop(); // discard current url
            return(string) stack.Pop();
        }

        protected void PushRequestUrl(string s) {
            Stack stack = (Stack) Session[URL_STACK];
            if (stack == null) {
                Session[URL_STACK] = stack = new Stack();
            }
            stack.Push(s);
        }

        protected void ReturnToPreviousPage(object sender, EventArgs e) {
            string prevRequest = PopPrevRequestUrl();
            Response.Redirect(prevRequest, false);  // note: string.Empty ok here.
        }

        protected void RetrieveLetter(object sender, RepeaterCommandEventArgs e, GridView dataGrid) {
            RetrieveLetter(sender, e, dataGrid, (string)GetGlobalResourceObject("GlobalResources", "All"));
        }

        protected void RetrieveLetter(object sender, RepeaterCommandEventArgs e, GridView dataGrid, string all) {
            RetrieveLetter(sender, e, dataGrid, all, null);
        }

        protected void RetrieveLetter(object sender, RepeaterCommandEventArgs e, GridView dataGrid, string all, MembershipUserCollection users) {
            dataGrid.PageIndex = 0;
            int total = 0;
            string arg = e.CommandArgument.ToString();

            if (arg == all) {
                dataGrid.DataSource = (users == null) ? (MembershipUserCollection)CallWebAdminHelperMethod(true, "GetAllUsers", new object[] {0, Int32.MaxValue, total}, new Type[] {typeof(int),typeof(int),Type.GetType("System.Int32&")}) : users;
            }
            else {
                dataGrid.DataSource = (MembershipUserCollection)CallWebAdminHelperMethod(true, "FindUsersByName", new object[] {(string) arg + "%", 0, Int32.MaxValue, total}, new Type[] {typeof(string), typeof(int), typeof(int), Type.GetType("System.Int32&")});
            }
            dataGrid.DataBind();
        }

        protected void SetApplicationPath() {
            if (!VerifyAppValid()) {
                Server.Transfer("~/error.aspx");
            }
        }

        // At point when app is set, verify it is valid (i.e., permissions are proper and app exists).
        private bool VerifyAppValid() {

            // check if session expired
            if (String.IsNullOrEmpty(ApplicationPath) || String.IsNullOrEmpty((string)Session[APP_PHYSICAL_PATH])) {
                Server.Transfer("~/home2.aspx");
            }

            try {
                Configuration config = OpenWebConfiguration(ApplicationPath);
                SaveConfig(config);
            }
            catch (Exception e) {
                Exception ex = new Exception((string) e.ToString());
                SetCurrentException(Context, ex);
                return false;
            }
            return true;
        }
    }

    [AspNetHostingPermission(SecurityAction.LinkDemand, Level=AspNetHostingPermissionLevel.Minimal)]
    [AspNetHostingPermission(SecurityAction.InheritanceDemand, Level=AspNetHostingPermissionLevel.Minimal)]
    public sealed class WebAdminModule : IHttpModule {
        private const string APP_PATH = "WebAdminApplicationPath";
        private const string APP_PHYSICAL_PATH = "WebAdminPhysicalPath";
        private const string REMOTING_MANAGER = "WebAdminRemotingManager";
        
        private void ApplicationError(object sender, EventArgs e) {            
            Exception exception  = ((HttpApplication)sender).Server.GetLastError().InnerException;
            HttpContext context = ((HttpApplication)sender).Context;
            string redirectUrl = "~/error.aspx";
            if (String.IsNullOrEmpty(redirectUrl)) {
                return;
            }

            WebAdminPage.SetCurrentException(context, exception);

            ((HttpApplication)sender).Server.Transfer(String.Format(CultureInfo.InvariantCulture, redirectUrl), true);
        }

        public void Init(HttpApplication application) {
            application.Error += (new EventHandler(this.ApplicationError));
            application.AcquireRequestState += new EventHandler(this.OnEnter);
        }

        public void Dispose() {
        }

        private void OnEnter(Object sender, EventArgs eventArgs) {
            HttpApplication application = (HttpApplication)sender;

            if (!application.Context.Request.IsLocal) {
                SecurityException securityException = new SecurityException((string)HttpContext.GetGlobalResourceObject("GlobalResources", "WebAdmin_ConfigurationIsLocalOnly"));
                WebAdminPage.SetCurrentException(application.Context, securityException);
                application.Server.Transfer("~/error.aspx");
            }

            if (application != null) {
                SetSessionVariables(application);
            }
            application.Response.Cache.SetCacheability(HttpCacheability.NoCache);
        }

        private void SetSessionVariables(HttpApplication application) {
            string queryStringAppPath = string.Empty;
            string queryStringApplicationPhysicalPath = string.Empty;
            string applicationPath = string.Empty;
            string applicationPhysicalPath = string.Empty;
            string setAppPath = string.Empty;
            string setAppPhysPath = string.Empty;

            try {
                SecurityPermission permission = new SecurityPermission(PermissionState.Unrestricted);
                permission.Demand();
            } catch {
                Exception permissionException = new Exception((string)HttpContext.GetGlobalResourceObject("GlobalResources", "FullTrustRequired"));
                WebAdminPage.SetCurrentException(application.Context, permissionException);
                application.Server.Transfer("~/error.aspx");
            }

            if (application.Context.Request != null) {
                queryStringAppPath = (string) application.Context.Request.QueryString["applicationUrl"];
                queryStringApplicationPhysicalPath = (string) application.Context.Request.QueryString["applicationPhysicalPath"];
            }

            if (application.Context.Session != null) {
                if (application.Context.Session[APP_PATH] != null) {
                    applicationPath = (string)application.Context.Session[APP_PATH];
                }
                if (application.Context.Session[APP_PHYSICAL_PATH] != null) {
                    applicationPhysicalPath = (string)application.Context.Session[APP_PHYSICAL_PATH];
                }
            }

            if ((String.IsNullOrEmpty(queryStringAppPath) && applicationPath == null) ||
               (String.IsNullOrEmpty(queryStringApplicationPhysicalPath) && applicationPhysicalPath == null) ) {
                application.Server.Transfer("~/home0.aspx", false);
                return;
            }

            if (!String.IsNullOrEmpty(queryStringAppPath)) {
                setAppPath = queryStringAppPath;
            } else if (!String.IsNullOrEmpty(applicationPath)) {
                setAppPath = applicationPath;
            }

            if (!String.IsNullOrEmpty(queryStringApplicationPhysicalPath)) {
                setAppPhysPath = queryStringApplicationPhysicalPath;
            } else if (!String.IsNullOrEmpty(applicationPhysicalPath)) {
                setAppPhysPath = applicationPhysicalPath;
            }

            if (application.Context.Session != null) {

⌨️ 快捷键说明

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