コード例 #1
0
        //public static void CreateHttpHandler(SystemWebSectionGroup websec)
        //{
        //    HttpHandlersSection httpHandlers = websec.HttpHandlers;
        //    bool found = false;
        //    foreach (HttpHandlerAction handler in httpHandlers.Handlers)
        //    {
        //        if (handler.Path == UniHttpHandler.HandlerPath)
        //        {
        //            found = true;
        //            break;
        //        }
        //    }
        //    if (!found)
        //    {
        //        HttpHandlerAction handler = new HttpHandlerAction(UniHttpHandler.HandlerPath, "Unicorn.Web.Security.JcoHttpHandler, Unicorn.Web", "*");
        //        httpHandlers.Handlers.Add(handler);
        //    }
        //}
        private static void InitConfigSections(ref System.Configuration.Configuration conf)
        {
            if (conf.GetSectionGroup("uniSecurity") != null)
            {
                return;
            }
            XmlDocument doc = new XmlDocument();

            doc.Load(conf.FilePath);
            XmlNode confNode     = doc["configuration"];
            XmlNode confSections = confNode["configSections"];

            if (confSections == null)
            {
                confSections = doc.CreateElement("configSections");
                confNode.InsertBefore(confSections, confNode.FirstChild);
            }
            if (!HasJcoSecurityInConfigSection(confSections))
            {
                XmlElement   jcoSec = doc.CreateElement("sectionGroup");
                XmlAttribute attr   = doc.CreateAttribute("name");
                attr.Value = "uniSecurity";
                jcoSec.Attributes.Append(attr);
                attr       = doc.CreateAttribute("type");
                attr.Value = "Unicorn.Web.Security.Configuration.UniSecuritySectionGroup, Unicorn.Web";
                jcoSec.Attributes.Append(attr);

                XmlElement masterPage = doc.CreateElement("section");
                attr       = doc.CreateAttribute("name");
                attr.Value = "masterPage";
                masterPage.Attributes.Append(attr);
                attr       = doc.CreateAttribute("type");
                attr.Value = "Unicorn.Web.Security.Configuration.MasterPageSection, Unicorn.Web";
                masterPage.Attributes.Append(attr);

                jcoSec.AppendChild(masterPage);
                confSections.AppendChild(jcoSec);

                CreateJcoSecuritySection(confNode);
                doc.Save(conf.FilePath);
                conf = ConfigUtility.GetConfigFile();
            }
        }
コード例 #2
0
 public static void InitializeSql(string connectionStringName)
 {
     if (initialized)
     {
         return;
     }
     try
     {
         System.Configuration.Configuration conf = ConfigUtility.GetConfigFile();
         SystemWebSectionGroup websec            = conf.GetSectionGroup("system.web") as SystemWebSectionGroup;
         WebConfigInitializer.InitWebConfig(conf, false, connectionStringName);
     }
     catch
     {
     }
     if (!DatabaseTablesExist)
     {
         InitSqlDatabase(connectionStringName);
     }
     initialized = true;
 }
コード例 #3
0
        //static void btnDoConfig_Click(object sender, EventArgs e)
        //{
        //	Initialize();
        //}

        public static bool CheckAppConfiguration(out string error)
        {
            //error="";
            //return true;
            try
            {
                error = "";
                System.Configuration.Configuration conf = null;
                try
                {
                    conf = ConfigUtility.GetConfigFile();
                }
                catch
                {
                }
                SystemWebSectionGroup websec = ConfigUtility.GetSystemWebSectionGroup(conf);
                if (websec.Authentication.Mode != AuthenticationMode.Forms)
                {
                    error = "Authentication.Mode = " + websec.Authentication.Mode.ToString();
                }
                if (websec.Membership.DefaultProvider != UniMembershipProvider.ProviderName)
                {
                    //&& websec.Membership.DefaultProvider != AccessMembershipProvider.ProviderName)
                    if (websec.Membership.DefaultProvider == null)
                    {
                        error = "Membership.DefaultProvider = null";
                    }
                    else
                    {
                        error = "Membership.DefaultProvider = " + websec.Membership.DefaultProvider;
                    }
                }
                if (!Roles.Enabled)
                {
                    error = "Roles is not enabled.";
                }
                if (error != "")
                {
                    return(false);
                }
                //UniSecuritySectionGroup jcoSec = (UniSecuritySectionGroup)conf.GetSectionGroup("uniSecurity");
                //if (jcoSec == null)
                //    error = "UniSecuritySectionGroup is not defined.";
                //if (jcoSec.MasterPage.Path == "")
                //	error = "JcoSecuritySectionGroup.MasterPage.Path is empty.";
                if (error != "")
                {
                    return(false);
                }
                if (websec.Membership.DefaultProvider == UniMembershipProvider.ProviderName)
                {
                    string conName = websec.Membership.Providers[UniMembershipProvider.ProviderName].Parameters["connectionStringName"];
                    bool   b       = CheckSqlTablesExists(conf.ConnectionStrings.ConnectionStrings[conName].ConnectionString);
                    if (!b)
                    {
                        error = "Sql tables doen not exists.";
                        return(false);
                    }
                }
                else
                {
                    throw new NotSupportedException();
                    //string conName = websec.Membership.Providers[AccessMembershipProvider.ProviderName].Parameters["connectionStringName"];
                    //if (!File.Exists(HttpContext.Current.Server.MapPath(conf.ConnectionStrings.ConnectionStrings[conName].ConnectionString)))
                    //{
                    //    error = "Access DB file: '" + conf.ConnectionStrings.ConnectionStrings[conName].ConnectionString + "' does not exists.";
                    //    return false;
                    //}
                }
            }
            catch (Exception ex)
            {
                error = ex.ToString();
                return(false);
            }
            return(true);
        }
コード例 #4
0
        internal static bool CheckConfig(Control control)
        {
            if (initialized)
            {
                return(true);
            }
            string    error;
            HyperLink lnk = new HyperLink();

            if (CheckAppConfiguration(out error))
            {
                //if (Array.IndexOf<string>(Roles.GetAllRoles(), "admin") == -1 && Array.IndexOf<string>(Roles.GetAllRoles(), "Admin") == -1)
                if (Roles.GetAllRoles().Length == 0)
                {
                    Roles.CreateRole("admin");
                }
                if ((Membership.GetUser("admin") == null || Membership.GetUser("Admin") == null) && Membership.GetAllUsers().Count == 0)
                {
                    lnk.Text        = "<br>کاربر Admin ايجاد نشده است<br/>براي ايجاد کاربر اينجا را کليک کنيد.";
                    lnk.NavigateUrl = UniHttpHandler.HandlerPath + "?a=conf&b=" + HttpContext.Current.Request.Path + "&p=2";
                    lnk.ToolTip     = "کاربر Admin وجود ندارد";
                    control.Controls.Add(lnk);
                }
                else
                {
                    initialized = true;
                }
                return(true);
            }

            System.Configuration.Configuration conf = null;
            try
            {
                conf = ConfigUtility.GetConfigFile();
            }
            catch (Exception ex)
            {
                error = ex.ToString();
            }
            if (conf != null)
            {
                try
                {
                    foreach (ConfigurationLocation location in conf.Locations)
                    {
                        if (location.Path == UniHttpHandler.HandlerPath)
                        {
                            System.Configuration.Configuration lconf = location.OpenConfiguration();
                            SystemWebSectionGroup lweb = ConfigUtility.GetSystemWebSectionGroup(lconf);
                            lweb.Authorization.Rules.Clear();
                            AuthorizationRule rule = new AuthorizationRule(AuthorizationRuleAction.Allow);
                            rule.Users.Add("*");
                            lweb.Authorization.Rules.Add(rule);
                            lconf.Save();
                        }
                    }
                    //WebConfigInitializer.CreateHttpHandler(ConfigUtility.GetSystemWebSectionGroup(conf));
                    conf.Save();
                }
                catch (Exception ex)
                {
                    error = ex.ToString();
                }
            }
            //LinkButton btn = new LinkButton();
            //btn.Text = "تنظيمات امنيتي سايت اتجام نشده است<br/>براي انجام تنظيمات اينجا را کليک کنيد.";
            //btn.Click += new EventHandler( btnDoConfig_Click );
            //btn.CausesValidation = false;
            //btn.ID = "JcoSecDoConfigButton";
            //control.Controls.Add( btn );
            //control.Page.PreRender += new EventHandler( Page_PreRender );
            //initAddedButtons.Add( btn );

            lnk.Text        = "تنظيمات امنيتي سايت اتجام نشده است<br/>براي انجام تنظيمات اينجا را کليک کنيد.";
            lnk.NavigateUrl = UniHttpHandler.HandlerPath + "?a=conf&b=" + HttpContext.Current.Request.Path;
            lnk.ToolTip     = error;
            control.Controls.Add(lnk);
            return(false);
        }