In part 1, I linked to a few blog entries about getting Basic authentication to work with AngularJS. I outlined what was required to make the suggested idea work in ASP.NET WebAPI, but I didn’t detail what was required in the AngularJS client. The blog entries that I linked to were also very light on details, so I thought I’d put together this blog post to show an end-to-end solution for this problem.
This project is available on GitHub at https://github.com/cerkit/BasicAuthWebApiSample.git
I’m missing a few features that were intended to be included due to my newbie status with AngularJS. In particular, I do not currently have a way to save the failed requests for later to retry them after the user successfully logs in. That will probably have to wait until I get better with AngularJS. Also, there may be a better way to inject the credentials into the web request other than putting the code into the controller. I’ll try to improve on this model at a later date.
To start, let’s take a look at the .config for the app.
Notice that I add a header on line 6 that sends the XMLHttpRequest to the server. If you’ll remember from the last post, this will cause the server to send an “xBasic” authentication method, thereby bypassing the browser’s built-in security dialog. This will allow us to answer the 401 Unauthorized response with our own login dialog.
Here is the template for the login dialog:
This contains the modal class that the dialog requires to be a modal dialog. In order for the dialog to be opened, a request has to be made to an $http resource. This is done by the main controller, but the actual work is done by an $httpProvider interceptor. You can see the interceptor definition on lines 8-36 of the above code. Notice that when the status of a request is 401, it fires the “auth-loginRequired” event. This indicates that the dialog needs to be shown. This event is answered by a directive that then launches the dialog. Here is the directive:
The CredentialsController performs the login:
Notice that the login method calls the event for “auth-loginConfirmed”. This will cause the modal dialog to be hidden. It is also answered by the SampleController so that it can refresh its data.
We’ll then see the login-dialog directive being used at the bottom our index.html page. This is there just so that we can respond to the event and show the dialog.
Since we’re using the SampleController, let’s take a look at what it does to get the data:
The refreshData() function checks to see if there’s a credentials cookie. If so, it sends the credentials along in the Authorization header. This is the part that should be changed. I think it needs to go in the interceptor, but I haven’t quite figured out how to do it. After setting credentials, the controller has various functions for dealing with the data. It also sets the currentUser property on the scope so that it can be displayed on the home page.
This should illustrate the end-to-end process of enabling basic authentication on an AngularJS application when used with ASP.NET WebAPI.
I’ve been very happy with the results. Just make sure you secure all of this behind SSL.