Click here to Skip to main content
15,910,303 members
Please Sign up or sign in to vote.
1.50/5 (2 votes)
See more:
Hello guys:

I'm totally new to C# and OAuth and I'm looking for help on how to create a C# Console Application that can be used to Authenticate a user on my project, by using his/her social network credentials (Facebook will suffice, for now).

Now, I've been searching around the web for articles but everyone has a complicated approach to it (complicated for me since I'm new to this, obviously) like using OAuth 2.0 with ASP.Net MVC, etc. They look great in principle and practice, but since I'm new to C# and ASP.Net MVC, I'm going nuts trying to figure out everything inside the code O_o. Plus, I really don't have the need to ask the user for permissions since my app will not be doing things on their behalf (which complicates the flow even further).

I just need to add Social Network login functionality to my project to give my users the option to log in by using the Social Network account they choose (facebook for now), only for Authentication purposes. In order to get acquainted with this process in C# and OAuth, I'm thinking that a console application in C# might be a good way to get my hands dirty without going as crazy as with those other examples; and I really need your help!

Thanks for your help in advance!!
Posted
Comments
Afzaal Ahmad Zeeshan 8-Jul-15 14:47pm    
Did you try to look into Facebook documentation?
Bounze 8-Jul-15 14:55pm    
Yes, and here's what they say about showing code-specific examples:
"Because of the various combinations of code languages that could be used in web apps, our guide doesn't show specific examples. However most modern languages will be capable of URL parsing..."
Apparently they feel like they would be sending a wrong message if they post examples using one language in particular...or something like that. But thank you for your suggestion, although I've been all over that site looking for help already ;-)
j snooze 8-Jul-15 17:54pm    
I think you're going to have a tougher time trying to do this with a console app than you think. To use facebook authentication(which I have done on an mvc app) you have to register your app with facebook on developers.facebook.com and get your secret code. When someone logs in using facebook authentication, facebook needs to validate the app talking to it, and something to callback to with the security token it creates to let you know this person is OK. So you have to build something that can be called back to by facebook to even start something like this. In my case I can just give facebook my url and it calls back to that to let me know the user has been authenticated. If you were hoping for 3 lines of code and a button push, it is a bit more complicated than that. A simple walk through, step by step tutorial exists here(for mvc). I know its web, but that can at least get you started with some of the things you'll need.

http://www.asp.net/mvc/overview/security/create-an-aspnet-mvc-5-app-with-facebook-and-google-oauth2-and-openid-sign-on
Bounze 9-Jul-15 8:07am    
Hi, j snooze! And thank you so much for your comment! Actually, I do understand that using a console app will be more work than to write 3 lines of code ;) hahahaah I have been investigating the code in the default template when you create a ASP MVC new project in VS. I do understand that everything is practically built in there and works beautifully; you just have to de-comment the code for the provider you wish to use (google, facebook, etc.) The problem is that in my specific situation, doing this by browser is not an option. Therefore, I understand that I would have to build the flow manually, but since I'm new to C# and ASP.Net MVC, I've been trying to "decipher" all the code in the default template so that I can understand what goes on and then figure out how to build my solution in a console or windows application from scratch using the concepts and object in the template: LOTS OF WORK. What I'm really looking for is a sort of TUTORIAL that can explain, in plain and simple words (like for beginners), the scenario: which elements/objects are needed for the whole thing to work and how to use them in C#.

Thanks a BUNCH for your time and care in answering my problem! :-D

1 solution

Hi friends,

I'm using fb login oAuth using javascript. here we use the code and get data in json form.

JavaScript
<script>
 window.fbAsyncInit = function () {
              FB.init({
                  appId: 'XXXXXXXXXXXXXX',//get app id from fb developer app
                  status: true,
                  cookie: true,
                  xfbml: true,
                  oauth: true,
              });
          };
          (function (d) {
              var js, id = 'facebook-jssdk'; if (d.getElementById(id)) { return; }
              js = d.createElement('script'); js.id = id; js.async = true;
              js.src = "//connect.facebook.net/en_US/all.js";
              d.getElementsByTagName('head')[0].appendChild(js);
          }(document));

          //LOGIN FUNCTION 
          function fb_login() {

              FB.login(function (response) {
                  if (response.authResponse) {

                      console.log('Welcome!  Fetching your information.... ');
                      access_token = response.authResponse.accessToken; //get access token
                      user_id = response.authResponse.userID; //get FB UID

                      FB.api('/me', function (response) {
                          if (response.name) {
                              var userName = response.name;
                              var r = splitUsername(userName);
                              var SNfirstName = r.firstName;
                              var SNlastName = r.lastName;
                              var SNuserId = response.id;
                              var SNuserEmail = response.email;
//here you will get fb login user data shown above r.firstName 
                              var args = {
                                  "SNfirstName": SNfirstName, "SNuserId": SNuserId, "SNlastName": SNlastName, "SNuserEmail": SNuserEmail
                              }


                              $.ajax({
                                  type: "POST",
                                  url: "edRegisterPage.aspx/SetSession",
                                  data: JSON.stringify(args),
                                  contentType: "application/json; charset=utf-8",
                                  dataType: "json",
                                  success: function () {
                                      var url = "/edRegisterPage.aspx";
                                      window.location.href = url;
                                  },
                                  error: function () {
                                      alert("Fail");
                                  }
                              });
                          }
                      });
                  } else {
                      alert('Login Failed!');
                  }
              }, { scope: 'publish_stream,email' });
          }
</script>



use this code to where you want to redirect user after login,suppose we redirect user to register page.because user details fill automatically after login with facebook.
C#
[WebMethod(EnableSession = true)]
        public static void SetSession(string SNfirstName, string SNuserId, string SNlastName, string SNuserEmail)
        {
            HttpContext.Current.Session["SNfirstName"] = SNfirstName;
            HttpContext.Current.Session["SNuserId"] = SNuserId;
            HttpContext.Current.Session["SNlastName"] = SNlastName;
            HttpContext.Current.Session["SNuserEmail"] = SNuserEmail;
        }




If user success then redirect user to home screen else,throw massage unable to login.

Hope,this code will help you.
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900