Tuesday, March 09, 2010

MS SQL Grand permission to execute

You can solve many protential website security issue by only granting execute permission to store procedures required by the web application.  Here are some scripts that help you achieve that. 
/* CREATE A NEW ROLE */
CREATE ROLE db_executor
/* GRANT EXECUTE TO THE ROLE */
GRANT EXECUTE TO db_executor
GRANT SELECT TO db_executor
GRANT INSERT TO db_executor
GRANT UPDATE TO db_executor
GRANT DELETE TO db_executor


/* Here is a way to grand execute to all store procedures. */

/* *************************** */
CREATE PROC grants as
declare curse cursor for select name from sysobjects where type='P'
OPEN CURSE
declare @proc varchar(100)
declare @stmt nvarchar(200)
fetch next from curse into @proc
while @@fetch_status=0
begin
set @stmt='grant execute on '+@proc+' to bnbuser'
exec SP_EXECUTESQL @STMT
print @stmt
fetch next from curse into @proc
end
close curse
deallocate curse
GO

Thursday, March 04, 2010

Speed Up Page Load For Asp.Net Pages

I ran into performance issue while I'm building a large datagrid at work. The ASP.net page was loading slowly because of the large size. The DataGrid has 200 rows and 50 columns, and depending on the data, it could be 1 mb in size for the html along.

Here is what I did.

1.) I remove all useless the white spaces and carriage returns on the HTML by adding the code below to the Master Page. This reduced my HTML size about 10%.

Written in C#

//Add this to the top of the page


using System.Configuration;
using System.Web.UI;
using System.Text.RegularExpressions;

//Overrides the Render method

protected override void Render(HtmlTextWriter writer)
{
using (HtmlTextWriter htmlwriter = new HtmlTextWriter(new System.IO.StringWriter()))
{

base.Render(htmlwriter);

string html = htmlwriter.InnerWriter.ToString();

if ((ConfigurationManager.AppSettings.Get("RemoveWhitespace") + string.Empty).Equals("true", StringComparison.OrdinalIgnoreCase))
{

html = Regex.Replace(html, @"(?<=[^])\t{2,}
(?<=[>])\s{2,}(?=[<])
(?<=[>])\s{2,11}(?=[<])
(?=[\n])\s{2,}", string.Empty);

html = Regex.Replace(html, @"[ \f\r\t\v]?([\n\xFE\xFF/{}[\];,<>*%&
^!~?:=])[\f\r\t\v]?", "$1");

html = html.Replace(";\n", ";");

}

writer.Write(html.Trim());

}

}

And add this to the web.config appsetting section:



2.) Asp.net generates insanely long controls ID names for example, ctl00_ContentPlaceHolder1_GridControl1_DataGrid1_ctl30_Label2. This is one of the label name generated by ASP.net and you can view it by using view source on the page. Well imagent this is being repeated for each cell, the HTML is huge! To reduce the size of generated web controls ID names, you can shorten the control name. For example, instead calling your ContentPlaceHolder "ContentPlaceHolder1" call it "cph1 and DataGrid1 call it "DG1". This reduced the html page size another 10%.


Yeap, that's a easy 20% HTML page size reduction.  Smaller page means it will load faster on the web browser.