Configure DNS Aliases in IISExpress for localhost

A direction we are taking Process PA is white labelling for our partners. To do this we are still hosting on the same scalable platform on Microsoft Azure and essentially adding another level of multi-tenanting in our codebase. When a user navigates to https://app.processpa.com/ they will get our first party branded experience. However, when a user navigates to something like https://partner1.processpa.com, they will get a tailored experience complete with branding, different resources, templates and features specific for those customers supported by our partners.

Building out the experience using Dependency Injection with Branch By Abstraction based on the host is reasonably straight forward. I however wanted the local development environment to be run in the same way, rather than switching in code for whatever partner experience was being built out at the time. To do this I need to be able to configure local DNS records for localhost and IISExpress. The answer I found on StackOverflow but below are the simplest steps that worked for me.

  1. Open Notepad as an Administrator
  2. Open in Notepad C:\Windows\System32\drivers\etc\hosts
  3. Copy the comment lines for localhost host and add as your DNS records
    127.0.0.1    partner1.localhost
    ::1          partner1.localhost
  4. For Visual Studio 2015, open <Solution folder>\.vs\config\applicationhost.config
  5. Find the site and add the bindings you need, making sure to change the ports.
    <bindings>
    <binding protocol="http" bindingInformation="*:1704:localhost" />
    <binding protocol="https" bindingInformation="*:44300:localhost" />
    <binding protocol="http" bindingInformation="*:1705:partner1.localhost" />
    <binding protocol="https" bindingInformation="*:44308:partner1.localhost" />
    </bindings>
  6. To prevent the need to run Visual Studio as admin to start IISExpress on those ports, open an Administrator command prompt and run:
    netsh http add urlacl url=http://partner1.localhost:1705/ user=machine\username
    netsh http add urlacl url=https://partner1.localhost:44308/ user=machine\username

If you have certificate trust issues check out: https://blogs.msdn.microsoft.com/robert_mcmurray/2013/11/15/how-to-trust-the-iis-express-self-signed-certificate/.

And that’s it! When you go to https://localhost:44300 or https://partner1.localhost:44308 you get the same application with a different experience.

Advertisements

Unexpected Bots and reCAPTCHA defense

A few weeks ago I soft launched my product to allow for direct sign ups from the public web site, Process PA. We’ve been running for a few month with just our foundation customers making sure things are running well before letting anyone register without interaction. I updated the web site without announcing to just test the process a bit before (hopefully) driving more traffic to the site.

Interestingly, I started getting random sign ups registered in the database. Many had even verified their email address. I hadn’t been directing people to the site yet and where we are now we don’t get many unknown visitors. Sure enough, it appears they are bot accounts. Quite surprising that bots are onto new sites quickly and filling out registration forms. I’m not sure what they expect out of it.

CAPTCHA Required

This is kind of a pain. While building a startup I have many things to do. And stopping bots this early on I didn’t think would be required. Fortunately putting CAPTCHA in place is pretty quick. However, I started using a very popular NuGet package BotDetect CAPTCHA. Although implementing was easy it results in those horrible user experience that everyone hates. I did not want to add any friction to legitimate sign ups.

Although the BotDetect CAPTCHA claims “not one confirmed case of automated CAPTCHA breaking by spammers” I’m sceptical. I did a thesis on vision processing over 10 years ago and it’s gotten much better since then. Spammer may not be breaking them, but Google states, “it can decipher the hardest distorted text puzzles from reCAPTCHA with over 99% accuracy”.

Google No CAPTCHA reCAPTCHA to the rescue

You’ve seen it across many websites now. Launched December 2014, this provides the simple ability for the user to check the box that says, “I’m not a robot”. And they are, most of the time, done. So much better for the user. So much harder for the bot.

Implementing is very easy from the instructions on the admin site which contains your keys. Get started at Google reCAPTCHA. Client side is a script include and a div. Server side is a web request. It is even simpler nicely wrapped up as an attribute from the NuGet package reCAPTCH.MVC with clear instructions on their project site.

With it all in place now, it looks like I’ll be needing human customers to keep up the sign up rate now that the bots aren’t allowed in. If you are tired of doing minutes and governance manually for your association, club or board come and try out Process PA.

Database unable to be recreated after delete with Entity Framework

Nice little gotcha when getting started with Entity Framework using the built-in ASP.NET MVC templates. Wanting to start from a fresh database using the MSSQLLocalDB there is a few little things to do that aren’t necessarily obvious:

1. Delete the mdb and ldf

Go to the App_Data directory and delete.

If the delete fails, make sure you stop IIS Express which is holding a handle to the files.
Stop IIS Express

If the delete still fails, make sure you have closed connections in the server explorer in Visual Studio.
Close Server Explorer Connection

Still failing to delete? Go to Task Manager and kill the SQL Server Windows NT [sqlservr.exe] process.
Kill the process

2. Run Update-Database from the Package Manager Console

Update-Database

Fails with “The EntityFramework package is not installed on project ‘####’.
Check the default project is set to the correct one which contains the Migrations folder. Mine keep defaulting back to the Tests project.

Fails with “Cannot attach the file *.mdf as database”.
I found the answer on StackOverflow. The default connections string contains the Initial Catalog property. Which is all good for a full SQL Database but not LocalDb. Removing that property and it is all good.

Summary

Entity Framework seems quite good with Code First Migrations. I find the documentation really hard to get up to speed if you are coming new to it. Much of the documentation seems to focus around the difference and pieces together what to use for the latest version wasn’t the simplest. There is a bunch of little things you need to be aware of and practice with Add-Migrations and making sure everything is upgradable properly. Persisting with it [pun intended] has been worthwhile so far…

Capture IIS Network Traffic in Fiddler

I have an IIS application that is querying Azure Active Directory Graph API from the server. I wanted to capture what is requests are happening using the client API. By default Fiddler does not capture these requests. Fiddler inserts itself into the WinINET layer as a proxy which is bypassed by IIS outgoing traffic.

To capture these requests  coming from an IIS application pool. Add to your web.config after the <configSections> element:

  <system.net>
    <defaultProxy enabled="true">
      <proxy proxyaddress="http://127.0.0.1:8888" bypassonlocal="False"/>
    </defaultProxy>
  </system.net>

where 8888 is the fiddler listening port, found in Tools > Fiddler Options

image

This can be found in the Fiddler documentation.

Happy web traffic debugging!

Adding Microsoft PubCenter Ads with AdDuplex fall-back on Windows Phone 8

DVLUP has a challenge Shamelessly promote your apps with AdDuplex. AdDuplex doesn’t earn you any income so why would you want to give ads to your users without any benefit to yourself? AdDuplex answers this succinctly “According to reports the average fill rate in mobile ad networks could be as low as 10%. AdDuplex helps you utilize 100% of your ad space, so even when you are not making money from the other ad network you get free promotion for your app.”

It is encouraged to use an ad network with fallback to AdDuplex and It only takes 5 minutes to add to your app.

  1. Add references to Microsoft Advertising and AdDuplex
    a. Install Microsoft Advertising SDK, if not already, and add reference
    Reference to Microsoft Advertising SDK
    b. Install AdDuplex via NuGet Package. Enter into the Package Manager Console:
    Install-Package AdDuplexWP8
  2. Add references to the namespace in your Xaml page you want to display the ads

    xmlns:ad="clr-namespace:Microsoft.Advertising.Mobile.UI;assembly=Microsoft.Advertising.Mobile.UI"
    xmlns:adduplex="clr-namespace:AdDuplex;assembly=AdDuplex.WindowsPhone"
  3. Add controls so that the AdDuplex banner is under the Microsoft Advertising banner.  
    <adduplex:AdControl x:Name="adDuplexAd" AppId="12345" VerticalAlignment="Bottom" />
    <ad:AdControl Name="photoFoodDiaryAdControlRecordPage" Width="480" Height="80" VerticalAlignment="Bottom"
            IsAutoRefreshEnabled="True" IsAutoCollapseEnabled="True" 
            ApplicationId="test_client" AdUnitId="Image480_80" />

    AdDuplex requires that the complete banner is not hidden. With this setup, if the Microsoft Advertising banner fails to get an ad the IsAutoCollapseEnable will set Visibility Collapsed showing the AdDuplex banner.

  4. Generate Ad ids

    a. In WP Dev Center publish or update. Add in-app advertising and Generate ad unit ID. 
    Add in-app advertising

    b. Sign up to Ad Duplex if you haven’t and add a new App.

    AdDuplex New app

  5. Add the required capabilities to your WMAppManifest.xml

    Capabilities
  6. Since the ad network doesn’t always return an ad, to test your ads check for any errors occuring, and set the ApplicationId and AdUnitId to the test values from here. Simple add a break point in the method and inspect e.Error.Message if the break point is hit.

    ErrorOccurred="AdControlRecordPage_ErrorOccurred"
    private void AdControlRecordPage_ErrorOccurred(object sender, Microsoft.Advertising.AdErrorEventArgs e)
    {
    }

    e.Error.Message

Now you should see the Microsoft test ad. Remember to switch you ApplicationId and AdUnitId back to what you got from the Dev Center and you are done.

Microsoft Test Ad

Technorati Tags:

Adding Localization to a Windows Phone app

If you have any Windows Phone 7 apps from the original templates, unlike the Windows Phone 8 app templates they did not come with localization setup already. So lazy me just wrote the strings inline. Fortunately adding localization after updating to Windows Phone 8 is easy.

  1. Add a Resources folder
  2. Add new Resources resx to the folder named AppResources.resx
    AppResources.resx
  3. Ensure Custom Tool is set to PublicResXFileCodeGenerator
    AppResources.resx Properties
  4. Add a new class named LocalizedStrings to the root of your project
    using YourAppNamespace.Resources;
    
    namespace YourAppNamespace
    {
        /// <summary>
        /// Provides access to string resources.
        /// </summary>
        public class LocalizedStrings
        {
            private static AppResources _localizedResources = new AppResources();
    
            public AppResources LocalizedResources { get { return _localizedResources; } }
        }
    }
  5. Add an Application.Resource of your LocalizedStrings class to your App.xaml

        <Application.Resources>
            <local:LocalizedStrings xmlns:local="clr-namespace:YourAppNamespace" x:Key="LocalizedStrings"/>
        </Application.Resources>
  6. Add strings in AppResources.resx

    AppResources.resx Content

  7. Use localized strings anywhere in your Xaml

    CompanyName="{Binding Path=LocalizedResources.CompanyName, Source={StaticResource LocalizedStrings}}"
  8. To Add other languages and for more details follow How to build a localized app for Windows Phone

Don’t Develop Features, Build Designed Products

When faced with a hard deadline you are forced to make decisions fast. Below I am going to detail briefly the design decisions that took place to deliver PlaygroundCommunity for the hack::data competition.

As I stated in my last post, Developers’ Weak Spot, time and time again I fall for this trap. In PlaygroundCommunity I started all wrong. I thought, after my success of the Kindy Web Site, surely there must be a good platform for me to begin this site with. It could give me logins, forums, comments, Facebook integration etc. So began the search. I fired up the Microsoft Web Platform installer and had a look at the open source .NET based blog, forums, content management systems, to find something just right. What I found was that I had fast headed out in the wrong direction on a new project and completely wasted my time.

I did not design the product that I wanted. I had a feature ideas written down, but the problem is shopping list requirements are just plain wrong. They give the false impression that you have thought about your product when you haven’t. My motive for searching for base that had it all was that I did not want to waste anytime coding something that is already done. However, more time is wasted building the wrong thing. I started designing the product wrong. I went:

Idea > Feature Shopping List > Search for existing platform to save development time

After I waste my time, I started again. It still starts with the idea. From the idea you generally cannot help thinking of all the things the application could do. A feature shopping list. Only do this briefly. You don’t even need to write this down and can skip it completely. Coming up with features is usually easy. You need to put all that aside and think of the user experience (UX) and the work flow that is desirable for your users first. When that is taken into account, requirements that sounded good on their own, that don’t fit in the UX get dropped easily. The same goes for discovering features that fall out of the workflow that would not have been easily considered on their own. Always come from the user perspective when considering a new feature. It is like grocery shopping. You plan your meals for the week, check out the recipes and then build a shopping list based on what you need.

The solution for PlaygroundCommunity was to mock the interfaces to determine the functionality that is required. What I ended up with was 2 pages, with external services consumed with JavaScript. Nothing difficult that required a heavy, feature-filled platform to start from. Although this is very clear in hindsight, from my feature shopping list, it was not. Below are the mocks I did in PowerPoint Storyboarding, an extension for PowerPoint that comes with Visual Studio 2012.

PlaygroundCommunity Mock Page 1PlaygroundCommunity Mock Page 2

From here you can see how close the end product came. I had a few changes on the mocks after consulting a couple of people before I even started coding. It was easy to determine what could also be cut to be able to make the deadline. What makes it interesting is due to the clear scope I was then able to take a risk and use technologies I had no previous experience with, for my learning and improved user experience.

Technorati Tags: