↓ Skip to main content

Articles

Validating an e-mail address the DataAnnotation way

·2 mins
There are many ways to validate if an e-mail address is right, and many wrong ways. The biggest wrong way is using a Regular expression, why? They are complex and there is not one that resolves this issue, you will always get an invalid e-mail address that passes through okay.

Determine which file type has been uploaded

·2 mins
It is quite an easy process to upload files from a web browser, but how do you know it is the correct type of document you are asking for? You can check the file extenstion, but a hacker would just change the file type to another extension, so this is not a fail safe way of checking the file type being uploaded.

Stay Alive in MVC

·2 mins
Have you ever opened a page for one of your websites and it lags for a while before it finally shows a page but then all of your following requests are quick? If you were to Google the problem you’d find that often it ends up having to do with IIS meeting an idle time limit and shuts down your site.

Overloading Methods in WCF

·3 mins
Is this possible? It is possible to overload a method in WCF. However, when you want to expose overloaded methods as operations in a WCF contract, there is more to it than simply writing two or more methods with the same name.

Cross Site Attacks – XSS

·5 mins
Cross Site Attacks (XSS) what is that all about? “Cross-site scripting (XSS) is a type of computer security vulnerability typically found in Web applications. XSS enables attackers to inject client-side script into Web pages viewed by other users. A cross-site scripting vulnerability may be used by attackers to bypass access controls such as the same origin policy. Cross-site scripting carried out on websites accounted for roughly 84% of all security vulnerabilities documented by Symantec as of 2007. Their effect may range from a petty nuisance to a significant security risk, depending on the sensitivity of the data handled by the vulnerable site and the nature of any security mitigation implemented by the site’s owner.” All user’s inputs can be a back door for attacker to attack your site. User’s inputs that we will categorize as un-trusted inputs are : Incoming URLs including Request.QueryString[] values Form post data (Request.Form[] values including values from hidden fields and disabled fields) Cookies Data in HTTP Headers (such as Request.UserAgent and Request.UrlReferrer). Your site could be attacked by altering the query string, form values, or cookies data. The solution is not to prevent request manipulation but to check that each request is a legal request for the logged-in visitor. Cross-Site Scripting and HTML Injection If an attacker can get our site to return some javascript to our visitors, then the attacker’s script can take control of our visitors’ browsing session and by this the attackers can collect the personal information (passwords, credit card details) of our visitors. There are two main ways an attacker follow to get this: Persistently: by entering formed malicious input into some feature and hoping we will store it into our database and then issue it back to other visitors. Non-persistently or Passively : by finding a way of sending malicious data in a request to our application and having our application echo that data back in it is response. The attacker then finds a way to trick a victim into making such a request. Razor HTML Encoding The Razor view engine helps protect us against XSS attacks by encoding any data that we refer to using the @ tag to make it safe to display as HTML. This means when we request an URL with a javascript code as a query string, Razor processes the query string value and replaces the special characters and rendering javascript code as a simple text. Razor view engine treats the contents of MvcHtmlString objects as if they were encoded, even when that is not the case. We can use Html.Raw helper method to include our HTML into the web page without it being encoded.

Preventing Cross Site Request Forgery

·8 mins
What is Cross Site Request Forgery (CSRF)? “A CSRF attack forces a logged-on victim’s browser to send a pre-authenticated request to a vulnerable web application, which then forces the victim’s browser to perform a hostile action to the benefit of the attacker. CSRF can be as powerful as the web application that it attacks.”

How to perform JavaScript Unit Testing

·3 mins
Following the TDD approach when you are working with MVC it is quite easy to produce Unit Tests that are based on the MVC Controllers, with more and more business logic appearing in the User Interface by using libraries such as jQuery it is becoming more important to perform Unit Testing on JavaScript.

files

·1 min
TalentCubed.zip (3.34 mb)

The repository layout

·4 mins
Team Foundation Server provides the ultimate flexibility in terms of how you arrange our data. Because it simply versions directories and files, and because it describes no particular meaning to any of those objects, you may arrange the data in your repository in any way that we choose. Unfortunately, this flexibility also means that it’s easy to find yourself “lost without a roadmap” as you attempt to navigate different Team Foundation Server project repositories that may carry completely different and unpredictable arrangements of the data within them.

SQL Performance Profiler for SQL Express

·1 min
I came across this smart little Profiler for SQL Express, or any version of SQL as it happens. Microsoft SQL Server family includes free Express edition, that is fully functional, however has some disappointing limitations which prevent from using it in development process. One of them is absense of profiling tools, standard SQL profiler is not included. However, I came across this SQL Performance Profiler to use with the express edition for tuning your system.

Search every SQL table for a string

·1 min
Here is some neat SQL to allow you to search every table and every field in the database for a string CREATE TABLE myTable99 (TABLE_NAME sysname, COLUMN_NAME sysname, Occurs int) GO SET NOCOUNT ON DECLARE @SQL varchar(8000), @TABLE_NAME sysname, @COLUMN_NAME sysname, @Sargable varchar(80), @Count int SELECT @Sargable = ‘There are too many subscription products resulting from the supplied request data’ DECLARE insaneCursor CURSOR FOR SELECT c.TABLE_NAME, c.COLUMN_NAME FROM INFORMATION_SCHEMA.Columns c INNER JOIN INFORMATION_SCHEMA.Tables t ON t.TABLE_SCHEMA = c.TABLE_SCHEMA AND t.TABLE_NAME = c.TABLE_NAME WHERE c.DATA_TYPE IN (‘char’,’nchar’,‘varchar’,’nvarchar’,’text’,’ntext’) AND t.TABLE_TYPE = ‘BASE TABLE’ OPEN insaneCursor FETCH NEXT FROM insaneCursor INTO @TABLE_NAME, @COLUMN_NAME WHILE @@FETCH_STATUS = 0 BEGIN SELECT @SQL = ‘INSERT INTO myTable99 (TABLE_NAME, COLUMN_NAME, Occurs) SELECT ' + ’’’’ + @TABLE_NAME + ’’’’ + ‘,’ + ’’’’ + @COLUMN_NAME + ’’’’ + ‘,’ + ‘COUNT(*) FROM [’ + @TABLE_NAME + ‘] WHERE [’ + @COLUMN_NAME + ‘] Like ' + ‘’’’+ ‘%’ + @Sargable + ‘%’ + ’’’’ –SELECT @SQL EXEC(@SQL) IF @@ERROR <> 0 BEGIN SELECT @SQL SELECT * FROM INFORMATION_SCHEMA.Columns WHERE TABLE_NAME = @TABLE_NAME GOTO Error END FETCH NEXT FROM insaneCursor INTO @TABLE_NAME, @COLUMN_NAME END SELECT * FROM myTable99 WHERE Occurs <> 0 Error: CLOSE insaneCursor DEALLOCATE insaneCursor GO DROP TABLE myTable99 GO SET NOCOUNT OFF

Switching from iPhone to Android

·5 mins
The iPhone is the top smartphone, but some Android rivals like the Galaxy S III are very attractive, and they’re available in large sizes that the iPhone is not. Plus, they support cool technologies Apple doesn’t support.

Disable Shut Down on your Server

·1 min
When you are connecting via a remote connection to a server I find the Shut Down is far far to close to the log off, and if you hit the Shut Down by mistake it can be a devil to get the remote machine to power back up and may require someone to visit the machine to hit the power button.

Deploy a database project with TFS Build

·2 mins
When you develop web application you often have this scenario. Some people are testers, they are testing the application in dev all day, and they want always to test latest version. Moreover testers usually fills data into web application, so they want their data to be preserved between various deploy. If you simply clear all data in test database you will end with a lot of furious tester so do not even think to do this. If a developer needs to change database schema, we need to automate not only the deploy of the web application, but we need also to sync test database with the latest changes. This is necessary so the test site can work with the new version, but old data is preserved for the happiness of the testers. This is quite simple using TFS and Database project. The situation is this one

Visual Studio 2012 running slow

·2 mins
Three months in and my Visual Studio 2012 is running slow, I’ll even go as far as saying it is freezing up some times, so I loaded up SysInternals Process Monitor and set a filter on Visual Studio and to my complete and utter amazement I found PerfWatson running wild in the system

Who is locking my SQL database?

·2 mins
Need to know who is logged on to your SQL database ,here is a nice script that I got from Gary Dunne DECLARE @spid1 int = NULL /* server process id to check for locks */ DECLARE @spid2 int = NULL /* other process id to check for locks */ set nocount on /* ** Show the locks for both parameters. */ declare @objid int, @dbid int, @string Nvarchar(255) CREATE TABLE #locktable ( spid smallint ,loginname nvarchar(20) ,hostname nvarchar(30) ,dbid int ,dbname nvarchar(20) ,objId int ,ObjName nvarchar(128) ,IndId int ,Type nvarchar(4) ,Resource nvarchar(16) ,Mode nvarchar(8) ,Status nvarchar(5) ) if @spid1 is not NULL begin INSERT #locktable ( spid ,loginname ,hostname ,dbid ,dbname ,objId ,ObjName ,IndId ,Type ,Resource ,Mode ,Status ) select convert (smallint, l.req_spid) –,coalesce(substring (user_name(req_spid), 1, 20),’’) ,coalesce(substring (s.loginame, 1, 20),’’) ,coalesce(substring (s.hostname, 1, 30),’’) ,l.rsc_dbid ,substring (db_name(l.rsc_dbid), 1, 20) ,l.rsc_objid ,’' ,l.rsc_indid ,substring (v.name, 1, 4) ,substring (l.rsc_text, 1, 16) ,substring (u.name, 1, 8) ,substring (x.name, 1, 5) from master.dbo.syslockinfo l, master.dbo.spt_values v, master.dbo.spt_values x, master.dbo.spt_values u, master.dbo.sysprocesses s where l.rsc_type = v.number and v.type = ‘LR’ and l.req_status = x.number and x.type = ‘LS’ and l.req_mode + 1 = u.number and u.type = ‘L’ and req_spid in (@spid1, @spid2) and req_spid = s.spid end /* ** No parameters, so show all the locks. */ else begin INSERT #locktable ( spid ,loginname ,hostname ,dbid ,dbname ,objId ,ObjName ,IndId ,Type ,Resource ,Mode ,Status ) select convert (smallint, l.req_spid) –,coalesce(substring (user_name(req_spid), 1, 20),’’) ,coalesce(substring (s.loginame, 1, 20),’’) ,coalesce(substring (s.hostname, 1, 30),’’) ,l.rsc_dbid ,substring (db_name(l.rsc_dbid), 1, 20) ,l.rsc_objid ,’' ,l.rsc_indid ,substring (v.name, 1, 4) ,substring (l.rsc_text, 1, 16) ,substring (u.name, 1, 8) ,substring (x.name, 1, 5) from master.dbo.syslockinfo l, master.dbo.spt_values v, master.dbo.spt_values x, master.dbo.spt_values u, master.dbo.sysprocesses s where l.rsc_type = v.number and v.type = ‘LR’ and l.req_status = x.number and x.type = ‘LS’ and l.req_mode + 1 = u.number and u.type = ‘L’ and req_spid = s.spid order by spID END DECLARE lock_cursor CURSOR FOR SELECT dbid, ObjId FROM #locktable WHERE Type =‘TAB’ OPEN lock_cursor FETCH NEXT FROM lock_cursor INTO @dbid, @ObjId WHILE @@FETCH_STATUS = 0 BEGIN SELECT @string = ‘USE ’ + db_name(@dbid) + char(13) + ‘UPDATE #locktable SET ObjName = object_name(’ + convert(varchar(32),@objId) + ‘) WHERE dbid = ’ + convert(varchar(32),@dbId) + ’ AND objid = ’ + convert(varchar(32),@objId) EXECUTE (@string) FETCH NEXT FROM lock_cursor INTO @dbid, @ObjId END CLOSE lock_cursor DEALLOCATE lock_cursor SELECT * FROM #locktable DROP TABLE #locktable