May 18 2008

Accessing the ASP.NET Authentication, Profile and Role Service in Silverlight

Category: ASP.NET 2.0 - General | SilverlightBil@l @ 19:38

A great article by Brad Adams on accessing the ASP.NET Authentication, Profile and Role Services in Silerlight!

Check it out here: http://blogs.msdn.com/brada/archive/2008/05/03/accessing-the-asp-net-authentication-profile-and-role-service-in-silverlight.aspx

 

Hope you enjoy it!
Regards

Tags: ,

May 17 2008

Input Validation Helper Methods

Category: ASP.NET 2.0 - General | ASP.NET SecurityBil@l @ 21:00

I found a  good resource on validating user input on Channel9. You can reach that resource by following this link: http://channel9.msdn.com/wiki/default.aspx/SecurityWiki.RegExInputValCode2

The page contains a set of utility methods to help you in validating the inputs for your web application. The technique used is "whitelisting" technique in a sense that the user input is validated against a pattern that is known to be good using Regular Expressions.

 

Hope you will benefit from this page!

Regards 

 

Tags: ,

Apr 6 2008

Problem in SQL Server Session State - ASP.NET 3.5

*** Updated - April 7 2008  ***

I played a little bit with the command line I am using to install the SQL Server Session State database and it seems to work fine: To register the database for the SQL Server Session State on ASP.NET 2.0 or ASP.NET 3.5:

aspnet_regsql -C "Data Source=.;Integrated Security=True" -ssadd -sstype c -d SessionStateDB

In addition to this, you might need to grant access to: 'NT AUTHORITY\NETWORK SERVICE' on your database

Hope this solves your problem as it solved mine!!
Regards 

*** Updated - April 7 2008  ***

 

I enabled a database with the schema tables used for SQL Server Session State as follows:

aspnet_regsql -S .\ -d aspnetdb -E -sstype c -ssadd

 

Then in the web.config file, I enabled SessionState as follows:

<sessionState 
   
allowCustomSqlDatabase="true"
   
mode="SQLServer"
   
sqlConnectionString="data source=.\;initial catalog=aspnetdb;trusted_connection=true"
   
timeout="20" />

 

When I run my page, I recieve the following exception:

Unable to use SQL Server because ASP.NET version 2.0 Session State is not installed on the SQL server. Please install ASP.NET Session State SQL Server version 2.0 or above.

 

It sounds strange, I am using the 2.0 version of the script!! Any ideas?

Thanks

Tags: , , , ,

Mar 27 2008

Nested MasterPage Not Firing Page_Load Event

Category: ASP.NET 2.0 - General | ASP.NET 3.5Bil@l @ 11:42

I faced a situation today where I am trying to attach to the page_load event of a child nested master page to load some data on the master page itself. The event was not firing at all. Later on I figured out that the AutoEventWireup property on the Page directive is false!!!

Seems by default when you create a nested master page, its AutoEventWireup property is set to false! What you need to do is just make it true!

 

Hope this helps!

Regards

Tags: ,

Feb 18 2008

How to Postback Asynchronously from inside a GridView

Category: AJAX-ATLAS | ASP.NET 2.0 - GeneralBil@l @ 17:08

I have posted a new article on the ASP.NET Wiki and the article is titled as:

How to Postback Asynchronously from inside a GridView

 

Check it out and have your comments and improvements!
Regards

Tags: ,

Feb 11 2008

HotFix: Performance and Editor fixes for Microsoft Visual Studio 2008 and Visual Web Developer Express 2008

Category: ASP.NET 2.0 - General | Visual StudioBil@l @ 07:34

The Visual Studio Web Tools team added a new HotFix for the Visual Studio Web environment.

You can read all the details about the fix here and download the hotfix from here.

 

Regards

Tags: ,

Sep 13 2007

HttpModules and IIS 7

When working with an ASP.NET Web Application under Vista/IIS 7 and developing an HttpModule, watch out for the Application pool you are using. If your application is configured to work with classic application pool, then it is enough to define the module within the httpModules section as:

<httpModules>
       <add name="MyModule" type="MyModule" />
</httpModules>

If however, you have configured your web application to work with Default Application Pool (i.e. integrated mode), then you should add another entry to your web.config file as follows:

    <!--
  The system.webServer section is required for running ASP.NET AJAX under Internet
  Information Services 7.0. It is not necessary for previous version of IIS.
 -->
    <system.webServer>
        <validation validateIntegratedModeConfiguration="false" />
        <modules>
                <add name="MyModule" preCondition="integratedMode" type="MyModule" />
        </modules>
    </system.webServer> 

 

Thanks for my friend Wessam for hinting this to me!!
Regards 

 

Tags: , ,

Aug 25 2007

Update on Website Menu + web.sitemap: A smart combination

Category: ASP.NET 2.0 - GeneralBil@l @ 19:00

I have updated the code I presented yesterday in: Website Menu + web.sitemap: A smart combination to handle pages that are not listed in the web.sitemap. Here is the updated code in bold:

        private static string GetSubRootNode()
        {
            // Get the current code
            SiteMapNode current = SiteMap.CurrentNode;
            // Get the root node
            SiteMapNode root = SiteMap.RootNode;
            // Get a copy of the root node
            SiteMapNode node = root;
            string url = "";
   
            // If we are processing the parent node,
            // then return its url.
            if (current != null)
            {
                if (current != root)
                {
                    // We check if the current's parent node
                    // is different from the root, go up
                    // one level in the web.sitemap. Kepp up looping
                    // until the current node's parent is the root
                    while (current.ParentNode != root)
                    {
                        current = current.ParentNode;
                    }
                    node = current;
                }
               
                // If localhost, then remove /CodeSuite/CodeSuiteWeb/
                url = node.Url;
                if (HttpContext.Current.Request.IsLocal)
                    url = url.Substring(24, url.Length - 24);
            }
            return url;
        }

If you still find any problems with the above code, please let me know!

Hope this helps,
Regards

Tags:

Aug 23 2007

Website Menu + web.sitemap: A smart combination

Category: ASP.NET 2.0 - GeneralBil@l @ 18:30

I am working on a website that contains a main menu on top. In addition, I am making use of the SiteMapPath to show a breadcrumb that helps the user while browsing the website. I might have the following heirarchy in the website:

Default.aspx
Articles.aspx
         --> BrowseArticles.aspx
                             -->  ShowArticle.aspx

 

I prepared first of all an xml file of the major pages something like this:

<?xml version="1.0" encoding="utf-8" ?>
<MainMenu>
    <MenuItem>
        <Title>Home</Title>
        <Href>Default.aspx</Href>
    </MenuItem>
    <MenuItem>
        <Title>Articles</Title>
        <Href>ShowCategories.aspx</Href>
    </MenuItem>
</MainMenu>

So, my aim is to show a main menu of the major pages in the website. I prepare some code using XLinq to browse the XML file and based on the URL of the currently accessed page, I know which menu item I can style it differently since it is the one accessed currently.

However, when I go through the heirarchy, I loose the ability to catch the major page to style it differently. This is explained since those sub pages are not included in my menu xml file. What I need to do is whereever I am in the heirarchy of a major item for instance, going deep through the Articles section, I always want the Articles item to be selected. So I made use of the web.sitemap to know the sub-root of the currently visited page.

Sub-Root means all pages that are one level below the root. Because I want to show in my main menu the major pages, so I am interested only in the sub-root nodes!

So, here is the code I used to construct the main menu:

        /// <summary>
        /// This method is required to build the main menu.
        /// It first starts by loading the XML menu file,
        /// then select all MenuItem elements and add them to
        /// a collection of MenuItem. The final step is a call
        /// to GenerateHTML which will generate the actual code
        /// for the main menu with the right item selected!
        /// </summary>
        /// <returns></returns>
        public static string BuildMainMenu()
        {
            // Query the menu.xml file into an XElement
            XElement menuItems= null;
           
            // Load Data from Cache
            if (Utility.Cache["MainMenu"] != null)
                menuItems= (XElement)Utility.Cache["MainMenu"];
           
            // Cache empty load from the xml file
            if (menuItems == null)
            {
                try
                {
                    menuItems = XElement.Load(HttpContext.Current.Server.MapPath("~/menu.xml"));
                   
                    // Store the XML in a Cache - For better peformance
                    // Caching of the menu for 90 days with cache dependency on the menu.xml
                    // file
                    System.Web.Caching.CacheDependency depend=
                            new System.Web.Caching.CacheDependency(HttpContext.Current.Server.MapPath("menu.xml"));
                    Utility.Cache.Insert("MainMenu", menuItems, depend, DateTime.Now.AddDays(90), TimeSpan.Zero);
                }
                catch (System.Xml.XmlException ex)
                {
                    throw ex;
                }
            }
           
            // Get the MenuItems collection
            List<MenuItem> menuItemsCol= new List<MenuItem>();
            IEnumerable<XElement> iElements=
                    from e in menuItems.Elements("MenuItem")
                    select e;
                   
            foreach (XElement x in iElements)
            {
                CodeSuiteWeb.Menu.MenuItem item =
                    new CodeSuiteWeb.Menu.MenuItem {Title=(string)x.Element("Title"), Href=(string)x.Element("Href")};
                if (item != null)
                    menuItemsCol.Add(item);
            }           
           
            // Generate HTML for the menu
            if ((menuItemsCol != null) && (menuItemsCol.Count > 0))
                return GenerateMenuHTML(menuItemsCol);
               
            return string.Empty;  
        }

The code above is well documented through XML documentation. There are left few major methods that will helps in building the menu:

        /// <summary>
        /// Generates the HTML code that constitutes
        /// the main menu of the website
        /// </summary>
        /// <param name="menuItemsCol">
        ///     A collection of <seealso cref="MyWeb.Menu.MenuItem" />
        ///     representing the items in the menu xml file/>
        /// </param>
        /// <returns></returns>
        private static string GenerateMenuHTML(List<MenuItem> menuItemsCol)
        {
            // Container to hold the HTML generated
            StringBuilder sb= new StringBuilder();
           
            // Get the visited page
            string currentPage= GetCurrentPage();
                        
            // Generate the Menu HTML
            foreach (MyWeb.Menu.MenuItem item in menuItemsCol)
            {                
                if (item.Href.StartsWith(GetSubRootNode(), StringComparison.CurrentCultureIgnoreCase))
                    sb.AppendFormat("<li><a href=\"{0}\" title=\"{1}\" class=\"hover\">{1}</a></li>", item.Href, item.Title);
                else
                    sb.AppendFormat("<li><a href=\"{0}\" title=\"{1}\">{1}</a></li>", Utility.BasePath + item.Href, item.Title);
            }
           
            return sb.ToString();
        }
   
        /// <summary>
        /// Gets the Sub-Parent node's URL of the current node.
        /// </summary>
        /// <returns>
        ///     A <see cref="System.String"/> that represents the URL of the sub-parent node
        /// </returns>
        private static string GetSubRootNode()
        {
            // Get the current code
            SiteMapNode current = SiteMap.CurrentNode;
            // Get the root node
            SiteMapNode root = SiteMap.RootNode;
            // Get a copy of the root node
            SiteMapNode node = root;
            string url = "";
   
            // If we are processing the parent node,
            // then return its url.
            if (current != root)
            {
                // We check if the current's parent node
                // is different from the root, go up
                // one level in the web.sitemap. Kepp up looping
                // until the current node's parent is the root
                while (current.ParentNode != root)
                {
                    current = current.ParentNode;
                }
                node = current;
            }
           
            // If localhost, then remove /CodeSuite/CodeSuiteWeb/
            url = node.Url;
            if (HttpContext.Current.Request.IsLocal)
                url = url.Substring(24, url.Length - 24);
 
            return url;
        }

        private static string GetCurrentPage()
        {
            string rawUrl= HttpContext.Current.Request.FilePath;  
            
            // If localhost, then remove /CodeSuite/CodeSuiteWeb/
            if (HttpContext.Current.Request.IsLocal)
                rawUrl = rawUrl.Substring(24, rawUrl.Length - 24);
           
            return rawUrl;
            //// Remove all the path and extension and reture pure page name
            //rawUrl= rawUrl.Substring(rawUrl.LastIndexOf('/')+1);            
            //return rawUrl;
        }   
 

As you can see, no matter how deep you go through the heirarchy of pages under one sub-root node, always the sub-root node (which is one of the major nodes in the main menu) will be selected!

Hope this helps!
Regards

Tags:

Jul 20 2007

Localization in ASP.NET 2.0

Category: ASP.NET 2.0 - General | General | GeneralBil@l @ 06:59

A series of two important and helpful articles by Rick Strahl can be found here:

Hope this helps you out,
Regards

Tags: , ,