This blog has, IMO, some great resources. Unfortunately, some of those resources are becoming less relevant. I'm still blogging, learning tech and helping others...please find me at my new home on http://www.jameschambers.com/.

Sunday, February 13, 2011

Mixin’ it up

Bootcamps. Keynote. Sessions I’m actually interested in. Meeting folks that I’ve only ever known online.  Should be a great event!

Tuesday, February 8, 2011

Parents, Open Your Eyes

I just read this blog post from over on ZDNet: It's time for iTunes and Xbox Live to put spending limits in place.  Mr. Adrian Kingsley-Hughes writes that:

There’s a fine line between something being lucrative, and starting to feel scammy. Apple is certainly sailing close to the wind with some of the in-app purchases it is allowing, and Microsoft could certainly do more to prevent this kind of bad publicity. [...] And it’s clear that the current mechanism of relying on parental controls isn’t enough. Both companies need to get their act together.

I would agree that parental controls are failing – couldn’t that be said about a lot of things? – imagebut we would stop short of flogging a parent who pulled off the same stunt at Wal-Mart and then tried to blame the store management. 

Could you imagine walking your child into the store, giving them a shopping cart and meeting them at the checkout – at which point you blindfold yourself and let the check-out clerk scan all items…and then your credit card?  And you don’t even check?

Look, I’m no fan of Apple, but I don’t think they (or Microsoft) are in the wrong here.  My wife has disabled in-app purchases and we don’t share our password for the iPod Touch or the Windows Phone 7 devices we own.  Not going to happen.

Our kids just don’t get unchecked access to our credit cards.  Period.

While Mr. Kingsley-Hughes refers to it as ‘insanity’ and points blame to the companies in question, I refuse to be an enabler for parents who want to make stupid, unconditionally blameless children.

Monday, February 7, 2011

Dedicated Back Button: Genius

My wife has an iPod Touch and we were running through some of the same apps that exist on iOS and Windows Phone 7.

At several points we both accidentally clicked the wrong link/button/UI element etc. on our own device.  There was one glaring difference, however; the iOS versions often land you in a place where you have to navigate through the home screen and back into the app where you accidentally made the click.

Not so, with Windows Phone 7: there’s a dedicated, hardware-based back button.  This makes it easy to back up through your screen progression to the last place you were at.

It’s so simple that you think that it wouldn’t be necessary, and yet, there she is…my wife pushing the empty space on her iPod like there should actually be something useful there.

Monday, January 31, 2011

Windows Phone 7 Wishlist #2

So I’ve been playing with the phone now for over two weeks now and having a great time showing it to my friends. They are shocked that it’s a Windows phone. I have two friends, in particular – one from Vancouver and one from Toronto – who were in town for this past weekend and are huge iPhone fans. image

Between the two of them they’ve owned every iteration of the iPhone, including their now current iPhone 4. They put their phones down and were fighting over my Samsung Focus.

The one was quite miffed that the Rogers rep at the mall didn’t demo it for him as he just picked up his iPhone 4 three weeks ago.

Things I’ve Noticed

  • When you give the home screen a little inertia to let it scroll, then repeatedly hit the Windows button, you can make the tiles skip and keep rolling from the start. This turned out to be a fun game for my kids, who have contests by pinning all the apps to the home screen and then seeing who can be the first to scroll-home-tap and give it enough momentum to get to the bottom of the list.
  • I tend to hit the back or search buttons when I’m using the camera. I am so used to holding my other phone that had no buttons in that area that my fingers get in the way. This isn’t so much a WP7 problem as a Samsung Focus problem. It happens enough to be annoying but not so much that it’s a problem.
  • Same thing with the volume and power, which are on exact opposite sides. You use the phone for leverage on the opposite side to push the button…with unintended results.

Things I’d (Still) Like to See

  • Video sharing
  • Zune Pass and music in the Zune marketplace for Canadians (I’ve already mentioned this, but it’s worth repeating)
  • Updates to the Facebook app – (doesn’t really seem to do it’s work in the background)
  • Some way to organize the apps list. I love the tiles, I love the way the other apps are off to the side, but what would really work is to carry forth the Metro metaphor and allow me to make a panorama out of the apps, with categories I can set. I wouldn’t even mind – at first – if I had to do it in Zune.

Thursday, January 20, 2011

ASP.NET, MVC 3, the Razor View Engine and Google Maps

With the release of MVC 3, the updated project templates and the Razor view engine we have several new features and tool improvements at our disposal.  I wanted to see how hard it would be to get a quick mash-up of some ASP.NET goodness and the Google Maps API.  For this project, I’m going to walkthrough creating a new project in ASP.NET using MVC 3 in Visual Studio 2010.  You’ll need to have the RTM build of MVC 3 (which contains other ASP.NET enhancements, btw).

Getting Started

Once installed, it’s fairly easy to invoke the New Project dialog and select ASP.NET MVC 3 Web Application from the Web templates.  If you don’t see the project type listed, make sure that you are targeting the correct framework.

image

I named my project RazorMaps.  When we click ok, we are then presented with a bit of a wizard to select our options for the project.  This is a simple but good improvement over the previous MVC bits because it rolls up the wizard, test project creation and multiple templates (empty versus the “internet application”) and lets us select the view engine.

image

imageFor simplicity sake here I’ve just selected an Empty template and no test project. 

Most of the project pieces feel familiar, in fact, there’s no noticeable changes to the MVC project at a root level and all the expected folders are there.

It’s nice to see that jQuery is added by default to the _layout.cshtml file, and to see that jQuery UI is included in the project.

As at my writing this, it’s also the latest bits for the jQuery library, and they have the vsdoc file updated (AWESOME! No more renaming the old one and replacing the library!), and there are other libraries in the mix as well (enabling unobtrusive validation, etc.).

A Simple Home Page

We need to get a view set up so that we have something to look at.  The base project doesn’t include the controller or the view we’re going to want, so we’ll start there.

Right-click on controllers, Add > Controller and name it HomeController.  We get our basic controller up-and-running and the code for our first ActionResult (our home page) is free:

image

Next, right-click anywhere in the Index method and select Add View from the context menu.  If the View Name is Index then you shouldn’t need to change anything else.  We’re using the Razor view engine and we already have our layout specified in _viewstart.

image

Press F5 to see your lovely work!

Hello, Google Maps

We have a couple of things that we need to do to get the basic, no-frills map up-and-running.  Though I worked from the simple tutorial on the Google Maps Javascript API page, I’m going to try to MVC this up as much as possible and look at some of the features of the Razor view engine.

We know that we need to add the Google Maps script to our page, and we can see from the tutorial that you also need to have a couple of styles.  Because we are using layouts in Razor and don’t want these on every page we create, we’ll take advantage of Razor Sections in our layout.

Open up _Layout.cshtml and add the following lines of code to the <head> portion of the document:

@RenderSection("Scripts", false)

<style type="text/css">
    @RenderSection("Styles", false)
</style>

We’re adding two optional RenderSections here.  This is very similar to creating content sections when using master pages.

Next, pop back into our index.cshtml file so that we can add the juicy bits to get the map loading.

Fleshing out the Page

Right at the top of the file, you’re going to want to add the following code:

@{
    ViewBag.Title = "MVC 3 and Google Maps";
}

@section Scripts {
    <script type="text/javascript"  src="
http://maps.google.com/maps/api/js?sensor=false"></script>
}

@section Styles {
    html { height: 100% }
    body { height: 100%; margin: 0px; padding: 0px }
    #map_canvas { height: 80% }
}

The ViewBag is a dynamic expression and evaluated at runtime.  The “Title” property is filled into our layout template with the @ViewBag.Title expression. 

Next, we have our two sections; the first adds the map script to our page, the second applies and specifies the styles we need to get the map rendering correctly.

The rest of the page consists of:

  • A div element (to host the map)
  • An initialize function that sets up the map, and
  • A jQuery shortcut to invoke the initialize function when the page is ready

Here’s the code:

<h2>Hello, Google Maps</h2>

<div id="map_canvas" style="width:80%; height:80%"></div>

<script type="text/javascript">

    function initialize() {
        var latlng = new google.maps.LatLng(40.716948, -74.003563);
        var options = { zoom: 14, center: latlng, mapTypeId: google.maps.MapTypeId.ROADMAP };
        var map = new google.maps.Map(document.getElementById("map_canvas"), options);
    }

    $(function () {
        initialize();
    });
   
</script>

And there you have it!  Press F5 again and the map loads.

Conclusion

If you have a good handle on ASP.NET MVC and you understood MasterPages, the switch to the Razor engine and templates is going to be easy and refreshing.  Integrating per-page code, via Razor section rendering, is straightforward and keeps our templates clean.

Updated script libraries and improved templates and a ton of feature enhancements round out this release nicely.

Wednesday, January 19, 2011

Built-In Authentication and Authorization Providers in ASP.NET with the MVC Framework

If you’re not already doing so, you should seriously be using the the built-in Auth & Auth in ASP.NET. While the subject is fairly well covered, I continue to get several questions and comments related to creating accounts, logging in and permissions and when talking with other developers. I am shocked at how many still roll their own authentication and authorization bits – often for no better reason than not knowing how great (and FREE!) the default providers are.

The Basics of Authentication and Authorization

There are two things you will need to do on most web sites with “account” functionality: identify existing users based on provided user names and passwords (authentication) and then express privileges to control access to protected resources (authorization).

These two facilities allow us to do some creative things, like showing different content to users when they are logged in, or to restrict and redirect requests based on the logged in user’s set of privileges.

In ASP.NET, Auth & Auth are Free

The first official, for-a-customer e-commerce web site that I notched on my belt was back in 1997. I spent days creating a system to log users in, store cookies (that I do not care to discuss security about!), store user information, track log-ins and the like.  Each page that I wanted to protect with security meant checking cookies for certain keys, looking those keys up, then checking against a static set of rules for permissions.  I spent weeks fixing the broken parts.  Because most of it was wrapped up in per-page scripts, I had to essentially recreate the whole mess when the customer decided they wanted to self-administer the storefront.

Today, so much of that mess is cleaned up for us.  I want to give you the steps to set up Authentication and Authorization in your next MVC web site.

  1. Open Visual Studio 2010 and create a new ASP.NET MVC 2 Web Application.

Yeup.  We’re done.

Walking Through What’s There

image

If you are familiar with the MVC pattern the default project is quite straightforward, but not entirely trivial.  There is no magic here, just convention and after working with the project for a few minutes you should be able to orient yourself.

The three basic concepts of MVC – Model, View and Controller – are expressed as classes and .aspx pages (as well as .ascx for partial pages and templates).  As part of the convention, each part has it’s own directory, and in the View folder we have subfolders for each controller.

The Project Components

What is relevant to us today are the items related to user accounts. You can see that there is an AccountController for us as well as the classes we need to log a user on in AccountModels.  The Account subfolder in Views gives us four pages dealing with account creation, maintenance and sign on.  Finally, the Shared subfolder in Views has a LogOnUserControl that displays different content based on the authentication status of the user.

At this point, we’re actually still missing a couple of pieces.  If you drill into your App_Data directory you would find that there is no database to hold your account data.  Thankfully, we don’t need to do much to create one; namely, we just use the site.

The Registration Process

Press F5 to start debugging the application.  When the site opens up you’ll see the default master page and index in action:

image

Follow the Log On link up in the top right corner of the page.  This will take you to a page with a form to log on, but also a link to the registration page. Follow that, and create an admin account.

imageOnce you’ve created your account you can return to the App_Data directory and see that the database has been created (you may need to click the show all button in the Solution Explorer).

The show all button looks like this: image

Authorization In An Attribute

This is where the easy kicks in.

To add authorization to your application you can make use of the attributes available to us in the ASP.NET MVC Framework.  It is as simple as adding one line of code to your controller.

Let’s turn our about page into something that only authenticated users can view.

image

Perfect! The [Authorize] attribute describes the controller action as something that only users who have been authenticated can access.  And now, when we load up our site, if we try to navigate to the About page prior to logging in you will be redirected to the login page.  To prove this, start debugging the site and add /home/about to your URL.  You’ll see this:

image

After logging in, you can see the About page in all its empty glory.  In fact, because your request was originally for the About page, the default AccountController pushes you through to that page once you’ve authenticated.

image

Some MVC Sweetness

Views in ASP.NET MVC inherit from the System.Web.Mvc.ViewPage object and therefor expose some interesting objects for us that we don’t have to work for to use.  Taking advantage of this fact allows us to shortcut to some features right in our views.

For example, the User object on our ViewPage allows us to test for authenticated users:

image

Or, we can test to see if they belong to roles in the ASP.NET membership/role provider:

image

While this can be super handy, it’s important to consider coding practices and whether or not your logic for such elements should be in your controller or your view.  This post will not enter that conversation, but it’s important to note that a similar roles-based approach is just as easy inside your controller:

image

Oh Yeah, About Those Roles

The easiest way to get roles going would be to navigate to the ASP.NET Configuration site.  You can enable and define roles from there:

image

This built-in administration tool simplifies the process of enabling roles on your site, adding existing users to roles and/or creating roles and bringing in existing users.  It also allows you to add a role to a user as you create them.  It’s tidy and functional, but doesn’t have many bells and whistles.

This isn’t the best for a production environment as the ASP.NET Configuration site is not deployed when you publish your app.  One solution would be to use a community-based console to help administer the site post-launch such as this one.

Some Reading Homework

I recently had the privilege of working with one of the authors of this book (Stephen Walther) and I can attest to the fact that these guys know this stuff inside and out.

ASP.NET 4 Unleashedcontains a ton of great information on using and abusing the .NET Framework when working on web applications, including a section devoted to the membership framework.

Conclusion

We’ve come a long way in web development.  Tasks that used to require “rolling your own” and a day or a week of dedicated time can now be reduced to simply clicking on “New –> Project”.

The beauty in that is that way that we can fully customize those bits, integrate them with our existing auth/auth stores and more.  We can choose our view engine (classic ASP.NET, MVC or now Razor).  We can integrate jazzy Ajax features through the fully supported jQuery libraries and its good-looking sister, jQuery UI.

It’s a good time to be a web developer.

Tuesday, January 18, 2011

Windows Phone 7 Wishlist

I have the Samsung Focusand am quite happy with the phone.  I’ve been using it for just about a week.  The device is the device and can’t change; it’ll be what I use for the next two years or so unless WP8 comes out and I can get a hardware upgrade.

However, this is still a first-generation iteration and there is room for improvement. Here are some things that I would like to see as far as changes to the platform go:

  • Give Canadians Zune Music in the Marketplace, as well as Zune Pass.
  • Build a way to keep the elegance of differentiation with email account notifications (on the lock screen) but unify the inbox. I am going to prototype what I would like to see here.  I want just one email tile on my home screen.
  • Copy and paste, as well as better cursor placement.
  • Let Canadian customers buy music in the Zune Marketplace, and let us purchase a Zune Pass.
  • I want a first-party app for Live Messenger that runs in the background and runs as quickly as email and text messenging.
  • Enable uploads for video to my Live account, Facebook and YouTube.  Give me an easy way to share (like, “Post to YouTube and Share on Facebook”).  Give me the option to only do this when I’m on WiFi (but to queue it when I’m not).
  • Finally, make music from Zune available for purchase to Canadian customers (and enable Zune Passes in Canada, while you’re at it!).

The first update to WP7 is rumored to be coming in the next few weeks.  I am looking forward to seeing what on my list makes the cut.