• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 166
  • Last Modified:

Dynamic Global Variable managed by Site Admin

I need to allow a Site Administration User to set a flag which will determine whether or not the website is currently accepting CC payments.  If flag = true, then allow access to those pages.  If flag = false, then don't.

I really don't want to have a table in my DB for this one flag.  it seems like total overkill.  But i also can't think of another, simpler way to do it.  I was thinking about maybe creating an XML file to handle it.   I also thought about doing this in the web.config (since that is basically an XML file already).  That would mean that that particular <appsetting> would be dynmically set by the Site Admin.  I read about this online and it doesn't sound like very many people think that is such a good idea.  Any suggestions?
0
davidcahan
Asked:
davidcahan
  • 4
  • 4
1 Solution
 
McExpCommented:
I would argue that in your instance where you are, in effect, changing the settings of your application, that appSetting is the correct place for this flag. The reason people avoid appSettings normally is because it forces the app to reset. However this could be considered benificial in your situation. I could imaging the scenario where someone has an active session with your site. Then you turn off credit cards, however someone could already be in one of your pages you want to disallow, then when they complete it will still process. However if the app has been reset and the session invalidated they will be forced out of your page and back to the start.

Does that make sense?

The only issue with programtically modifying the web.config is it cuases an app reset, but since you are making a specific change to your site it wouldn't hurt if this happend!
0
 
davidcahanAuthor Commented:
that makes total sense...is there perhaps a "kinder, gentler" way to reset the app for user with already active sessions?  

Imagine a situation where the flag is in the DB.  Obviously i would hit the DB when the user attempts to hit the CC pages.  Obviously, the app wouldn't need to reset and i could still handle users with active sessions.  BUT i really really hate the idea of creating a 1 column, 1 row table just for this.   How about my other idea?  Use a "site" config XML file.  I know that the web.config is used for this, but if i use my own XML file i don't have to worry about the site resetting.
0
 
McExpCommented:
Yea, I've been considering using a custom xml file for configurable app settings for one of my sites. This is the ideal compromise (in my opinion), I'm just checking that I can't reuse the Configuration Class against my own custom XML file before I begin coding something from scratch.

If I find some time over the weekend I might have a think about developing it.
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
davidcahanAuthor Commented:
let me know if you do and how it comes out
0
 
davidcahanAuthor Commented:
Excellant Explanation.  Clear, simple and to the point
0
 
davidcahanAuthor Commented:
McEXP

Here's what i came up with:

I have a file at the root of the project called CustomAppSettings.xml.  Here's a sample node

<Root>
  <AppSettings>
    <Name id="AllowCCProcessing">False</Name>
  </AppSettings>
</Root>

I have a custom class called SiteAccess.  In site access i have a method called restricCCAccess:

Public Sub RestrictCCAccess(ByVal Redirect As Boolean, ByVal ErrorMessage As Boolean)
        m_ErrorMessage = ""
        Dim ProcessCC As Boolean
        Dim xmldoc As New XmlDocument
        Dim childNode As XmlNode

        xmldoc.Load(HttpContext.Current.Server.MapPath(".") & "\CustomAppSettings.xml")
        childNode = xmldoc.SelectSingleNode("//Root/AppSettings/Name[@id='AllowCCProcessing']")
        ProcessCC = CBool(childNode.InnerText)


        If ProcessCC = False Then
            If Redirect = True Then
                HttpContext.Current.Response.Redirect("AccessRestrictedCC.aspx")
            End If

            If ErrorMessage = True Then
                m_ErrorMessage = "Your Account does not give you access to this page"
            End If

        End If
      End Sub
0
 
McExpCommented:
So, I found some time, I thought you might be interested in my interpretation of your solution. I've included an example page which will output the current setting and allow the update of the configuration. The first time the page is run, there will not be a config file, so you get the defualts after you have clicked on one of the linkButtons it will save the values down to the config and all future requests will come from your config file.

--- App_Code\CustomConfig.cs ---
using System.Web;
using System.IO;
using System.Xml.Serialization;

/// <summary>
/// Summary description for CustomConfiguration
/// </summary>
public class CustomConfiguration
{
    private bool _AllowCCProcessing;

      public bool AllowCCProcessing
      {
            get { return _AllowCCProcessing;}
            set { _AllowCCProcessing = value;}
      }

    public CustomConfiguration()
    {

    }

    public void Save (string ConfigPath)
    {
        using (Stream ToStream = File.Create(HttpContext.Current.Server.MapPath(ConfigPath)))
        {
            XmlSerializer serilaliser = new XmlSerializer(this.GetType());
            serilaliser.Serialize(ToStream, this);
        }
    }

    public static CustomConfiguration LoadConfig (string ConfigPath)
    {
        string AbsoluteConfigLocation = HttpContext.Current.Server.MapPath(ConfigPath);
        if (File.Exists(AbsoluteConfigLocation))
        {
            using (Stream FromStream = File.OpenRead(AbsoluteConfigLocation))
            {
                XmlSerializer serialaliser = new XmlSerializer(typeof(CustomConfiguration));
                return (CustomConfiguration)serialaliser.Deserialize(FromStream);
            }
        }
        else
        {
            return new CustomConfiguration();
        }
    }
}

--- Default.aspx (Demo TestPage) ---
<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">
   
    CustomConfiguration cs = CustomConfiguration.LoadConfig("~/my.config");
   
    protected void LinkButton1_Click(object sender, EventArgs e)
    {
        cs.AllowCCProcessing = true;
        cs.Save("~/my.config");
    }

    protected void LinkButton2_Click(object sender, EventArgs e)
    {
        cs.AllowCCProcessing = false;
        cs.Save("~/my.config");
    }

    protected void Page_PreRender(object sender, EventArgs e)
    {
        lblCurrentSetting.Text = cs.AllowCCProcessing.ToString();
    }
   
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            Current CC Setting:<asp:Label runat="server" ID="lblCurrentSetting" /><br />
            Set CC Config to <asp:LinkButton ID="LinkButton1" runat="server" OnClick="LinkButton1_Click">true</asp:LinkButton> or
            <asp:LinkButton ID="LinkButton2" runat="server" OnClick="LinkButton2_Click">false</asp:LinkButton>
        </div>
    </form>
</body>
</html>
0
 
McExpCommented:
I've just noticed you develop in vb.net, here is an example of how to call the class using VB

--- VbDefault.aspx (VB Demo test page) ---
<%@ Page Language="VB" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

    Dim cs As CustomConfiguration = CustomConfiguration.LoadConfig("~/my.config")
   
    Protected Sub LinkButton1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        cs.AllowCCProcessing = True
        cs.Save("~/my.config")
    End Sub

    Protected Sub LinkButton2_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        cs.AllowCCProcessing = False
        cs.Save("~/my.config")
    End Sub
   
    Protected Sub Page_PreRender(ByVal sender As Object, ByVal e As System.EventArgs)
        lblCurrentSetting.Text = cs.AllowCCProcessing.ToString()
    End Sub
   
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
            Current CC Setting:<asp:Label runat="server" ID="lblCurrentSetting" /><br />
            Set CC Config to <asp:LinkButton ID="LinkButton1" runat="server" OnClick="LinkButton1_Click">true</asp:LinkButton> or
            <asp:LinkButton ID="LinkButton2" runat="server" OnClick="LinkButton2_Click">false</asp:LinkButton>
    </div>
    </form>
</body>
</html>
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

  • 4
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now