Saturday, 10 September 2016

ASP.NET - Setting HTML Meta tags in content pages

Problem:


Though the @Page directive in a content page allows one to specify the Description and Keywords attrubutes, the same is ignored when the page is rendered. Instead the Description and Keywords as specified in the master page is what gets rendered as part of the final html output. Is this the intended design and is there a solution to work around this issue?


Solution:


By design, the HTML Meta tags specified in the content page is ignored when the page is rendered. In this context it is important to understand the following:

  • The Master page contains the <head> tag of the page and not the content page. As such the meta tags specified in the master page will prevail.
  • The Content page derives the System.Web.UI.Page class, which though recognizes the Title attribute provided as part of the @Page directive, it does not recognize the other meta tags like description and keywords.
  • The master page and content page are dendered in teh following order:
    • Content Page PreInit event
    • Master Page Init event
    • Content Page Init event
    • Content Page Load event
    • Master Page Load event

As you may observe, while the Load event of Master page happens after that of the Content Page, the Init event of the Master page happens ahead that of the content page.  Given that the Master page loads after the content page, you can manage to use the Title attribute specified in the content page using the script tag within the <title> element, as below:

<title><%: Page.Title %></title>


You cannot however handle the Meta tags in the same way. One solution to handle the Meta tags specified in the Content page is to use a custom Page class, which extends the System.Web.UI.Page class, wherein add support to handling the Description and Keywords as input in the @Page directive. This can be accomplished by adding appropriately overriding the OnLoadComplete event of the Page class, wherein the needed Meta tags are constructed using the values specified in the @Page directive and the same are added to the Page Header.

Check out this codepage link for a sample solution.

Saturday, 9 July 2016

SSRS - Custom Period Filters

Problem:

Just wondering if the reports published on SQL Server Reporting Services can be customized to have a predefined period filters like Last Week, Last Month, Last Quarter, etc.

By: Anonymous

Solution:

You have few options to work around this, which are given below:

1. Dynamic Parameter

Basically, your underlying query or stored procedure needs the values for the From and To paramteres to apply in the where clause of the query. You can do this by first creating a mutli valued Period parameter, with values like, Last Week, Last Month, etc specified as available values. The From and To parameters can then be set up in such a way that it fetches the default value from a query, which takes the value from the Period parameter and then return the corresponding From and To values respectively. In these, the Period Parameter shall be positioned above the From and To parameters. The query can be as simple as

SELECT 
CASE 
WHEN @Period = 'LW' THEN DATEADD(D,((DATEPART(dw, GETDATE())-1)*-1)-7,GETDATE())
WHEN @PEriod = 'LM' THEN DATEADD(M,-1,DATEADD(D,(DAY(GETDATE())-1)*-1, GETDATE()))
END AS FROM_DATE

for the default values of the From parameter. You may extend this idea and use it different ways to make a parameter dynamically derive a value based on a previous parameter. Note that the order of the parameter is important as the parameters are constructed sequentially and there is nothing like an onChange event that you can think of.

2. Let the SQL Query / Procedure do the work

The second alternative is to put in a logic similar to the above within the Report Query or Stored Procedure, so that the user just selects the period filter and the SQL Server interprets it appropriately and returns the appropriate results. The only difference with this option is that the From and To Filters need not be designed in the report and the user won't see it on screen.

3. Wrap the report within an Application

Just in case, you are rendering the reports within an Application, like a ASP.NET application or a Windows Forms Application, then you can have the complete control on the Filters in the presentation layer of your application. For instance in this case, you accept a Period Filter from the user in your front end application and then transform it to an appropriate From and To dates to the ReportViewer component and get the report rendered accordingly.





Sunday, 3 July 2016

Issues in Signing a .NET Assembly using .pfx file

Problem:

I have procured a code signing certificate with a view to ensure that one of application that we intend to distribute to external users is signed so that the same can be trusted by the end users. I imported the certificate into the certificate store and used it successfully to sign the Click Once Manifest using the Signing tab in the Projec Properties dialog in Visual Studio 2013. But I could not have the assembly signed. I exported the certificate from the store as a .pfx file and tried using it for signing the assembly, but am getting issues like "Private Key not Found", while the Private key is very much present in the .pfx file.

By: Rajkumar David

Solution:

Visual Studio 2013 has known issues in handling PKCS# 12 certificate files as it cannot handle files with multilpe certificates in the CA certificate chain. Visual studio may not still consider such certificates for signing the assembly because of the KeySpec Parameter, which is usually set as AT_KEYEXCHANGE(1), whereas Visual Studio expects this to be AT_SIGNATURE(2). It is possible that while requesting the certificate, the KeySpec is set as 1 and as such the certificate is generated with the value as 1. You may verify CSR that you have submitted to the CA to check this.

OK, now what do you with that certificate? You have option to import such certificates with KeySpec set as AT_SIGNATUR. The Windows Servers from 2003 onwards have a commandline certificate import utility - certutil.exe. This command allows you to import the certificate with the right KeySpec parameter. Use the following command to do this:

certutil -importPFX -user <pfxfilename> AT_SIGNATURE

After importing the certificate using the above command into the certificate store and then export it back as .pfx file from the store for assembly signing within Visual Studio. This should resolve the given problem.

More on the CertUtil command can be found here.