Archive

Posts Tagged ‘.Net Application’

Centralizing Users’ Authentication at Active Directory Level

December 23, 2011 Leave a comment

First of all, sorry for my long absence.

One month ago, I had a talk at TechInsights South East Asia, 2011 related to one of my favorite security topics for centralizing user management. I thought why not share it with you guys.

I hope you enjoy it.

Peace and love …

MS Chart Part 3 (Combining Different Chart-Types)

June 28, 2011 9 comments

Sometimes you may need to combine two or more different Chart-Types. In this article we go through the stages of creating a composite MS Chart. In this project we make a Pareto function to show how to combine two different chart types in a single ChartArea.

Pareto chart: A Pareto chart, named after Vilfredo Pareto, is a type of chart that contains both bars and a line graph, where individual values are represented in descending order by bars, and the cumulative total is represented by the line (In probability distribution there are two ways to display distribution in chart, PMF (Probability Mass Function) and CDF (Cumulative Distribution Function) that can be used to display both in a single area).
The purpose of the Pareto chart is to highlight the most important among a (typically large) set of factors. In quality control, it often represents the most common sources of defects, the highest occurring type of defect, or the most frequent reasons for customer complaints, and so on. Wilkinson (2006) devised an algorithm for producing statistically-based acceptance limits (similar to confidence intervals) for each bar in the Pareto chart. The Pareto chart is one of the seven basic tools of quality control.

After a short introduction to Pareto Chart, lets start the development by starting a new project from scratch. (The reference is Microsoft MSChart sample project)

Pareto MS Chart

Pareto Chart

ASPX File:

<asp:Chart ID="Chart1" runat="server" Width="412px" Height="296px" BorderlineDashStyle="Solid" Palette="BrightPastel" BackSecondaryColor="White" BackGradientStyle="TopBottom" BorderWidth="2" BackColor="WhiteSmoke" BorderColor="26, 59, 105">

<Legends>

<asp:Legend Enabled="False" IsTextAutoFit="False" Name="Default" BackColor="Transparent" Font="Trebuchet MS, 8.25pt, style=Bold">

</asp:Legend>

</Legends>

<BorderSkin SkinStyle="Emboss"></BorderSkin>

<Series>

<asp:Series Name="Default" BorderColor="180, 26, 59, 105">

</asp:Series>

</Series>

<ChartAreas>

<asp:ChartArea Name="ChartArea1" BorderColor="64, 64, 64, 64" BackSecondaryColor="White" BackColor="Gainsboro" ShadowColor="Transparent" BackGradientStyle="TopBottom">

<AxisY2 IsLabelAutoFit="False" Interval="25">

<LabelStyle Font="Trebuchet MS, 8.25pt, style=Bold" />

</AxisY2>

<AxisY LineColor="64, 64, 64, 64">

<LabelStyle Font="Trebuchet MS, 8.25pt, style=Bold" />

<MajorGrid LineColor="64, 64, 64, 64" />

</AxisY>

<AxisX LineColor="64, 64, 64, 64">

<LabelStyle Font="Trebuchet MS, 8.25pt, style=Bold" />

<MajorGrid LineColor="64, 64, 64, 64" />

</AxisX>

</asp:ChartArea>

</ChartAreas>

</asp:Chart>

In the first section there is only one chart type added to the ChartArea which is a Column Chart namely Default. We add the second chart in the code behind later. I also set some of the appearance properties in asp tag to make it look better.

Code Behind:

protected void Page_Load(object sender, EventArgs e)

{

AfterLoad();

}

private void RandomData(Series series, int numOfPoints)

{

Random rand;

// Use a number to calculate a starting value for

// the pseudo-random number sequence

rand = new Random(5);

// Generate random Y values

for (int point = 0; point < numOfPoints; point++)

{

series.Points.AddY(rand.Next(49) + 1);

}

}

public void AfterLoad()

{

// Number of data points

int numOfPoints = 5;

// Generate rundom data

RandomData(Chart1.Series["Default"], numOfPoints);

// Make Pareto Chart

MakeParetoChart(Chart1, "Default", "Pareto");

// Set chart types for output data

Chart1.Series["Pareto"].ChartType = SeriesChartType.Line;

// set the markers for each point of the Pareto Line

Chart1.Series["Pareto"].IsValueShownAsLabel = true;

Chart1.Series["Pareto"].MarkerColor = Color.Red;

Chart1.Series["Pareto"].MarkerBorderColor = Color.MidnightBlue;

Chart1.Series["Pareto"].MarkerStyle = MarkerStyle.Circle;

Chart1.Series["Pareto"].MarkerSize = 8;

Chart1.Series["Pareto"].LabelFormat = "0.#";  // format with one decimal and leading zero

// Set Color of line Pareto chart

Chart1.Series["Pareto"].Color = Color.FromArgb(252, 180, 65);

}

void MakeParetoChart(Chart chart, string srcSeriesName, string destSeriesName)

{

// get name of the ChartAre of the source series

string strChartArea = chart.Series[srcSeriesName].ChartArea;

// ensure that the source series is a column chart type

chart.Series[srcSeriesName].ChartType = SeriesChartType.Column;

// sort the data in all series by their values in descending order

chart.DataManipulator.Sort(PointSortOrder.Descending, srcSeriesName);

// find the total of all points in the source series

double total = 0.0;

foreach (DataPoint pt in chart.Series[srcSeriesName].Points)

total += pt.YValues[0];

// set the max value on the primary axis to total

chart.ChartAreas[strChartArea].AxisY.Maximum = total;

// create the destination series and add it to the chart

Series destSeries = new Series(destSeriesName);

chart.Series.Add(destSeries);

// ensure that the destination series is either a Line or Spline chart type

destSeries.ChartType = SeriesChartType.Line;

destSeries.BorderWidth = 3;

// assign the series to the same chart area as the column chart is assigned

destSeries.ChartArea = chart.Series[srcSeriesName].ChartArea;

// assign this series to use the secondary axis and set it maximum to be 100%

destSeries.YAxisType = AxisType.Secondary;

chart.ChartAreas[strChartArea].AxisY2.Maximum = 100;

// locale specific percentage format with no decimals

chart.ChartAreas[strChartArea].AxisY2.LabelStyle.Format = "P0";

// turn off the end point values of the primary X axis

chart.ChartAreas[strChartArea].AxisX.LabelStyle.IsEndLabelVisible = false;

double percentage = 0.0;

foreach (DataPoint pt in chart.Series[srcSeriesName].Points)

{

percentage += (pt.YValues[0] / total * 100.0);

destSeries.Points.Add(Math.Round(percentage, 2));

}

}

There isn’t any specific chart type for Pareto; we should make it as a combination of two different chart types called Column and Line which is done in the code behind as described below.

Random():

This function is responsible to make 5 random values between 0 to maximum 49 which is called repeatedly in different sections of the project.

 

AfterLoad():

This method is called in Page_Load event handler which is responsible to handle the whole process. Firstly, it sets 5 random values to Default series which is a column chart type. Next, it calls MakeParetoChart()   (which is described later)to create a second line chart, then, it sets some appearance properties to the new generated series.

MakeParetoChart():

This method is responsible to add the second series called Pareto to the chart area. This series is a line chart for the pareto.

And yes it is over. Hope you found it interesting.
I am looking forward to reading your comments bout this post.

Web forms or ASHX (Handlers) (a short differentiation)

“What should I choose [Web-forms or Handlers] when I want a simple response from the server? “. After 6 years of development with C#.net I heard this question a lot. Due to this, I decide to answer it here.

Web forms VS ASHX

Fig 1.0

Assume that you are developing a JQuery AJAX based application that sends its requests to servers and receives a specific response from it. In this case the first solution that may strike you is using handlers. Maybe because of some features I list here:

–          It returns only a specific response; it is so light and is not derived from a heavy class that may have some performance penalties.

–          Unlike a web form it doesn’t need to compile its interface, generates HTML and returns the generated HTML to clients.

–          Unlike web services (mostly used before .Net 3.0), it returns almost all of request’s properties. One of the most prominent one is users’ identity.

Yes, using Handlers has many, many benefits but choosing to use them for such purpose may bring many problems as well.

In a standard web-form programming (I’d better say as a good OOP practice) you should implement a multi-layered

Web forms VS ASHX (Handlers)

Fig 2.0

inheritance for all of your web-forms. It is totally a beginner’s mistake to inherit a web-form directly from System.Web.UI.Page.

In this approach (Fig 2.0) you distribute each group of actions to separate layers. For instance, you place secure parts (handling authorized users) inside SecurityWeb class, non-secure parts placed into NonSecureWeb class etc. Finally, all of your customized classes can inherit from WebBase class which executes your common and shared operations. WebBase class is also derived from the built-in System.Web.UI.Page class.

Now, you have built a clean architecture for security, caching, common operations/properties and many more. Just assume that you want to enforce all of the functionalities you defined in your layered architecture take place in you AJAX handlers. Assuming that you handle the incoming requests with a handler class. A handler class that cannot inherit anything from a parent class. So there is almost no clean and neat way to have all of your efforts happen before executing the handler’s block. You have no choice to make external calls to other classes by passing heavy objects and parsing the return values!!

This is just duplicating many of your codes that results in inconsistent and non-secure consequences.

Better solution:
The alternative is to replace the handler class with a web-form one that inherits from an/other parent class/es. Now you can have all of the important actions happen in almost everywhere without any code duplication or security problem outcomes.

Indeed, Web forms are heavy, very heavy. Here I provided my common practice to decrease some weigh of this fatty class.

How to make web forms act close to handlers:

By adding the following code you can easily change the behavior of ASPX files to act similarly with ASHX files:

Response.Clear();
Response.ContentType = "text/html";
Response.Expires = -1;
Response.CacheControl = "no-cache";
Response.AddHeader("Pragma", "no-cache");
Line6: string response = "";</pre>
<pre>// your process over Request and generate the Response</pre>
<pre>Response.Write(response);
Response.End();

Above, is a sample that returns a text. Obviously, you can change it to any desired return value. Line 6 is where you should place your operation to feed the response variable.

You reached the end:). I should thank for your attention, deeply appreciate your sharing thoughts about this post and looking forward to receiving them here 😉

Umbraco – A new taste of development

Niels Hartvig

Niels Hartvig - Founder

An open source CMS but this time in C#. I saw many applications on the web (refer to http://csharp-source.net, http://csharpopensource.com/ or this one http://www.dotnetopen.net/) that offer many open source apps in .Net but can anyone of them run huge applications such as www.asp.net or wired.co.uk?

Umbraco is an open source CMS in C# which could run www.asp.net and wired.co.uk and far too many other enterprise web apps (claiming more than 85000 installations). Yes it is based on open source license and it is chosen by many organizations such as Toyota and Microsoft.

Niels Hartvig, is the founder of this CMS. He was a simple Danish man who started changing his world by developing his idea. And again a developer made it.

This CMS can be easily used freely to start a personal web site, a business web site, a forum or … and also can be used by developers to build their own customized CMS based on their needs.

It is 100% based on .Net and was coded in MVC. It can be opened easily in VStudio or WebMatrix (there is a template for Umbraco in WebMatrix) to make any alteration. Furthermore, it can be connected to MSSql Server or MySql Server. You can also code for your own DBMS. Moreover, you can plan it over Windows Azure. For all you need to know about development, they have an active forum talking about development issues or just see this video to be shocked about the power of this CMS.

Umbraco and Azure Logo

There is a really nice section in their web site called [Make A Wish], in which you can post any idea that you think is missed.

The last thing you should know is Umbraco’s base application is 100% free of charge unless you prefer it can be bundled with guaranteed and professional support, bug fixing warranty and productivity enhancing add-ons.

Why waiting so long, download it and start your own web ;))