Monday 29 December 2014

Detect Shake in Phone using Jquery

In this post I want to explain you how to implement phone shake detection using jquery. Using this I had implemented an interesting concept that shake the mobile device and get the product discount. Very easy to use add this to your e-commerce project, sure this user experience feature will attract the people for more sales. Please try these live demos with your mobile device, this works with mobile web browser device accelerometer.

Detect Shake in Phone using Jquery

Read more »
Share:

Monday 22 December 2014

Sharing the Cookies in Web Farm OR across different servers

Consider you have an application which provides you an authentication cookie using ASP.NET Membership provider, and you are using this authentication cookie across multiple servers to access the secured contents. I have depicted this scenario using the diagram below.

image

Now to read the client cookies across the applications you can simple use the following line of line of code fetch the cookies

HttpContext.Current.Request.Cookies[FormsAuthentication.FormsCookieName].Value;

But wait a minute, is it that simple? In fact yes except the fact that to secure your session and prevent from any men in the middle attacks your cookies are encrypted using the machine key of Authentication Server. Which might look similar to the one below. This is configured at your machine level, which means you may not usually find this key in your local web.config files.

<machineKey
validationKey="21F090935F6E49C2C797F69BBAAD8402ABD2EE0B667A8B44EA7DD4374267A75D7AD972A119482D15A4127461DB1DC347C1A63AE5F1CCFAACFF1B72A7F0A281B"
decryptionKey
="ABAA84D7EC4BB56D75D217CECFFB9628809BDB8BF91CFCD64568A145BE59719F"
validation
="SHA1" decryption="AES"/>


And also you might be aware that every machine has its own machine.config file which is tailored to that particular machine, so if the cookie is encrypted using the machine key of Server1 then it cannot be decrypted using machine key of Server2 or any other Server.



So even if you managed to get the cookie, but when you try to read the data from the cookie you must have to first decrypt the cookie in order to read any key from the cookie. I have provided below a sample code which exactly does the same.



private void SetFormsAuthenticationTicket()
{
FormsAuthenticationTicket ticket
= default(FormsAuthenticationTicket);
if (System.Web.HttpContext.Current.Request.Cookies.Get(System.Web.Security.FormsAuthentication.FormsCookieName) != null)
{
ticket
= System.Web.Security.FormsAuthentication.Decrypt(
System.Web.HttpContext.Current.Request.Cookies.Get(
System.Web.Security.FormsAuthentication.FormsCookieName).Value);
string[] roles = ticket.UserData.Split(new char[] { '|' });
GenericIdentity userIdentity
= new GenericIdentity(ticket.Name);
GenericPrincipal userPrincipal
= new GenericPrincipal(userIdentity, roles);
System.Web.HttpContext.Current.User
= userPrincipal;
}
}


To get a better view I have also provided the screenshot of the code below.



image



This does solve your problem, but the when you try run the code you might get the following exception.




System.Web.HttpException : Unable to validate data. at System.Web.Configuration.MachineKeySection.EncryptOrDecryptData(Boolean fEncrypt, Byte[] buf, Byte[] modifier, Int32 start, Int32 length, IVType ivType, Boolean useValidationSymAlgo, Boolean signData)




image



This happens because as I mentioned above, your cookie was created by machine key of Server1, but some of the part of your application is served by Server2 which tries to decrypt the cookie using the code above. So to mitigate this issue first this you might need to do is to generate the machineKey which can be shared across all your applications who is sharing the cookies and located across your network. I have written a separate post on How to generate  the machineKey using IIS 7.0+ you can visit the link: http://tutorials.indianjobs.co.in/2014/12/generate-machinekey-in-iis-70.html



Secondly you have to place the same decryptionKey in all your application local web.config, and you are done.



You might encounter this types of scenario is small scale applications, but for most of the complicated application these days where applications are placed on completely different domains, you will need to implement your own SSO architecture. Details are out of then scope of this article, so you might take a look for details in some other article such as Single Sign On (SSO) for cross-domain ASP.NET applications or Single Sign-On (SSO) for .NET or Using a third party identity provider like Facebook, Google, etc




References:



http://msdn.microsoft.com/en-us/library/ff649308.aspx



http://www.codeproject.com/Articles/288631/Secure-ASP-NET-MVC-applications

Share:

Wednesday 17 December 2014

Generate MachineKey in IIS 7.0+

Normally machineKey is already configured in your machine.config file which is applied to all your application if you are working in the same Web Farm. But most of the time this may not be the scenario, it may be possible that your applications are distributed across different Web Farms.

To deal with this all you need is to configure the same decryption keys in web.config of all the applications to decrypt the authentication cookie created at client side.

To generate a new machine key all you need is to first open the IIS Admin Console, and

1. Select the Server

2. Select the Machine Key from right hand side of the console

image

3. Double Click to open the Machine Key and then Select ‘Generate Keys’ from Actions

image

4. You got the Decryption Key and Unique Key

You can use this Keys in you web.config file which might look similar to something like

image

Share:

Monday 15 December 2014

UnSubscription Email Utility

In this post I am going to show a very simple yet very useful utility which everyone of us encounter when we get lots of unwanted mails which we had subscribed sometime in our web exploration but later we want to get rid of those subscribed mails. Moreover this is a very integral part of our business use cases.

I have used ASP.NET MVC as an example but if you understand the concept, this can be achieved using any of your favorite programming logic as well.

I tried to keep the example as simple as possible so that I can cover the concept/business logic in more details. In this example as soon as I run the application I will call the Index action method with my hardcoded email id. In real world you might want to make it more dynamic and will try to pass this as a parameter.

image

Now I have written a utility  method which is used to generate the subscription link, which can be embedded in the marketing emails, so when the user click on this link Say Unsubscribe Me, he will land on the page where just in a click of button his mail id will be removed from the marketing email database.

image

For the simplicity sake of this example I have not used the real time database operations but for your implementation you will definitely need DB operation to update the user preferences.

Not let me highlight the most important logic of this GetUnSubscriptionLink method; since we are dealing with one key information of a user which is his email id, so I have used CryptoHelper utility which is used to Encrypt the email id of the consumer. This will also help us to filter any Brute force attack where the hacker will try to enter random emails on his list to invoke the unsubscribe methods.

You can download your copy of CryptoHelper utility from here: http://1drv.ms/1r2kisl

One of the example of the unsubscription link is shown in the screen below. For better programming practice you can configure the root URL in your config file.

image

Additionally you can do a second level of validation where you can prompt the end user to provide his email id in a text box and validate this against the decrypted string containing the email id from the query string, if both has a match then unsubscribe otherwise ignore the unsubscribe request.

In my example as soon as you pass all the validation I am calling the Unsubscribe action method of my controller which takes the encryptedText as a parameter and decode the text to unsubscribe the user from marketing emails.

image

Sample screen once the user is unsubscribed

image

I am providing below a utility Property which I found somewhere on internet, this might be very useful to you if you are playing with URL’s

image

Crypto Helper Utility: http://1drv.ms/1r2kisl

You can download the full working copy of this example from this link: http://1drv.ms/1vU5ZCO

Hope this helps, let me know in comments if you have any trouble in downloading the files or understanding my thoughts Smile

Share:

Planning Angular 1.4

We had a planning meeting last week to decide what will go into AngularJS 1.4. Here is a summary of what happened. You can watch the video of the meeting on YouTube below.

Release Schedule

We decided that the first release (1.4.0) will be in Spring 2015, coinciding with ng-conf on 5th March 2015. In the meantime, we'll continue with 1.3.x releases as usual.

Work Planning

Lucas prepared a spreadsheet with a list of suggested items for 1.4 from the issues and PRs on GitHub with the most community interest. This focussed on large work items or things that would require breaking changes or considerable new API. The bulk of the meeting was going through these items, deciding whether they should appear in Angular 1.x and, if so, which release and who would own the item.

1.4 Targets

There is a tracking spreadsheet for the items that are scheduled for 1.4.

The main themes for 1.4 are as follows:
  • Router - Brian - a new router for Angular 1 and 2 - Progress
  • I18N - Chirayu - provide a first class internationalization story for Angular - Design Doc
  • Forms - Martin - a fresh look at parsing/formatting/validation to simplify usage and maintenance (while fixing numerous outstanding issues) - Design Doc
  • HTTP - Pawel - improvements to the $http service, such as serialization, JSON parsing, testing mock DSL
  • Parser - Lucas - performance improvements to $parse service
  • Documentation - Caitlin - redesign the look of the docs app to use Material Design

In addition, we're planning to include the following notable or breaking changes:
  • $injector - Brian - throw an error if you redefine a module, to help identify bugs faster (#1779)
  • $compile - Igor - provide extra new module.component helper for defining component type directives more easily (#10007).
  • $compile - Caitlin - throw an error if non-optional isolated scope mappings are missing their attributes (#9216).
  • Project layout/Modularity - Pete - further partition angular.js into smaller optional modules/files to reduce the non-optional core file size (useful for mobile use cases).

Github Milestones and Labels

Finally we will start developing 1.4.x on the master branch soon. To support this here are the new labels/milestones for on-going development:

Milestones:

1.4.x - use this for issues and pull requests that are accepted and scheduled to be in 1.4

Labels:

branch: 1.2.x (replaces stable: yes)
branch: 1.3.x (replaces stable: no)
branch: 1.4.x (replaces 1.4 - for triaging 1.4.x issues and PRs)
Primary Focus: (new for items that we are focussing on for 1.4 - i.e. the stuff in the tracking spreadsheet)

Other Versions and Backporting

The master branch (i.e. 1.4.x) will now receive the majority of the focus.
The 1.3.x branch will receive version specific fixes backported from master.
The 1.2.x branch will now only receive fixes for security issues and major regressions.

Video

In our continued efforts to be transparent and open about our design and development activities, we published a recording of our planning meeting: https://www.youtube.com/watch?v=Uae9_8aFo-o

Screen Shot 2014-12-14 at 22.14.10.png

Just the Beginning

The planning we did for 1.4 is just the beginning. In addition to the outline above, we welcome additional suggestions for 1.4 via GitHub. Once we've released 1.4.0, we'll continue with 1.4.x releases that include non-breaking fixes that didn't make it into 1.4.0.

Share:

Thursday 11 December 2014

Hello World !!! using Apache Cordova and Visual Studio 2013

You heard it right, this is for all the .NET Developers (other platform also welcome) who have ever wished to build an app using Apache Cordova that targets multiple mobile platforms: Android, iOS, Windows, and Windows Phone.

Disclaimer: All images are copyright to their respective owners.

I am going to show you a very simple mobile application which displays Hello World, using apache Cordova and Visual Studio 2013.

1. Setup the environment by installing the Preview of Visual Studio Tooling Support for Apache Cordova, you can download the CTP from here.

Visual Studio Tools for Apache Cordova

image

2. Once the installation is complete, run Visual Studio 2013 and create a new project. You need to go to Javascript templates or you can select the Multi device Hybrid App and click OK button. this will give you default project structure

image

image

3. For this post I am not going to explain all the files, rather I will go directly to my index.html and change the default text Hello World !!! and we are done.

image

4. And finally select the desired Emulators and run the program.

image

And here is the output, that’s it and you are done.

image

You can double click on config.xml file to customize the Custom and Core properties of your application individually for each platforms.

image

Hope this makes your life simple Smile Enjoy.

Other helpful link:

http://msopentech.com/blog/2014/05/12/apache-cordova-integrated-visual-studio/

http://msdn.microsoft.com/en-us/library/dn757054.aspx

Share:

Monday 8 December 2014

Create a RESTful services using Slim PHP Framework

Two years back we had published an article called create a RESTful Services using PHP, in that most the PHP methods got depreciated. I have been looking for a simple RESTful api framework in PHP, I found few lightweight frameworks called Slim and Epiphany. In this tutorial I had implement a sample user updates RESTful web services project using Slim framework in PHP, it is very simple to implement and only focused on RESTful.

Create a RESTful services using Slim PHP Framework

Read more »
Share:

Wednesday 3 December 2014

Google New reCaptcha using PHP - Are you a Robot?

Today Google has been introduced new reCaptcha API called Are you a robot? “No Captcha reCaptcha” a complete new design captcha system. This protects your website for spammers and robots, in this post I had implemented new reCaptch API system with HTML login form using PHP. I like the new design it is clean and impressive, hope you will like it. Please take a look quick look at the demo.

Google New reCaptcha using PHP - Are you a Robot?

Read more »
Share: