Solved

Dynamic Global Variable managed by Site Admin

Posted on 2007-11-16
8
154 Views
Last Modified: 2012-06-27
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
Comment
Question by:davidcahan
  • 4
  • 4
8 Comments
 
LVL 16

Accepted Solution

by:
McExp earned 250 total points
ID: 20303996
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
 

Author Comment

by:davidcahan
ID: 20304163
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
 
LVL 16

Expert Comment

by:McExp
ID: 20305491
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
 

Author Comment

by:davidcahan
ID: 20305686
let me know if you do and how it comes out
0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 

Author Closing Comment

by:davidcahan
ID: 31409700
Excellant Explanation.  Clear, simple and to the point
0
 

Author Comment

by:davidcahan
ID: 20306088
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
 
LVL 16

Expert Comment

by:McExp
ID: 20307771
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
 
LVL 16

Expert Comment

by:McExp
ID: 20307790
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

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

I have developed many web applications with asp & asp.net and to add and use a dropdownlist was always a very simple task, but with the new asp.net, setting the value is a bit tricky and its not similar to the old traditional method. So in this a…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This video discusses moving either the default database or any database to a new volume.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now