Friday, April 11, 2014

ASP.NET tips: ASP.NET MVC4 OAuth Facebook Login

Today, I tried to add Facebook OAuth login feature into my ASP.NET MVC 4 application. Here is the step to add that feature: 
1. Open AuthConfig.cs that you can find in App_Start folder in your project.

2. Un-comment the Facebook client register code and insert yout application id and application secret as bellow:
   OAuthWebSecurity.RegisterFacebookClient( appId: "32499828342XXXX", appSecret: "3242kjsfudfsoirewXXXX");

3. And then run your application and go to http://yourmachine/Account/Login, you will find a button for login using your Facebook account. Try to press that button, if it is successful then you have configure your Facebook app correctly.


Common Error:

When you press Facebook button It will redirect and give this kind of  error message:
 -----------------------------------------------------------------
Given URL is not allowed by the Application configuration.: One or more of the given URLs is not allowed by the App's settings. It must match the Website URL or Canvas URL, or the domain must be a subdomain of one of the App's domains
-----------------------------------------------------------------

To resolve this problem you need to open you developer.facebook.com account and open your application go to Settings > Advanced, search for Valid OAuth redirect URIs field and insert http://yourmachine/ExternalLoginCallback, since you are using ASP.NET MVC4, or change to other callback url that you need.

That's it for today.. :D
Thank you.

No comments:

Post a Comment

Finally, C# 9 record, the equivalent of Scala's case class

While C# is a wonderful programming language, there is something that I would like to see to make our life programmer easier. If you are fam...