↓ Skip to main content

MVC

Kendo Grid custom sort order

·2 mins
I’ve been racking my brains as the sorting on the Kendo Grid is pretty good, only that I need to be able to not sort in the order that is being displayed. An example would be numbers written out in full and you want them in number sort order.

Active links on Bootstrap Navbar with ASP.NET MVC

·2 mins
If you have used Bootstrap with your ASP.NET MVC application, you might have faced some issues with implementing active links of it’s Navbar component. We’ll have to dynamically add a css class called active to the particular menu item in order to make it selected in the Navbar. Here is a HtmlHelper extension which is capable of rendering menu items as well as drop-down menu items. I’ve used ASP.NET MVC 5 with Razor and Bootstrap 3.

Make Fake MVC Objects for testing

·3 mins
If you need to create some Fake MVC objects for testing MVC objects then here you are: FakeControllerContext using System.Collections.Specialized; using System.Web; using System.Web.Mvc; using System.Web.Routing; using System.Web.SessionState; using MvcContrib.TestHelper.Fakes; public class FakeControllerContext : ControllerContext { public FakeControllerContext(IController controller) : this(controller, null, null, null, null, null, null) { } public FakeControllerContext(IController controller, HttpCookieCollection cookies) : this(controller, null, null, null, null, cookies, null) { } public FakeControllerContext(IController controller, SessionStateItemCollection sessionItems) : this(controller, null, null, null, null, null, sessionItems) { } public FakeControllerContext(IController controller, NameValueCollection formParams) : this(controller, null, null, formParams, null, null, null) { } public FakeControllerContext(IController controller, NameValueCollection formParams, NameValueCollection queryStringParams) : this(controller, null, null, formParams, queryStringParams, null, null) { } public FakeControllerContext(IController controller, string userName) : this(controller, userName, null, null, null, null, null) { } public FakeControllerContext(IController controller, string userName, string[] roles) : this(controller, userName, roles, null, null, null, null) { } public FakeControllerContext ( IController controller, string userName, string[] roles, NameValueCollection formParams, NameValueCollection queryStringParams, HttpCookieCollection cookies, SessionStateItemCollection sessionItems ) : base(new FakeHttpContext(new FakePrincipal(new FakeIdentity(userName), roles), formParams, queryStringParams, cookies, sessionItems), new RouteData(), (Controller)controller) { } }

Disclaimer Page using MVC

·2 mins
If you ever need a disclaimer page in MVC, there are a few things you need to consider, the first is you should not be able to get to a page without agreeing to the disclaimer and if you have to re-login then you need to be presented with the disclaimer page again.

URL Activity Tampering and how to prevent it in MVC

·13 mins
No web applications are developed without concerning the security issues. But it is not sure whether the security measures are implemented correctly and the applications are well protected. We could see a lot of web applications query string parameters in plain string which are easy to manipulate and way to escalate restricted resources. Even the URL parameters are encrypted there is still a chance of a bruteforce attack by manipulating the encrypted hash and it yields when the database is large where the chance of matching a random hash is high. So it is clear the importance to protect the URL in the web application is necessary for all programming platforms. Here, this is discussed with reference to ASP.NET MVC and the utility class is presented.

URL Validator

·1 min
I needed to check to see if a URL that the user was entering was a valid Url, does it contain HTTP:// or HTTPS:// I ended up writing a small ValidationAttribute to add to the collection I am now building up, here is the code:

MVC way to check the File Extension

·4 mins
Following on from my article about checking for the file signature to ensure that the file is who it says it is I decide to take it further and produce a Data Validation attribute, I found that the Microsoft.Web.Mvc.dll library already has one ,but it only checks for the file names, which is generally okay.

MVC running on IIS8

·1 min
After a number of 500 errors coming up from IIS8 under Windows Server 2012, I final found the solution. The solution was to go back to the Microsoft Server 2012 Dashboard and select the “Add Roles” feature. Scroll down to the root “Web Server (IIS)”, expand “Web Server”, expand “Application Development” and select “ASP.NET 3.5” and “ASP.NET 4.5” and “Install”.

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.

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.”

Encrypting Web.config for the website

·1 min
Security is always high for installation to customer so is it not about time you encripted the connection string in your web.config file? This is how it looks like before encrypting: <connectionStrings> <add name="Pubs" connectionString="Server=localhost;Integrated Security=True;Database=Pubs" providerName="System.Data.SqlClient" /> <add name="Northwind" connectionString="Server=localhost;Integrated Security=True;Database=Northwind" providerName="System.Data.SqlClient" /> </connectionStrings> We can encrypt any section of your Web.config file on-the-fly and programatically. If you have full access to your Web server, you can encrypt your connection strings with this single command-line located in the in the %windows%\Microsoft.NET\Framework\versionNumber folder:

Upgrading an ASP.NET MVC 3 Project to ASP.NET MVC 4

·2 mins
 ASP.NET MVC 4 can be installed side by side with ASP.NET MVC 3 on the same computer, which gives you flexibility in choosing when to upgrade an ASP.NET MVC 3 application to ASP.NET MVC 4.

Using MEF in MVC to inject Controllers

·2 mins
After travelling for a year I was interest to see if MEF would work with MVC, and after some digging around I found it can. ***UPDATED**** Added a CastleWindsor example With any MEF you’ll need a Composition Factory which in this case I’ll place in the MEF directory, but it can be in a different project if required.