How to get the users IP Address in C#.NET?

When you search this on your favourite search engine, (Google) you will get flooded by load of different way to get the end result. This is great in a sense as you know there are thousands of people with different answers, but then that is where the problem is at. There are so many single responses to how you can get the users IP Address in C#.NET, but not one that shows you everything. Therefore I will present to you below different methods to get the users IP and how I have implemented it in a project.

All the methods I found to be best are using the Request Server Variables. These are predetermined environment variables and request header information. This is why you can get IP address as it is part of the header information.

The standard server variable to use is ‘REMOTE_ADDR’, which is done as the following:

HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];

However if the user is behind a proxy server, then the above will return that IP Address and not the actual users. The server variable to get the users IP address from behind the proxy server is ‘HTTP_X_FORWARDED_FOR’ done as below:

HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];

Then you get that issue of not knowing if the user is behind a proxy or not, as the first example will return one or the other and the second example will only return a value if there is a proxy server. Therefore we need to find out if there is a proxy server or not. You can do this with the server variable ‘HTTP_VIA’, if this has a value then they must be using a proxy server and so you can get the correct IP as below:

If (HttpContext.Current.Request.ServerVariables[“HTTP_VIA”] != null) {
return HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
}

return HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];

Or another method is to do it in reverse and check if the proxy method is not null first instead like:

string userIp = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];

if (string.IsNullOrEmpty(userIp)){
return HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
}

return userIp;

This is the best method I found to get the users IP Address in ASP.NET C#, but if you think there is a better way then please comment below with your solution.

Kentico the mule of development

I have talked before about how Content Management Systems(CMS) are getting so good, that it means they are doing more of the work so you don’t have too. Instead of doing the donkey work, you can be improving the whole application and doing the most advance features. However over my time of working with such a CMS called Kentico I have found some pro’s and con’s of this mode of working, so I wanted to share my thoughts of if it is a favourable idea to be working on these CMS’.

Kentico’s power

First off an overview of what Kentico is, in case you have not heard of it. Kentico is a C#.NET Swiss army knife of a CMS as it can do Content Management, E-commerce, Online Marketing and basically most of the work for a developer.

Some of the features are:

Kentico uses the name ‘Page Types’ for its content templates. In these you describe the format of how this content should be held, like a database table. You tell it what the field is called in the database, the size, the data format and things like the caption the Content Editor sees. When the Content Editor then adds new content and chooses that Page Type, they are presented with a form with these fields to enter the content. What it then means for the developer, is they have a standard format of how to pull different content from the CMS and not just get a blob of data. (Find out more http://www.kentico.com/product/all-features/web-content-management/custom-pages)

As well as just getting well formatted content, you can use ‘Web Parts’. Web Parts are like functional modules that get used on a template. These can be things like a navigation bar that is on each page, but you can also have different types on each template. Also these can pull content from the database using the Page Type like a news feed or list of blog posts. (Find out more http://www.kentico.com/product/all-features/web-content-management/webparts)

However the Web Parts are added by the developer and are only a single instance of the Web Parts. What we really want is the Content Editor to be able to choose what pages have what modules and for this there are ‘Widgets’. These are instances of the Web Parts, which means you create a Web Part and then the Widget references it. When the Content Editor uses the Widget it takes a copy of the Web Part that it stores on the Page. The control it gives is for the Content Editor to decide what module shows where, when and how. These can get very complex to give more control to the Content Editor or the Developer can keep some control by limiting the functionality of the module. (Find out more http://www.kentico.com/product/all-features/web-content-management/widgets)

The other great content editor control is to build forms, yes full forms. With the CMS you can use a bit of a WYSIWYG to construct a form with various types of fields and then get them submitted to the database. These can also have customisation to send emails to the user or the administrator, create A/B split testing of forms and the editor can even customise the layout of the form. This will spare some hours building custom forms each time with the same validation, same submitting pattern and same design. (Find out more http://www.kentico.com/product/all-features/web-content-management/on-line-forms)

You can read more about all the features in depth and download a demo from the Kentico website. [http://www.kentico.com/product/all-features]

Tell me why?

Other than just showing you the brochure, I wanted to explain what makes using a full customisable CMS like Kentico brings.

In my opinion DevOps is all about empowering the next person to do more with less work, for example an Operations Engineer could make it easier for a Developer to spin up development environment. This kind of this means the Operations Engineer can keep doing other work and the Developer can get on with their work faster. This is the same thing with Kentico and the Content Editors. The more generic and bespoke Web Parts you make, the more the Content Editor can do without the assistance of the Developer, which then leaves the Developer to get on with other work like improving the systems.

When you have a bespoke website that you need to do all the changes within the code, then the Developer needs to do all the leg work for even the smallest change. If the Content Editor wants a box moved to another location of the page, then that’s a boring task for the Developer. However with a CMS like Kentico, the Content Editor will be able to move this by themselves.

I would rather this kind of work pattern as for both Front End and Back End development, as I want to be working on the next great and latest thing, while also looking to improve my code and the testing. This work pattern removes them small tasks that interrupt your work, plus also if you work in Scrum like myself then it takes up your sprint points working in the more Developer heavy pattern.

As mentioned above its not just moving Widgets and custom Web Parts that make this CMS great. It is also the fact that the Content Editors can create their own forms. I remember having to built every simple form from scratch each time, but this now puts the work in their hand, but also in a simple way. I also say simple forms, but it is as simple as you the Developer wants to make it. As you can customise or custom build the Form Controls that build up the form and the Form Web Part that is the base of loading plus saving the form, then you can make them as complex as you want. If you want the form to be in different style, then build different Form Widgets. If you want multiple fields that result in a single field of content, like an Address, then build a custom Form Control. The ideas are only limited by you the Developer or the Content Editors ideas.

The downsides I have seen are where the Content Editors have a lot of great simple ideas. I have been given tasks of adding a new type of button or adding a new content area to a Widget. Although we are empowering them, we also still need to provide the tools to them, which aren’t always the most inventive ones. There is also a balance between empowering them and giving them the code. You could expose all the customisable features of the Web Part, like a button so the colour, size, wording, features but then it’s a huge amount of work for one button. This would then put them off using it, however the other way of closing it down can then put more tasks on you.

Another challenge is what you can customise and what you should. Kentico’s recommendation for when you are customising anything, is if it is default to Kentico then clone it and customise the clone. This is so if or when you need to upgrade the CMS, you haven’t broken anything that the upgrade will use, plus it could get over written when the upgrade is then place. Even though Kentico is full customisable, the method in which it performs some task might not be how you like and at best practice you need to leave them how they are.

Final thoughts?

Although there are downsides to using a CMS like Kentico, I think any method of using a CMS will have its downsides. I feel with this set up I am more looking at improving the code, myself and the product, rather than doing the same task each time.

What CMS do you use and do you think it helps you become a better developer, comment below?

All for one and one for all browser

The basic thing of website developing is to make sure you cross browser check your website across all browsers, even if you don’t like them. The biggest pain in every developers ass is the Microsoft Internet Explorer. What if though, you can have one browser or at least one browser standard?

Internet Explorer(IE) has got to be the worst for this only because Microsoft do not see the point in upgrading their browser automatically. Just about every other browser does this so when you are hit with a very non-technical person, then they are still up to date. I do see why they don’t in a way, because a lot of corporate companies used the older versions to support applications. If they do not update the application then the browser cannot be updated just in case it doesn’t support it. Upgrading everything could cost the company much more compared to supporting what is already there. This was the same, and the same company, with the operating system Windows XP. This system was integrated fully into companies systems so they couldn’t change to newer, fast and more reliable operating systems as they would not be supported. Although unlike the versions 7 and lower of IE, the operating system was fully supported by Microsoft, up to recent.

The issue with browsers not keeping update, which is not just IE, is you can’t use all the new updates to website developing code. Thing like CSS3 was not fully, or with some browsers, not at all supported, so when you do use a fantastic trick that looks great it doesn’t in that browser. Most will just not use it and resort to the lesser version, like border-radius. This would put a rounded corner on the selected element, but in browses like IE version 8 it would be square corners instead. This although is not perfect as it looses the great effect, it still works well as the content would be readable. However if you use HTML5 code like a <header> tag, that is now the best way to show contextual code and give the search engine more readable code, it would fail. What will happen is the opening tag will try and self close itself by adding a closing tag, but right next to it. The closing tag you entered then also does the same, but adds it in front of it. This of course breaks the website as the content is now not contained in that tag. This is where new coding technology is wasted.

//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js

(adsbygoogle = window.adsbygoogle || []).push({});

There are a lot of work around that smart developers have came up with including HTML5 Shiv and some JavaScript that that can, for that particular problem, replace the HTML5 elements with replacements like div. Some content creators instead choose the most brutal methods to get round the lack of support by just not supporting it. By this I mean they tell clients that they do not support a particular version of the browser. This can then push people to upgrade or most likely to avoid that website. This is not the best and being a good developer you should support your client as they are obviously the customer. Others even go for only supporting a particular browser, though some are due to the company who is doing it. For example Google did a Lego WYSIWYG builder, which was awesome, but it was only on the desktop Chrome browser.

So other than finding more hacks to support the new and best technologies how can we fix this problem? The method I think could be great for developers would be the ultimate one browser for all, but this of course would not work for the browser companies.

On the pro’s for the developers it would remove the need to cross browser test as you would only have one browsers. The website developing standards would be up to date so there would be no compatibility issues and then finally if the builder or community allowed it, the browser would automatically update to keep up to date with the standard. From this there would be no compatibility issues, speeds up developing with no cross browsing testing and updates would be, well up to date.

The con’s comes under one big thing, competition. If you only get one browser then there is no competition to push the development of browsers and their technology. Most of the HTML and CSS standards and developments are pushed by developers wanting easy ways to develop, but the browser need to be pushed as well. The other thing with one browser, so who supports it? Google is not going to support the browser if it is not Chrome with Google branding and the others wouldn’t support the browser if it was branded as it isn’t by them. This browser would have to be community driven, but then it wouldn’t last with out funding and then where do you get funding etc.

//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js

(adsbygoogle = window.adsbygoogle || []).push({});

So as a result I think an all in one browser would be great for developers, but bad for the companies that drive them to pioneer technology. It would result in more damage then good, but then how do we combat the browsers like IE that don’t want to update? If these browsers were to keep up to date and to keep up with standards then I think there would be more technical advanced websites with more creativity as we will not be held back.

Tell us what you think, would you want an all for one browser or do you think there is a better way?