Monday, 11 February 2013
Tuesday, 25 September 2012
ASP.NET Web Forms Application Paths Related
This article is going to list all ASP.NET Web Forms application paths related properties and their values on the Request object. Below the test result are based on this scenario: local hosted IIS site named VirtualSite, first we access to http://localhost/VirtualSite/Redirect/dummy.aspx and there is a HttpModule to redirect the request to http://localhost/VirtualSite/Redirect/Redirect.aspx. The request has parameters "a=1&b=2&c=3". So here it is:
| Request Property | Value |
| ApplicationPath | /VirtualSite |
| AppRelativeCurrentExecutionFilePath | ~/Redirect/Redirect.aspx |
| CurrentExecutionFilePath | /VirtualSite/Redirect/Redirect.aspx |
| FilePath | /VirtualSite/Redirect/Redirect.aspx |
| Path | /VirtualSite/Redirect/Redirect.aspx |
| QueryString | {a=1&b=2&c=3} |
| RawUrl | /VirtualSite/Redirect/Dummy.aspx?a=1&b=2&c=3 |
| Url.AbsolutePath | /VirtualSite/Redirect/Redirect.aspx |
| Url.AbsoluteUrl | http://localhost:25739/VitualSite/Redirect/Redirect.aspx?a=1&b=2&c=3 |
| Url.Authority | localhost:25739 |
| Url.DnsSafeHost | localhost |
| Url.Host | localhost |
| Url.LocalPath | /VitualSite/Redirect/Redirect.aspx |
| Url.OriginalString | http://localhost:25739/VitualSite/Redirect/Redirect.aspx?a=1&b=2&c=3 |
| Url.PathAndQuery | /VitualSite/Redirect/Redirect.aspx?a=1&b=2&c=3 |
| Url.Port | 25739 |
| Url.Query | ?a=1&b=2&c=3 |
| Url.Scheme | http |
Since there is a redirect, as you can see, only the RawUrl contains the very original request, all other values are for the redirected Url, event the Request.Url.OriginalString
Thursday, 6 September 2012
Do not mix using HtmlTextWriter and WebControl itself to set its attributes
What does it do? First in the Render method we set the class to "Disabled" if Enabled property is false. Later when AddAttributesToRender method fired during base.Render() it will attach more CSS classes on the web control. All look good, right? No. It won’t work when Enabled == false.
Why? Check what the base.AddAttributesToRender() in line 16 does first. The AddAttributesToRender method will copy every single attribute from the web control into the HtmlTextWriter for page rendering later. If the attribute name has already existed in the HtmlTextWriter then the value of this attribute is abandoned to avoid value overwritten. This rule doesn't apply for Style attribute. For Style attribute, the value will be appended at the end of existing style string.
Ok, in our case when Enabled == false, since we have already set the Class attribute equal to "Disabled" then no matter how many other CssClass values assign to the web control directly those values will be abandoned when base. AddAttributesToRender() called.
So here is my simple advice: see the title of this article. Since we can always set attributes value on web control level it is unnecessary to touch the HtmlTextWriter under any condition.
Friday, 20 April 2012
Uhuru Cloud - My First App
Uhuru Cloud support JAVA, .NET, php and other web applications but I'd like to start with the simplest one, my company website, a single html/js webpage.
To deploy app to Uhuru Cloud you will need either install a MMC (Microsoft Management Console) or Uhuru Visual Studio plugin. I choose MMC because the multi page user guide for installation of Visual Studio plugin is quite scary. Uhuru gives a very detailed PDF doc to help deploying app through MMC and the whole process is very straight forward.
One thing worth mentioning, when pushing your app to Uhuru CloudFoundry, it will automatically identify the suitable framework and runtime version for your app. In my case, since it is just a html/js site, CloudFoundry treat it as a php app (fair enough, compare with JAVA and IIS, php host cost less). While after couple of mouse clicking and filling one small windows form, my first app is officially online now. See here: wandj.uhurucloud.net
Saturday, 24 March 2012
Cheat Sheets for Web Development you must have
The original article is from: Cheat Sheets for Web Development you must have. Here I only copy those I am familiar with as a back up.
Monday, 2 January 2012
Tips of securing your ASP.NET MVC3 application
Tip 1, Always encode user input content before showing it on web page. You can do this simply by using Html.Encode() for innerHTML, Html.AttributeEncode() for attribute values and Url.Encode() for href link values.
You should know code expressions in Razor are implicitly HTML encoded, therefore in cases you do need to render some HTML value that should not be HTML encoded you can use the Html.Raw() method to ensure that the value is not encoded. BUT by doing so you have thrown open the door to evil.
Tip 2, Always JavaScript Encode user input values in your JavaScript code. You can use Ajax.JavaScriptStringEncode() to do this. The reason behind this is the html encode methods mentioned in Tip 1 cannot fully stop the XSS attacks because the html encoding cannot handle hex escape string but DOM does. Therefore the hacker may use your JS code to pass hex escape string into DOM object and the DOM object will convert the string back to JavaScript code and hurt you. Have a look the code below:
1: <div id="test">Text here is suppose to be replaced</div>
2: <script type="text/javascript">
3: $(function () {
4: $("#test").html(
5: '@Html.Encode("\\x3cscript\\x3e alert(\\x27oops\\x27) \\x3c/script\\x3e")');
6: });
7: </script>
Copy the code into your razor page and run it you will see an alert message says “oops”.
Tip 3, Follow the listed principles below to prevent CSRF attacks (if you don’t know what CSRF is please google it).
- First thing first, you need follow Tip1 and 2 to make you site XSS proof
- Always use POST instead of GET to submit data changes
- Validate HTTPReferrer by checking the HttpContext.Request.UrlReferrer to see if the client that posted the data was indeed from your site page
- Put token Verification in your form. You can use Html.AntiForgeryToken() to create a hidden token value in your form and in the related controller use ValidateAntiforgeryToken attribute to check if the token is matched
Tip 4, Protect your open redirection URL. The ASP.NET MVC 3 template includes code to protect against open redirection attacks in its AccountController after user successfully log on. You need also do similar thing in other places where a potential URL redirection may occur.
Tip 5, Use the AuthrizeAttribute to require a login access or role membership access. AuthrizeAttribute can be used on Action level or Controller level. You can use FormsAuthentication.SetAuthCookie() to keep the user login info into http cookie and it can be read by calling User.Identity.IsAuthenticated() in the requested Controller.
Tip 6, Use ValidationAttibute to do view-model level data value validation automatically. Most of the ValidationAttributes support client side validation if related jQuery lib file included which is a huge benefit for us developers.
Tip 7, Prevent over posting data through ASP.NET MVC3 model binding feature which can allow your attacker an opportunity to populate model properties you didn’t even put on your input forms. You can use BindAttribute to specify the name of properties which allow auto model binding. Alternatively you can do a manual data binding by calling UpdateModel() or TryUpdateModel() methods in the controller.
Still another way to deal with over-posting is to avoid binding directly to the data model. You can
do this by define a View-Model class that holds only the properties you want to allow the user to set.
Sunday, 16 October 2011
Dynamic HttpModule registration in .NET Framework 4.0
This article could be useful for the scenario that you are developing HttpModule handling related functionality for a web application and you do not have the right to or not intend to modify the Global.asax file to register your HttpModule handler.
So here we go. Step 1, before register a HttpModule handler dynamically the first thing is to create the handler. See code below:
public class MyDynamicModule : IHttpModule
{
void IHttpModule.Dispose()
{
}
void IHttpModule.Init(HttpApplication context)
{
context.BeginRequest += new EventHandler(context_BeginRequest);
context.EndRequest += new EventHandler(context_EndRequest);
}
//... more code ignored
}
Step 2, to register this HttpModule handler, we need the reference to Microsoft.Web.Infrastructure assemble and use the contained method DynamicModuleUtility.RegisterModule. See sample code below:
DynamicModuleUtility.RegisterModule(typeof(MyDynamicModule));
But where we call this line of code? As you know HttpModule registration has to be done before the instance of HttpAppliction start. To achieve that we need the newly introduced attribute class System.Web.PreApplicationStartMethodAttribute in .NET framework 4.0. Note this attribute is assembly level attribute. See the code below, as step 3 of our example, of how to use the attribute:
[assembly: PreApplicationStartMethod(typeof(DynamicHttpModule), "RegisterMyModule")]
The code above basically says I want the web application to do something in its pre-start event, which is, to execute the static method DynamicHttpModule.RegisterMyModule, see below the code of the DynamicHttpModule class:
public static class DynamicHttpModule
{
public static void RegisterMyModule()
{
DynamicModuleUtility.RegisterModule(typeof(MyDynamicModule)); //code from step 2
}
}
And that is it.
Wednesday, 12 October 2011
Controlling XML Serialization Using Attributes
The article is mainly focus on mapping the object type name with the xml file node name when De-serializing the xml file and when the node name is not a proper C# type name
Long story short, here are some brief ideas:
- Mapping xml root element name with class name: XmlRootAttribute
- Mapping element name other than the root with class property/field name: XmlElementAttribute
- Mapping List of elements underneath the parent node, normally to a ArrayList (or generic List<T>): XmlArrayAttribute and XmlArrayItemAttribute
- Mapping a xml attribute to a C# class property/field: XmlAttribute
More details you can check the Microsoft article: Controlling XML Serialization Using Attributes
Friday, 7 October 2011
Control.BeginInvoke() vs Control.Invoke()
The BeginInvoke() method has a IAsyncResult type return value and the Invoke() method simply return a object. This makes a big difference between them which is the BeginInvoke() is making a asynchronous callback to the thread created the handle of the control.
Assuming the main thread is A and currently executing code is in thread B, if call Invoke() from thread B then thread B has to wait for the callback to thread A being finished and then continue the following process.
Tuesday, 27 September 2011
Tuesday, 7 June 2011
Ways of passing data between WP7 pages
- add a public propery on App class to retain the data during current lifecycle;
- add a public propery on the target page, and then in the prev page's OnNavigatedFrom event set the value of the property for the next page;
- use the property State from the application's current PhoneApplicationService instance, which is a type of generic dictionary. But the object been stored has to be serializable (the reason is when app goes to deactivated it will serialize the objects to isolated storage);
- isolate storage. BUT obviously it is not wise to frequently access isolate storage during application running. Here is a great article of compairing the performance of using isolate storage http://blogs.claritycon.com/kevinmarshall/2010/11/03/wp7-serialization-comparison/
Friday, 18 February 2011
How Secure Are Query Strings Over HTTPS?
- URLs are stored in web server logs,
- URLs are stored in the browser history,
- URLs are passed in Referrer headers
Monday, 19 July 2010
Developing on SharePoint 2010 (Day 2)
a) SharePoint Server Architecture
b) SharePoint Object Hierarchy
So as you can see the SharePoint Object model is relatively straightforward. Here we have a very simple slip of code which start from reaching all services of the Farm and navigate through to the item and file level objects.
1: foreach (SPService svc in SPFarm.Local.Services)
2: {
3: if (svc is SPWebService)
4: {
5: foreach (SPWebApplication webApp in ((SPWebService)svc).WebApplications)
6: {
7: foreach (SPSite site in webApp.Sites)
8: {
9: foreach (SPWeb web in site.AllWebs)
10: {
11: foreach (SPList list in web.Lists)
12: {
13: foreach (SPListItem i in list.Items)
14: {
15: // do something
16: }
17: }
18:
19: foreach (SPFile f in web.Files)
20: {
21: // do something
22: }
23: }
24: }
25: }
26: }
27: }
Friday, 9 July 2010
Oracle Procedures (version 11.1)
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Temporary Tables vs. Table Variables
| Temp Table | Table Variable | |
| record transaction logs? | Yes | No |
| inside transaction scope? | Yes | No |
| cross execution batch? | Global temp table | No way |
| support index? | Yes | No |
| where to store? | Tempdb | Memory |
Thursday, 8 July 2010
Developing on SharePoint 2010 (Day 1)
This article is talking about developing web parts for SharePoint 2010 using Visual Studio 2010. If you are looking for articles for older version of SharePoint or Visual Studio I recommend this URL: http://www.aspfree.com/c/a/Windows-Scripting/Beginning-SharePoint-Web-Part-Development/
First of all, there are two types of web parts VS 2010 supports: Standard Web Part (which in VS 2010 Item Template list named Web Part) and Visual Web Part.
A standard web part is just a class inherited from abstract class WebPart. Below is the Inheritance Hierarchy of WebPart class.
1: WebControl
2: |---Panel
3: |---Part
4: |---WebPart
To add contents into standard web part we need override the CreateChildControls method and inside put code logic to create sub controls manually and add them into the Controls collection. See the code below:
1: protected override void CreateChildControls()
2: {
3: LiteralControl msg = new LiteralControl("test");
4: this.Controls.Add(msg);
5: }
The visual web part, in the other hand, give you a visual design UI to drag existed web control in. But if you check the source code, a visual web part is just a standard web part plus a Web.UI.UserControl. And in the overrided CreateChildControls method used a trick like this:
this.Controls.Add(Page.LoadControl(_controlPath));
One more thing for visual web part: the value _controlPath is the physical URL of the user control and the value of it is maintained by VS 2010.
Monday, 10 May 2010
How to convert UTF-8 to urlencoded string
As you know, System.Web.HttpUtility.UrlEncode can easily convert UTF-8 string into the format of %xx%xx so it can be used in a URL string. Now imagine you are not working on a ASP.NET project and you don’t want to add reference of System.Web.dll in your project, then how can you do the same thing? Below is one solution I found:
1: private static string GetEncodedString(string username)
2: {
3: StringBuilder sb = new StringBuilder();
4: byte[] btyes = System.Text.Encoding.Default.GetBytes(username);
5: for (int i = 0; i < btyes.Length; i++)
6: {
7: sb.Append(@"%" + Convert.ToString(btyes[i], 16));
8: }
9:
10: return sb.ToString();
11: }
Now if you pass in chinese string 你好 it will turn out %c4%e3%ba%c3, which is exactly what I want. The reason I create this method is because I am trying to use HttpWebRequest component to directly post data to a web page and I need a way to submit UTF-8 string.
BTW, the only difference between my method and UrlEncode is my method will convert all characters in the passed in string to %xx format even it is ASCII but UrlEncode won’t do that.
Monday, 12 April 2010
Star Schema, Snowflake Schema and others
Star schema resembles a star, one or more fact tables are surrounded by single level dimension tables (one completely de-normalized table per relationship). Dimension tables aren't normalized - that means even if you have repeating fields such as name or category no extra table is added to remove the redundancy.
Snowflake schema resembles a snowflake because comparing with Star Schema the dimension tables are further normalized or have parent tables.
Either way the fact tables are usually designed in third normal form because all data depends on either one dimension or all of them, not on combinations of a few dimensions.
Star Schema vs. Snowflake Schema
The reason for using a star schema is its simplicity from the users' point of view: queries are never complex because the only joins and conditions involve a fact table and a single level of dimension tables, without the indirect dependencies to other tables that are possible in a better normalized snowflake schema.
Snowflake schema generates more joins than a star schema during, which translates into longer queries. Therefore it is normally recommended to choose the star schema design over the snowflake schema for optimal performance.
Snowflake schema does have an advantage of providing more flexibility.
Disadvantage of Star Schema
A well designed database has to be in 3NF whereas the star schema's dimensions are de-normalized. Normally a huge number of redundant data existed in dimension tables.
For improving query performance many pre-processes (sum, grouping, resort etc) have been done on the dimension’s purpose and saved in the dimension table. When business logic changed to modify the related pre-processes are always hard.
When create new dimension table and build relationship with fact table, it can cause geometrically fact table size increasing.
How to Index Star/Snowflake Schema Data
Declare a primary key in each dimension table.
For star dimensions, declare foreign key (FK) relationships between each dimension table and the corresponding fact table. Additionally, for snowflake dimensions, declare FK relationships between each secondary dimension table and the primary dimension table that it augments.
Make sure that there is a primary key associated with the fact table(s), even if you have to use an IDENTITY field.
Declare indexes on each of the primary keys in the dimension tables and in the fact table(s).
Declare indexes on each of the foreign keys in the fact table(s).
Sunday, 14 March 2010
WTF: UAC and rsAccessDenied error in Windows 2008 R2 (x64)
Here is today’s WTF, imaging you spend whole day installed SSRS and finally start creating your first report service, but when you are trying to deploy it you keep receiving error message regarding denied accessing, it is really confused me.
This is not the worst part yet, if you google the error type [rsAccessDenied] you can find the solution is accessing http://localhost/reports/ and assign proper user roles to the report service login user. But when login http://localhost/reports/ in Windows 2008 R2, you can see only a blank home page without any settings you can work with. This is because UAC blocked the dynamic content in this page. Even I added the URL to trusted site list, you can still only see a blank page, which really piss me off.
To overcome this issue, you have to turn off UAC and restart the computer, and then access http://localhost/reports/ you can see all the functionality you need.
Followed are some tips installing SSRS 2008 x64 on Windows 2008 R2 x64
First thing first I strongly suggest installing VS2008, SQL SERVER 2008 in the default path, otherwise you will suffer installation failure soon or later, especially for the Business Intelligence Development Studio.
Another thing you need know is Report Service is no longer relying on IIS. Even you stopped IIS service you can still access Report Service from URL http://localhost/Reports and http://localhost/ReportService.
Assuming you have already installed SSRS 2008 with SQLSERVER 2008, next step is configure database connection. To do this go to Microsoft SQL Server 2008/Configuration Tools/Reporting Service Configuration Manager, then click database page and connect the database server you want. The reporting service manager will create two databases ReportServer and ReportServerTempDB for itself.
Another thing you may need to know is the SSRS report web service asmx URLs are:
http://localhost/ReportServer/ReportService2005.asmx (to manage the service)
http://localhost/ReportServer/ReportExecution2005.asmx (to create reports)
Even you installed MSSQL2008 the services are still named 2005, isn't it kind of WTF. :)
Monday, 8 March 2010
SILVER - An easy way to remember asp.net lifecycle
I – Initialize
L – Load
V – Validate
E – Event Handling
R – Render
Haha,really easy to remember.







