如何在C#中使用他/她的FB帐户验证用户? [英] How do I Authenticate a user using his/her FB account in C#?

查看:70
本文介绍了如何在C#中使用他/她的FB帐户验证用户?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好:



我是C#和OAuth的新手,我正在寻找有关如何创建 C#控制台应用程序的帮助可以通过使用他/她的社交网络凭据(Facebook就足够了)来用于 验证 我项目中的用户。



现在,我一直在网上搜索文章,但每个人都有一个复杂的方法(对我来说很复杂,因为我很新,显然)就像使用OAuth 2.0使用ASP.Net MVC等。它们在原理和实践上看起来很棒,但由于我是C#和ASP.Net MVC的新手,所以我很想弄清楚代码O_o中的所有内容。另外,我真的没有必要要求用户提供权限,因为我的应用程序不代表他们做事情(这使得流程更加复杂)。



我只需要在我的项目中添加社交网络登录功能,以便我的用户可以选择使用他们选择的社交网络帐户登录(目前为Facebook),仅用于身份验证。为了熟悉C#和OAuth中的这个过程,我认为C#中的控制台应用程序可能是一个很好的方式来弄脏我的手,而不会像其他示例那样疯狂;我真的需要你的帮助!



感谢您的帮助!!

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!!

推荐答案

嗨朋友们,



我正在使用fb登录oAuth使用javascript。这里我们使用代码并以json格式获取数据。



Hi friends,

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

<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: < span class =code-string> POST,
url: edRegisterPage.aspx / SetSession
data: JSON .stringify(args),
contentType: 应用/ JSON; charset = utf-8
dataType: json
成功: function (){
var url = / edRegisterPage.aspx;
window 。< span class =code-sdkkeyword> location .href = url;
},
错误: function (){
alert( 失败);
}
});
}
});
} else {
alert(' 登录失败!'< /跨度>);
}
},{scope:' publish_stream,email'});
}
< / script>
.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>







使用此代码到哪里你想在登录后重定向用户,假设我们将用户重定向到注册页面。因为用户登录facebook后会自动填写用户信息。




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.

[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.


这篇关于如何在C#中使用他/她的FB帐户验证用户?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆