Tuesday, December 25, 2012

MVC4-Integrating OAuth/OpenID

 MVC 4 has enabled login from facebook and other sites using OAuth and OpenID. MVC 4 uses DotNetOpenAuth library for OAuth and OpenID authentication. In this blog post, we would see how to configure MVC 4 for Facebook Login

1) Create MVC4 application

2) Go to Facebook below URL - https://developers.facebook.com/apps

3) Click on 'Create New App' button

4) Enter your application Name























5) There could be captcha image to validate yourself

6) Fill in the basic information in the next form that is shown. Display Name and Email Address would be prefilled.






















7) Click on ‘Website with Facebook login link’ and enter your site URL (like www.example.com).

However if your testing your application on your local machine enter the url like http://localhost:12345 (where 12345 would be your local port number)



















8) Click on ‘Save Changes’ button and you would see App ID and App Secret Key

9) Now its time to make changes in application
Open the AuthConfig.cs in App_Start folder. Here you will all the code commented for different clients like Facebook, Twitter,GooglClient etc

            OAuthWebSecurity.RegisterFacebookClient(
                appId: "",
                appSecret: "");


10) Uncomment code lines for Facebook. Add the App ID and App Secret Key.

11) Run the application and click on Log In button. There you would see additional button for Facebook Login














12) Clicking on 'Facebook' button will authenticate user with Facebook credentials.










13) Once you register, you can see that you are authenticates and you can see your name.











Hope this article of mine would give some overview of MVC OAuth.

SQL Query: Removing Zero's

Today, we would consider a scenario of removing ZERO's from left side present in the column for one SQL Table.

Consider we are having following data in the table:


Select * from
[dbo].[RemoveZero]

Result:


Column1
0001
000100
100100
000 0001
00.001
01.001




and following is the result we are looking for:


Column1
1
100
100100
1
.001
1.001

So, how to do this? 
We can achieve the above result by using REPLACE and TRIM functionality

Below is the query which would produce the above result:

Select replace(ltrim(replace(numcol,'0',' ')),' ' ,'0') from 
[dbo].[RemoveZero]

So ,if you see:
a) We are replacing '0' with space by using Replace function
b) Then we are using LTRIM function as it would only Left trim the spaces from Left side
c) Then we are again replacing the spaces with Zero's , so this would bring back the zero anywhere already present.

Hope you would have liked this scenario.