戰地連結︰ Home My Flickr NBA.com About

2009年9月9日星期三

Disabling Time Synchronization under Virtual PC 2007

Overview

VPC with Virtual Machine Additions installed will automatically synchronize its time with host. Virtual PC Guy tells us how to disable it in his post.

Reference

Disabling Time Synchronization under Virtual PC 2007

Steps

  1. Shut down the VPC (I found that cannot use “Save state”)
  2. Open the .VMC file with notepad and add the "components" element in it:

    <integration>

      <microsoft>

        <mouse>

          <allow type="boolean">true</allow>

        </mouse>

        <!--Add the "components" element-->

        <components>

          <host_time_sync>

            <enabled type="boolean">false</enabled>

          </host_time_sync>

        </components>

  3. Save the .VMC file and restart VPC, done!

Associate XSD to XML file to enable IntelliSense

Overview

We .Net developers use IntelliSense extensively in our everyone work. I am happy using IntelliSense for XML files like web.config file. Then one day I come across a XML file with schema not available out-of-the-box from VS (think about NHibernate hbm files). How can I use IntelliSense with it?

Reference

XML Schemas dialog in Visual Studio 2008

Steps

To use schema(s) for a XML file

  1. Open a XML file in Visual Studio 2008
  2. In menu bar, select "XML" => "Schemas…"
  3. In the "XML Schemas" windows, select "Use this Schema" in the "Use" column next to your desired schema
  4. Click OK, done!

The "XML Schemas" windows will search for schema files (.XSD) in your project or in folder %VS 2008 install folder%\xml\Schemas. You can also manually add schemas files using the "Add…" button

Introduction to ASP.NET Forms Authentication

In ASP.NET, forms authentication means that users authenticate themselves using a Web form. This feature is provided by the HTTP module FormsAuthenticationModule. Setting up forms authentication in ASP.NET is quite simple and is presented in a post at WindowsDevCenter.com.

Reference

  1. ASP.NET Forms Authentication - Part 1
  2. Explained: Forms Authentication in ASP.NET 2.0 (MSDN)
  3. FormsAuthentication Class (MSDN)

Steps

Enable anonymous access in IIS.

Anonymous access is enabled by default. If not, enable it manually for the web application.

Modify web.config file to allow Forms Authentication

To allow Forms Authentication, first we have to add the "authentication" element under "system.web" in web.config.

  1. In the "authentication" element, set the "mode" attribute to “Forms” to specify Forms Authentication.
  2. Add a "forms" element under "authentication" element to specify configuration settings for Forms Authentication
  3. Add a "authorization" element under "authentication" element to deny all anonymous users and redirect them to login page.

Code Snippet (web.config):

<configuration>

  <system.web>

    <authentication mode="Forms">

      <forms name="DEMO"

            loginUrl="login.aspx"

            protection="All"

            timeout="30"

            path="/" />

    </authentication>

    <authorization>

      <deny users="?" />

    </authorization>

  </system.web>

</configuration>

Create the login page

The login page is where denied users will be redirected to. It is referenced by "loginUrl" attribute in "forms" element. As shown by the above code snippet, our login page will be “login.aspx”.

Code Snippet (login.aspx.cs)

protected void Login_Click(Object sender, EventArgs E)

{

    if ((UserName.Value == "username") &&

        (UserPass.Value == "password"))

    {

        FormsAuthentication.RedirectFromLoginPage(UserName.Value, PersistCookie.Checked);

    }

    else

    {

        lblResults.Text = "Invalid Credentials: Please try again";

    }

}

The above code snippet is the onClick event of the login button. When it is fired, it will:

  1. Validate the user credentials entered. In this case the validation logic is completely provided by us and only allow one user to login.
  2. If the user is valid, FormsAuthentication.RedirectFromLoginPage() is called to authenticate the user and redirect back to the page he/she wants to visit.
  3. Else a warning message is shown.

Configure user credentials in web.config

Usually we will store the user credentials in a Database. However for easy implementation (or for testing purpose) we can also set user credentials in web.config file.

By adding "credentials" element under "forms" element, we can add user credentials in username/password pair format.

<forms>

    <credentials passwordFormat="Clear">

      <user name="user1" password="password1"/>

      <user name="user2" password="password2"/>

      <user name="user3" password="password3"/>

    </credentials>

</forms>

Then we can use FormsAuthentication.Authenticate() method to validate user credentials against those stored in web.config.

2009年9月3日星期四

ASP.NET MVC - Get Started

Overview

I just started learning ASP.NET MVC, and would like to list some learning materials links here.

Reference

Official ASP.NET MVC Tutorials - http://www.asp.net/learn/mvc/

Download "Microsoft Web Platform Installer" to get ASP.NET MVC - http://www.microsoft.com/web/downloads/platform.aspx

NerdDinner at CodePlex - http://nerddinner.codeplex.com/

NerdDinner Site - http://www.nerddinner.com/

2009年7月22日星期三

Installing Driver for Legacy Network Adapter on Win2k3 x64 VPC (Hyper-V)

Overview

I installed a 64-bit Windows 2003 R2 Enterprise SP2 on one of my Hyper-V VPC, and add a Legacy Network Adaptor to it. However the network adapter is not automatically installed in the x64 win 2k3 because its driver is not available.

With some Googling I found that 64-bit version of win 2k3 and xp are not supported with corresponding Legacy Network Adaptor driver. Someone suggested a workaround by using the equivalent Vista driver (both x86 and x64).

So I go the driver files from a machine running on x86 Vista and tried it. But the driver installation terminated with Error: "Driver not intended for this platform".

Suspecting this maybe due to the x86/x64 difference, I got another set of driver files from a x64 Windows Server 2008. This time the driver installation ran smoothly and the Network Adaptor works well!

Directory containing Legacy Network Adaptor driver:

Vista:
%windir%\system32\driverstore\FileRepository\dc21x4vm.inf_7d8c6569

Windows Server 2008:
%windir%\system32\driverstore\FileRepository\dc21x4vm.inf_e14caac7

Reference

Windows XP x64 in Hyper-V - Network Drivers

2009年7月20日星期一

ASP.NET User Control - Part 1

Overview

User Control is quite useful in creating a customized control which can be reused throughout pages. Instead of creating a custom server control, developers can create user control which (in my opinion) is easier to create and modify the control layout.

In this part, I will talk about how to create a user control and include it in an .aspx page.

Reference

ASP.NET User Controls Overview

How to: Include a User Control in an ASP.NET Web Page

Steps

Create a User Control

Creating a user control is very simple with few steps:

  1. You need to have a Web Application Project
  2. In Solution Explorer, right-click on the project's name (or a folder), select "Add" => "New Item..."
  3. Select "Web User Control" under "Web" category, give the control a name and click "Add"
  4. Done!

The newly created user control consists of a .ascx (not .aspx) page and a code behind file (just ignore the designer file). The .ascx page currently contains nothing but a "@ Control" directive.

User control .ascx page is very much like a snippet of an .aspx page, you can modify the layout of the user control by adding HTML tags or ASP.NET controls in it just like what you do to .aspx page. Below is the code of a very simple .ascx page:

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="TestUserControl.ascx.cs" Inherits="MyTestWeb.Web.Controls.TestUserControl" %>

 

<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label><br />

<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><br />

<asp:Button ID="BtnSubmit" runat="server" Text="Submit" />

 

Include a User Control in an .aspx page

After a User Control is created, it can be used by any .aspx page within the same Web Application by including the control into the page.

To Include a User Control, we have to do 2 things:

  1. Create a "@ Register" directive which specify the "TagPrefix", "TagName" and "Src" (Source file) of the User Control.
  2. Declare the User Control with tag "<TagPrefix:TagName   />" just like any other ASP.NET control (the difference is ASP.NET Control start with TagPrefix "asp")

Below shows the code of a simple .aspx page including the User Control we just created.

<%@ Page Language="C#" MasterPageFile="~/Pages/Global.Master" AutoEventWireup="true"

    CodeBehind="TestPageWithMaster.aspx.cs" Inherits="MyTestWeb.Web.Pages.TestPageWithMaster"

    Title="Untitled Page" %>

 

<%-- "@ Register" directive --%>

<%@ Register TagPrefix="uc" TagName="TestUserControl" Src="~/Controls/TestUserControl.ascx" %>

 

<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">

</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">

    <form runat="server">

 

        <%-- User Control declaration --%>

        <uc:TestUserControl ID="TestUserControl1" runat="server" />

 

    </form>

</asp:Content>

2009年7月8日星期三

Client side validation skipped if submit Button have "onClientClick" JavaScript function

Overview

I have a form using ASP.NET validation controls, I have been using these ASP.NET validators for quite a time and they behaved well. However the problem come when I added a "onClientClick" event handler to the form submit button, and then the validation is just skipped and the form post back every time.

The problem is that the "onClientClick" event get fired before the validation so the validation is skipped. To solve this, we can use the JavaScript function "Page_ClientValidate()" to trigger the client side validation.

Reference

asp:Button Validation with OnClientClick javascript - Not Validating

ASP.NET Validation in Depth

Code Snippet

You can tell "Page_ClientValidate()" to only validate controls of a particular validation group by passing the validation group name as parameter.

function BtnSubmitButton_click()

        {

            if (Page_ClientValidate("ValidationGroup"))

            {

                return confirm("Submit this form?");

            }

            return false;

        }