Friday, August 18, 2017

Don't Let Microsoft SQL Round Your DateTime Field's Milliseconds

I needed to export some data out of Application Insights and save it locally.  While doing a sanity check, I realized that some of my datetime values were not the same.

Therefore, I created a simple test.
  1. Create a table with just a datetime field.
  2. Insert the value I expected.
  3. Select that data back out and witness the rounded value.



I finally found an answer here:
https://stackoverflow.com/questions/36414315/inserting-datetime-with-milliseconds-into-sql-server-table-issue

MS SQL rounds the DateTime field in increments of .000, .003, and .007.  I would assume that means it is saving on memory space behind the scenes by only offering 3 options for the single digit of milliseconds?

Whatever the case, if precision is necessary (as it was for the Application Insights data I was getting),  use the newer datetime2(3) field definition.

Friday, January 6, 2017

Why do I have a binding for SitecoreApplicationCenter in my web.config?

I was doing some cleanup in our config files, and I noticed we have a binding called "SitecoreApplicationCenter".  Looking through our Sitecore solution, I didn't see this referred to anywhere, until I opened the Sitecore.Kernel.dll.

This reference is used when communicating with Sitecore's App Center... just like it's named.  It will be used when making calls to the service: https://apps.sitecore.net/appsservice.asmx

We'll never use this on our frontend servers, so I transformed it out of that config.  But I kept it in our Content Manager server config.

Friday, December 2, 2016

[HttpException (0x80004005): Unable to validate data.] when changing FormsAuthentication machineKey encryption

A new feature on our website required us to use a different encryption algorithm for our Forms Authentication cookie.

Simple change; just update one line in the web.config.  However we read data from that cookie on every web request.  And so as soon as we changed that algorithm, anyone who had previously visited our site would get an ASP.NET yellow screen of death.

"Unable to validate data." ASP.NET error message screen


No problem, I thought.  I can just delete that cookie instead of reading it.  That turns out not to be so easy to do.  The best I could do was set an expiration on the cookie in the client response.  And then I also deleted the cookie in the server request.

// Remove the cookie from the server request
contextBase.Request.Cookies.Remove(FormsAuthentication.FormsCookieName);
                            
// Expire the cookie on the client
authCookie.Expires = DateTime.Now.AddDays(-1d);
Response.Cookies.Remove(authCookie.Name);
Response.Cookies.Add(authCookie);

This got me past some errors, but somewhere deep in someone else's DLL the problem still lingered.  I tried a different approach.  I resorted to using a base Forms Authentication method, SignOut().  That worked perfect!  Here is the code I ended up with.  It was placed in the Global.asax Application_PostAuthenticateRequest() event.

protected void Application_PostAuthenticateRequest(object sender, EventArgs e)
{
    var contextBase = ServiceLocator.Current.GetInstance();
    var authCookie = contextBase.Request.Cookies[FormsAuthentication.FormsCookieName];
    if (authCookie != null)
    {
        var encodedTicket = authCookie.Value;
        if (!String.IsNullOrEmpty(encodedTicket))
        {
            FormsAuthenticationTicket ticket = null;
            try
            {
                ticket = FormsAuthentication.Decrypt(encodedTicket);
            }
            catch(HttpException hex) 
            {
                if(hex.Message == "Unable to validate data.")
                {
                    // Clean up any AUTH cookies with 
                    // a different encryption
                    FormsAuthentication.SignOut();
                    return;
                }
            }
            // Unimportant company stuff happens here
        }
    }
}

Tuesday, September 27, 2016

Google's Email Validator Regular Expression

I am anticipating the release of the Google Home device.  And while I was checking out the page's source code, I saw some validation code they use to check email addresses.

[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,10}$

It has always been difficult to find a short enough and smart enough regex for validating emails.  I'll follow Google's lead and use this next time I need a quick check for a valid email.

Monday, August 29, 2016

Alexa Zipcode Lookup


I have been playing around with building another Alexa skill.  I mostly wanted to explore some different techniques to use in a skill.

This new skill will lookup zipcodes for you.  It does it in a few different ways:
  • If you tell it a zipcode, it will tell you what city that zipcode is for
  • If you tell it a city and state, it will tell you what zipcodes are associated with that city and state
  • It will return a card to your Alexa app with a map of the location
The skill uses a few different services.  Zippopotam.us is the service that I use to do zipcode and reverse zipcode lookups.  And I make use of Google Maps API to assist with the spelling of city names, when the Alexa Voice Service doesn't recognize the spelling.  Also, the maps are provided by Google Maps API.

I am going to write about the different parts of this skill across a few different posts.  I hope some of these techniques will help you with your next skill.

The code for this skill is up on Github...  https://github.com/chrispauly/alexa-zipcode-lookup 

Friday, August 26, 2016

NPM to the Rescue for your Alexa Skill



I started writing Alexa Skills in AWS Lambda using the example blueprint and online code editor.

Recently, using the new code samples that Amazon has on Github, I've graduated to writing my code locally in VS Code, and then zipping up the files and uploading them into my Lambda function.

Now, I'm taking it one step further, and instead of writing all the code, I am leveraging NPM to provide functions for tasks that would be tedious and/or difficult to write myself.

I wasn't sure how difficult it would be to start using NPM, like what config settings I would need or how to use them in my skills.  I found out it is as easy as 1-2-3.

  1. Download NodeJS and install it on your OS.
  2. Open a Command Prompt and go into the ./src folder of your skill (the folder where index.js lives).  Run the command npm install [package name]
  3. Refer to the examples provided for your package and incorporate that in your code.

That's it!

My main concerns were having to worry about directory paths or unfamiliar javascript configuration files.  But I had none of those issues.  I just zipped up my code and uploaded it into my Lambda function and everything worked!

I stumbled upon how easy this process was when I needed to write a function to translate digits into words.  When I wanted to ask for a city name in my service, I found that certain city names did not come in as expected.  For example, my hometown, Two Rivers, WI, when the CityName slot was populated by AVS with my spoken words, it came into the skill service as "2 Rivers".  I needed to use the actual city name in my skill service.  It required the actual spelling of the city name, so I needed to translate the number 2 into the word Two.

I found that there are other cities with numbers in their name, so I decided that I should build a more generic function that could translate numbers into words.  But before I wrote that function, I Google'd for javascript functions that do that.  NPM had a number of packages that would work for me, but I settled on number-to-words.  I followed the 1-2-3 steps above and was so happy it worked without any extra effort.

Monday, August 15, 2016

Alexa Meet LUIS


LUIS (Language Understanding Intelligent Service) is a research project that uses a Bing Cognitive AI service to match spoken dialog with an intent.  When creating a LUIS app, you'll create an Intent Schema and Sample Utterances similar to that in an Alexa skill.

Once you have a LUIS application built, you need to train it to create its internal models that it uses to predict which intent best matches the phrase sent to it.  It responds with a JSON object that includes your intent names and the scores that represent how well that phrase matched an existing intent.



I wanted to leverage the power of LUIS to determine what intent has the highest probability to match a spoken phrase, when I didn't include that phrase as a sample utterance in my Alexa skill.

This required me to create a catchall intent.  The schema for this intent is exactly like I described in a previous blog article about matching all the spoken words...  Polly Want a Cracker? A Simple Alexa Skill that Echoes Your Words

Once I created a skill that had a number of intents, along with my catchall intent, I began testing phrases that were not included in my sample utterances.  I soon realized that the AVS already performs LUIS-like language understanding.  It was extremely difficult to ever have the Alexa skill choose my catchall intent.

The one way I could see using LUIS now, would be to build the Polly Want a Cracker skill.  Then use LUIS to define my intents and slots (which are called entities in LUIS).  LUIS has a great service that provides active learning in your app.  LUIS stores the new phrases it hears and lets you review them and label new entities.  This is not available in Alexa right now, so a skill developer has a difficult time understanding what phrases people are trying in their skill that may not be working.