Skip to main content

Posts

Showing posts from April, 2015

Google+ Signin for ASP.NET MVC 4

With ASP.NET MVC 4 Microsoft has added the ability to allow your users to log in to your application using various OAuth and OpenID providers.  Microsoft has supplied clients for Facebook, Twitter, Google, Microsoft, LinkedIn and Yahoo.  The Google client is based on OpenID and not OAuth.  With the recent announcement of   Google+ Signin   I set out to create a client which uses the Google+ login via OAuth instead of the standard OpenID login. Google has supplied some   great documentation   on how to achieve this, but what I wanted to do was to create something which integrated nicely with the existing OAuth infrastructure which Microsoft supplied in MVC 4. Overview of the Google OAuth Client The Google OAuth signin process is   described in detail   in the Google Developers documentation.  This is pretty much the standard process for any OAuth 2.0 provider. There is one caveat however, and that is that Google expects the redirect URL to exactly match the one you define whe

Convert a Unix timestamp to a .NET DateTime

The POSIX time, or  Unix time , is the number of seconds elapsed from the midnight of January 1st 1970 in UTC coordinates. This timestamp is used in all *nix languages; probably you will never need to use a Unix timestamp in .NET, but if you have to interact with other application or sites, maybe built in PHP or Java, you will probably have to deal with it. But .NET doesn't have a method to convert a Unix timestamp to a DateTime and viceversa (or I didn't find it), so I had to implement it. static DateTime ConvertFromUnixTimestamp( double timestamp) { DateTime origin = new DateTime(1970, 1, 1, 0, 0, 0, 0); return origin.AddSeconds(timestamp); } static double ConvertToUnixTimestamp(DateTime date) { DateTime origin = new DateTime(1970, 1, 1, 0, 0, 0, 0); TimeSpan diff = date - origin; return Math.Floor(diff.TotalSeconds); } The first method converts a Unix timestamp to its DateTime equivalent, and the second takes a DateTime and convert

Check if Popup Window Closed

Sometimes you will want to use a popup window for an antiquated system and need to maintain a reference to the popup window so that when the popup loses focus you can bring it back in focus with a click of a button. The following script will allow you to do that. In brief, the script opens a window when a button is clicked, if it loses focus you just need to click the button again to get it back. The script also deals with the fact that the user may close their browser window, navigate away from the main page or refresh the current browser window. Enjoy! <html> <head> <title></title> <script type="text/javascript" src=" http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js "> </script> </head> <body> <button id="button">Click Me!</button> <script type="text/javascript"> var win; $(window).load(function() { $('#button').click(function(){ openPo