One of my favourites things to fiddle with are home web servers. In fact the oldest post from this site (ported from previous websites) was about this very subject. This has always been a “minimal cost” hobby, in that I have tried to use old hardware I already have, and free services for things like domain names. The only thing I wasn’t getting for free with these setups was the electricity. So I wondered, by accepting a small initial outlay, whether it was possible to create a server with zero ongoing running costs.
Just recently I purchased a Raspberry Pi Zero and used it to create my latest web server. At the moment it is running from the mains electricity, but I am planning in the near future to try and run the thing on solar power alone. Anyway, I will probably write more about the project on the site being run by the Raspberry Pi at some point. You can check out the site here.
After playing around with Bitcoin exchange rate APIs today, I’ve created a website that displays exchange rate information from blockchain.info. I still need to add some more functionality to the site so it provides all the information I typically need relating to Bitcoin prices in one place. The site can be found here.
Just recently I was introduced to Free Code Camp and have started on their path to Full Stack Development Certification.
The great thing about Free Code Camp is that, towards the end of your certification, it gives you the opportunity to work on real projects for nonprofit organisations. This clear path to applying your studies to real world situations is a great motivation and something a lot of other online courses do not offer.
I’m currently whizzing through the first batch of tutorials, which cover things I’m already familiar with. You can check out my progress here.
If you are reading this then you are probably already aware that, out of the box, there is nothing to stop a user logging in multiple times with the same account details in .NET.
This can be a problem if your users are paying to consume a service from you, as they could share their login details with their friends. Or perhaps you have a process that just doesn’t play nice with multiple logins.
I’ve tried to keep this solution as simple as possible. We store minimal data to check whether a user’s login is current, and use .NET’s built in account controls by overriding part of the AuthorizeAttribute class.
This code works in MVC5, but can be adapted for use in Web Api 2 with minor changes.
To quickly outline what the code is intended to do: we will assume that all user logins are valid and therefore the most recent login is where the user is, and that all other logins are stale. We do not want to force a user to manually log off previous sessions (which would be very annoying) but should simply log them out, leaving only the current session active.
To do this, we’ll create a database table to hold users’ login details. Each time a user logs in, we’ll check if they are in this table. If they are not, we’ll add them. If they are, we’ll overwrite their existing details with their new login details. This means the table will always contain only the details of their most recent login.
Later on, when we want to check if their login is the most current, we’ll compare their credentials with what we have in the database. If their details match those in the database, it is still their most recent login and they can proceed. If their details have been overwritten due to a more recent login, we know the session is old and can mark it to be logged out.
This holds the data we are going to store to keep track of users’ logins. I’ve seen solutions that keep track of every login a user makes, but we really don’t need that much information. We only need to keep two details: the username and some unique identifier for that session. We will only ever store details of their most recent login, so we don’t need to record, for example, whether the session is active or not. Here, I have added a Date field as well, although it is not required.
This class interacts with the Login table we’ll create in our database and only requires two methods. The first, PutOrPostLogin, will either add the user to the table or, if they are already in the table, overwrite the information with their new login details.
The second method, IsLoggedIn, simply checks if the username and unique id that are passed to it exist in our database. If the information is there, we consider it to be their current log in but if it’s not, they need to be logged out.
At some point during the login process, we need to store the user’s login details in our database. Here, in the AccountController, after a successful login, we create a new Login object and assign the users email and current Session ID to it. There are a number of options you can use for a unique identifier here. It’s worth noting that, if you’re going to use the Session ID, you need to include the line Session["sessionid"] = HttpContext.Session.SessionID; or the Session ID will not persist, and each time you check it you’ll get a different result back. Lines 5-11:
The AuthorizeAttribute is used in MVC to check whether users are authorised to access restricted parts of your website. We can extend this class and add in an extra check to see if our user should be allowed to remain logged in.
The method we need to override is AuthorizeCore. In Web Api 2, the method you would need to override is the perhaps more sensibly names IsAuthorized.
So, the first thing we do is call the overridden AuthorizeCore method from the base class, so that the standard authorisation checks can be done. As a safety net, if we encounter a problem and either the Username or Session ID are null or empty, we’ll give them the benefit of the doubt and just return the result from the base class.
Assuming we do have a Username and Session ID, we then just need to call our IsLoggedIn method, which will check if their session is the most recent, i.e. whether it should be considered active, or not. The AuthorizeAttribute will do the rest of the work for us and log out the user if false is returned.
I had been thinking about rebranding for a while, as my old domain name was far from unique. A quick Google search showed I shared my name with, amongst others, a large Pakistani bank and a Viennese band that describe their genre as “funk funk funk”.
So after nearly a year of using a different domain, I have changed to johnskdev.com. Whilst it doesn’t exactly roll of the tongue, it is descriptive and more importantly it was not taken as a username on any of the major social networks or forums I frequent. This meant I could have one consistent name across all these sites.
It was particularly satisfying to Google my new name, shortly after I had registered the domain:
That’s about as unique as you can get!
Of course, I needed to move my WordPress installation from my old domain. I had transferred WordPress sites between servers before (keeping the same name), but not moved to a new domain. It turned out to be fairly straightforward, however, and WordPress themselves provide a useful guide on this subject.
I have retrospectively changed names in previous blog posts to try to ensure consistency, but you may find the odd reference to the previous domain on here.
As I explained in my last post, I am currently learning all there is to know about the .NET Framework.
I can get a bit bored creating test sites on my local machine and much prefer to see things working and live on the internet. As such I’ve created a new sub-domain, net.johnskdev.com, that points to an Amazon server where I have deployed a test MVC 5 site.
This is going to be my testbed for everything I’m learning in .NET, so don’t expect it to look like a polished website. In due course, a lot of examples and experiments will appear here, but at the time of writing this, the site is just the default one you get when you create a new MVC project.
I would write a guide on how I set this up, but I don’t think I could improve on this great article that takes you through all the steps, from setting up an EC2 instance to uploading your .NET project. The only problem I encountered was that Web Deploy didn’t seem to install correctly on my server, so I would suggest also reading through this IIS article on installing and configuring Web Deploy. It’s written for IIS 7, but it worked fine with IIS 8.
When I last wrote back in July, I had just finished my second Android course and was due to start the third and final part of a MOOC specialisation on Mobile Cloud Computing with Android. Things have changed quite a bit since then, so I thought I’d just explain what has been happening.
At the end of July I started working at a startup in Sydney as a Web Developer. This role involved working with, amongst other things, .NET, MVC 5, C#, LINQ, Entity Framework and the Razor View Engine. I didn’t have very much experience in these areas but was very keen to learn, so I basically put a hold on my current MOOC courses so I could focus fully on my new role. I’m really pleased to say that the firm I’m working at recently offered me a permanent position, so for the foreseeable future I’m going to be learning more about C# the .NET framework.
I’m a big fan of minimalist web design. Here are five of my favourite free WordPress themes that have a clean, minimalistic look. They don’t rely on big photos to make an impact (although most look good with photos too) and so are ideally suited to individual blogs that have a lot of text.
When I started this site I wanted a clean, simple, flat design which, of course, also had to be responsive. I picked Tiny Theme as it fitted this criteria and looked great on mobile devices. The only real gripe I had with it was that on a full size computer screen, it looked a bit too simple.