Labels

Wednesday, May 16, 2007

Inline Server Function Call

Note:  Use ‘#’ instead of ‘=’

 

<form id="form1" runat="server">

    <div>

        <asp:Label ID="Label1" runat="server" Text="<%# GetTime() %>"></asp:Label>

    </div>

 </form>

 

protected void Page_Load(object sender, EventArgs e)

    {

        Label1.DataBind();

    }

 

    public string GetTime()

    {

        return DateTime.Now.ToString();

    }

 

Thanks & Regards,

Arun Manglick

SMTS || Microsoft Technology Practice || Bridgestone - Tyre Link || Persistent Systems || 3023-6258

 

DISCLAIMER ========== This e-mail may contain privileged and confidential information which is the property of Persistent Systems Pvt. Ltd. It is intended only for the use of the individual or entity to which it is addressed. If you are not the intended recipient, you are not authorized to read, retain, copy, print, distribute or use this message. If you have received this communication in error, please notify the sender and delete all copies of this message. Persistent Systems Pvt. Ltd. does not accept any liability for virus infected mails.

Tuesday, May 15, 2007

Trim your page size

Trim your page size

 

Not something we think about much - but becomes important when clients are connecting over a slow network. Reducing the page size can reduce the waiting time for the client for the page to get rendered. Ways to reduce the page size:

 

a. Remove extra spaces, new lines, tabs - This is something that goes against good coding pratices actually, but will help reducing the page size

 

b. Use script includes for static javascripts so that they can be cached for subsequent requests.

c. Limit the use of graphics, consider using compressed graphics

d. Use CSS for styling to avoid sending same formatting directives to the client repeatedly

e. Avoid long control names! (Ever given it a thought? :))

f. Use ASP .NET's server side comments in the page. The syntax is:

 

·         The key difference is that with client-side comments it is the browser which is ignoring the content within them.  Code/controls within client-side comments will still be executed on the server and sent down to the browser.  As such, if there is a server error caused within them it will block running the page.

·         With server-side comments, the ASP.NET compiler ignores everything within these blocks at parse/compile time, and removes the content completely when assembling the page.

·         Consequently, any errors caused by mal-formed controls or issues with inline code or data-binding expressions within them will be ignored.  The page is also just as fast with controls/code within server-side comments as if there were no controls/code on the page at all (there is no runtime performance overhead to them).

 

 

 

Thanks & Regards,

Arun Manglick

SMTS || Microsoft Technology Practice || Bridgestone - Tyre Link || Persistent Systems || 3023-6258

 

DISCLAIMER ========== This e-mail may contain privileged and confidential information which is the property of Persistent Systems Pvt. Ltd. It is intended only for the use of the individual or entity to which it is addressed. If you are not the intended recipient, you are not authorized to read, retain, copy, print, distribute or use this message. If you have received this communication in error, please notify the sender and delete all copies of this message. Persistent Systems Pvt. Ltd. does not accept any liability for virus infected mails.

Avoid DataBinder.Evall(): Uses reflection

Hi,

Technically, the DataBinder.Evall() method uses reflection when evaluating the data item to find a property with a certain name. You do pay a performance penalty when you use reflection.

<%# DataBinder.Eval(Container.DataItem, "Title") %>

As an alternative, you can improve the performance of your DataBinding expressions by casting the data items to a particular type like this:

<%# CType(Container.DataItem, System.Data.DataRowView)("Title")%>

 

 

Thanks & Regards,

Arun Manglick

SMTS || Microsoft Technology Practice || Bridgestone - Tyre Link || Persistent Systems || 3023-6258

 

DISCLAIMER ========== This e-mail may contain privileged and confidential information which is the property of Persistent Systems Pvt. Ltd. It is intended only for the use of the individual or entity to which it is addressed. If you are not the intended recipient, you are not authorized to read, retain, copy, print, distribute or use this message. If you have received this communication in error, please notify the sender and delete all copies of this message. Persistent Systems Pvt. Ltd. does not accept any liability for virus infected mails.

Tuesday, May 8, 2007

Impersonation hand-in-hand with Authentication

Before we begin, let’s go thru the raw definition of ‘Impersonation’.

 

What is Impersonation:

Sometimes users' requests is required to run in the security context of some other user identity. This is where Impersonation comes in picture.

 

Impersonation is a process in which a user accesses the resources by using the identity of another user’.

 

An example of impersonation is the use of the IUSR_<Machine Name> account e.g ‘IUSR_PS4738’ . This account is automatically created by IIS.

Whenever a Web site has anonymous access enabled, then IIS runs all the users' requests using the identity of the IUSR_<Machine Name> account.

 

History:

In ASP: -Impersonation is enabled by default.

i.e. When you request pages with Classic ASP, the page executed with the permissions of the user making the request.

In other words, in Classic ASP, each request impersonates a user account.

For example,

·         If you request a page while logged in under the Administrator user account, the requested Active Server Page executed with the permissions of the Administrator account.

·         If you requested an Active Server Page anonymously, the Active Server Page executed under the IUSER_MachineName account.

 

Present:

                In ASP.Net: -Impersonation is disabled by default.

                i.e When you request pages with ASP.Net, the page is executed with the permissions of the ASPNET account.

                The ASPNET account automatically gets added to your server when you installed the .NET Framework on IIS 5.0.

 

                For reference: In case of IIS 6.0, ‘NETWORK SERVICE’ accounts works instead of regular ‘ASPNET’ account.

                There is lot more attached to it. I’ll come up with them in my next post very soon.

 

                                Lets understand the Impersonation in three different cases.

 

 

Default “No Impersonation”

·         Impersonation is disabled by default.

·         However, you can explicitly specify that ASP.NET should not use impersonation by including the following code in the file Web.config

 

<identity impersonate=”false”/>

 

·         As mentioned above, in case of disabled impersonation, request runs under the privileges of ASPNET account.

·         However you can change this by making a setting in the processModel section of the machine.config file.

·         For e.g To use a high-privileged system account instead of a low-privileged, set the username attribute of the processModel element to SYSTEM.

·         When so configured, the ASP.NET worker process will have the right to access nearly all resources on the local server. In Windows 2000, Windows XP, and Windows Server 2003 family systems, the System account also has network credentials and can access network resources as the machine account.

 

·         In case of Anonymous user also the request will be handled using ASPNET/SYSTEM account. Reason being,  In case of disabled impersonation IUSER_MachineName account will not take effect.

 

<identity impersonate =”true”/>

 

·         This setting is required to be configured  in Web.config.

 

<identity impersonate=”true”/>

 

·         Once enabled below happens in case of Authenticated & Anonymous users

·         If Authenticated access: ASP.NET will handle the request using the credentials of the authenticated user making the request. There is a drawback attached to it as below.

·         If Anonymous access in IIS: The request will be handled using  IUSER_MachineName account.

 

Drawback: If case of authenticated access, if the used credentials have high-privileges access, then it may create trouble. So to overcome this, ideally impersonation must be enabled with limited privileges. This is covered below.

 

<identity impersonate=”true” username=”DOMAIN\username” password=”password”/ >

 

·         This setting is required to be configured  in Web.config.

 

                <identity impersonate=”true” username=”DOMAIN\username” password=”password”/ >

 

·         Once enabled below happens in case of Authenticated & Anonymous users

·         If Authenticated access: ASP.NET will take on the credentials mentioned as above in Web.config instead of the credentials used to login to the system. Hence safe access.

·         If Anonymous access in IIS: The request will be handled using  IUSER_MachineName account.

 

 

 

 

 

 

 

Please follow the attached figure, explaining impersonation hand-in-hand with authentication flow.

 

In case of any doubts, do let me know.

 

 

Thanks & Regards,

Arun Manglick

SMTS || Microsoft Technology Practice || Bridgestone - Tyre Link || Persistent Systems || 3023-6258

 

DISCLAIMER ========== This e-mail may contain privileged and confidential information which is the property of Persistent Systems Pvt. Ltd. It is intended only for the use of the individual or entity to which it is addressed. If you are not the intended recipient, you are not authorized to read, retain, copy, print, distribute or use this message. If you have received this communication in error, please notify the sender and delete all copies of this message. Persistent Systems Pvt. Ltd. does not accept any liability for virus infected mails.

Remapping URLs

Context: This is about how to serve a different page than the page a user requests.

 

Need: Below are the situation when it is really required.

·         You have an site containing lot of pages, and you have a page which is heavily used in the redirection statement in all the other pages. Due to some reason you came up with a new page instead of the earlier. Then in this situation all is required is to go and make changes to all the code statements containing code to redirect to this often used page.

In such situation if you can do it declaratively in just few minutes, then nothing can be great than this quick trick.

 

 

How:

                The simplest way to remap a URL is to specify the remapping in your application's web configuration file.

 

                <?xml version="1.0"?>

<configuration>

<system.web>

  <urlMappings>

    <add  url="~/Home.aspx"  mappedUrl="~/Default.aspx"/>

  </urlMappings>

</system.web>

</configuration>

 

Note:

·         It doesn't matter whether the Home.aspx page actually exists.

·         Though the request is remapped, the URL doesn’t get change. i.e User will not get any feel of remapping.

 

When working with remapped URLs, if there is any need to determine the original URL that a user requested, then use below.

 

·         Request.RawUrl : Returns the original URL (before being remapped).

 

 

Gotcha:

·         The mappedUrl attribute can contain query strings. However, it cannot contain wildcards.

·         For this need is to implement a custom urlMapper in the form of HTTPModule. [Will be covered in next post]

 

 

Thanks & Regards,

Arun Manglick

SMTS || Microsoft Technology Practice || Bridgestone - Tyre Link || Persistent Systems || 3023-6258

 

DISCLAIMER ========== This e-mail may contain privileged and confidential information which is the property of Persistent Systems Pvt. Ltd. It is intended only for the use of the individual or entity to which it is addressed. If you are not the intended recipient, you are not authorized to read, retain, copy, print, distribute or use this message. If you have received this communication in error, please notify the sender and delete all copies of this message. Persistent Systems Pvt. Ltd. does not accept any liability for virus infected mails.

How to Register User Controls and Custom Controls in Web.config

Problem:

 

In previous versions of ASP.NET , to use the custom server controls and user controls on a page, <%@ Register %> directive is used at the top of pages like so:

<%@ Register TagPrefix="ucl" TagName="header" Src="Controls/Header.ascx" %>
<%@ Register TagPrefix="ucl" TagName="footer" Src="Controls/Footer.ascx" %>
<%@ Register TagPrefix="ControlVendor" Assembly="ControlVendor" %>

Once registered these controls can be used anywhere on the page by using the tagprefix and tagnames configured.


<html>
<body>
    <form id="form1" runat="server">
        <ucl:header ID="MyHeader" runat="server" />
    </form>
</body>
</html>

This works fine, but can be a pain to manage when you want to have controls used across lots of pages within your site (especially if you ever move your .ascx files and need to update all of the registration declarations.

Solution:

ASP.NET 2.0 makes control declarations much cleaner and easier to manage. Instead of duplicating them on all your pages, just declare them once within the new pages->controls section with the web.config file of your application:

<?xml version="1.0"?>

<configuration>

  <system.web>
    
    <pages>
      <controls>
        <add tagPrefix="ucl" src="~/Controls/Header.ascx" tagName="header"/>
        <add tagPrefix="ucl" src="~/Controls/Footer.ascx" tagName="footer"/>
        <add tagPrefix="ControlVendor" assembly="ControlVendorAssembly"/>
      </controls>
    </pages>

  </system.web>

</configuration>

Once registered the controls within the web.config file, can then be used similar to above.

<html>
<body>
    <form id="form1" runat="server">
        <ucl:header ID="MyHeader" runat="server" />
    </form>
</body>
</html>

Hope this helps,

 

Thanks & Regards,

Arun Manglick

SMTS || Microsoft Technology Practice || Bridgestone - Tyre Link || Persistent Systems || 3023-6258

 

DISCLAIMER ========== This e-mail may contain privileged and confidential information which is the property of Persistent Systems Pvt. Ltd. It is intended only for the use of the individual or entity to which it is addressed. If you are not the intended recipient, you are not authorized to read, retain, copy, print, distribute or use this message. If you have received this communication in error, please notify the sender and delete all copies of this message. Persistent Systems Pvt. Ltd. does not accept any liability for virus infected mails.

Quick fix to removal of Cookie..

 

If you are new to using cookies then probably you must have wrote similar to the below code to Add & Remove cookies.

 

 

 Adding Cookie:

 

 HttpCookie test = new HttpCookie("Test");
            test.Value = "Testing Data";
            Response.Cookies.Add(test);

 

Removing Cookie: Either of one.

 

        Response.Cookies.Remove("Test");
        Response.Cookies.Clear();

 

But removal of cookie does not work this way.

 

Reason:

 

Remove() and Clear() remove a cookie instance or all cookies from the Response respectively.

They do not do anything on the client, it is a disconnected architecture. Therefore, in order to invalidate (remove) the cookie from the Request as sent by the client - for the next request and subsequent requests you need a quick fix.

Quick Fix:

Set the cookie expirey to be less than the next request time

 

Response.Cookies["Test"].Expires = DateTime.Now.AddDays(-1);

 

 

Hope it it useful.

 

 

Thanks & Regards,

Arun Manglick

SMTS || Microsoft Technology Practice || Bridgestone - Tyre Link || Persistent Systems || 3023-6258

 

DISCLAIMER ========== This e-mail may contain privileged and confidential information which is the property of Persistent Systems Pvt. Ltd. It is intended only for the use of the individual or entity to which it is addressed. If you are not the intended recipient, you are not authorized to read, retain, copy, print, distribute or use this message. If you have received this communication in error, please notify the sender and delete all copies of this message. Persistent Systems Pvt. Ltd. does not accept any liability for virus infected mails.