使用 facebook 进行 ASP.NET 网站身份验证 [英] ASP.NET website authentication with facebook

查看:38
本文介绍了使用 facebook 进行 ASP.NET 网站身份验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看过很多关于 ASP .NET 网站和 Facebook 之间集成的文档,但我还没有找到一个简单的工作示例,即使使用 Facebook C# SDK.

我想要的只是一个使用 Facebook 登录"的示例,并获取基本的用户信息(例如姓名、电子邮件和照片).

你们能帮我吗?

非常感谢!

解决方案

正如你所说的using Facebook C# SDK,那么这里是画布应用程序的路径和一些代码:

1- 从 Visual Studio 创建您的 Web 应用程序
2- 安装 nuget 并通过 nuget Facebook C# SDK
获取3- 从 https://developers.facebook.com/apps/ 创建和配置您的应用.
4- 您用于 facebook 集成的网络配置:

<预><代码><配置><configSections><section name="facebookSettings" type="Facebook.FacebookConfigurationSection"/></configSections><facebookSettings appId="123..." appSecret="abc...." siteUrl="http://apps.facebook.com/myapp/" canvasPage="http://app.facebook.com/myapp" secureCanvasUrl="https://myapp.com/" canvasUrl="http://myapp.com/" cancelUrlPath="http://www.facebook.com/"/>...

通过使用 sdk,你可以解析由 facebook js sdk 编写的签名请求或 cookie

FacebookWebContext fbWebContext = new FacebookWebContext();//检查用户是否已认证bool IsAuthenticated = fbWebContext.IsAuthenticated();

在这里您可以通过以下方式统计朋友数:

FacebookWebClient fbWebClient = new FacebookWebClient();动态结果 = fbWebClient.Get("me/friends");var 朋友 = 结果["数据"];int frienCount =friends.Count;

对于客户端:

<div id="fb-root"></div><脚本>window.fbAsyncInit = 函数(){FB.init({appId: '123 ...',状态:真实,饼干:是的,xfbml: 真的,oauth:true });};(功能 () {var e = document.createElement('script');e.async = true;e.src = document.location.protocol + '//connect.facebook.net/en_US/all.js';document.getElementById('fb-root').appendChild(e);} ());<!-- 其余的 html -->

用于登录和请求 javascript

的许可

 FB.getLoginStatus(function(response) {控制台日志(响应);if ((response.status)&&(response.status=='connected')) {//成功} 别的 {//用户拒绝}, {scope:'user_likes, offline_access'}});

我更喜欢在我的项目中客户端登录,因此尚未注册的用户有登录页面,例如,如果提交表单,那么我调用上面的代码块.

注意:您必须为 Internet Explorer 设置 P3P 标头以根据您的服务器读取/写入 cookie.对于 IIS,global.asax:

protected void Application_BeginRequest(Object sender, EventArgs e){HttpContext.Current.Response.AddHeader("p3p", "CP="CAO PSA OUR"");}

Volià

I've seen a lot of documentation about integration between ASP .NET web sites and Facebook, but I haven't found a simple working example, even using Facebook C# SDK.

All I want is a "login with Facebook" example, and to get basic user information (such as name, email and photo).

Can you guys help me please?

Thanks a lot!

解决方案

as you say using Facebook C# SDK, then here is path and some code for canvas application:

1- Create your web application from visual studio
2- install nuget and get by nuget Facebook C# SDK
3- from https://developers.facebook.com/apps/ create and configure your app.
4- Your web config for facebook integration :

<configuration>
  <configSections>
    <section name="facebookSettings" type="Facebook.FacebookConfigurationSection" />
  </configSections>
  <facebookSettings appId="123..." appSecret="abc...." siteUrl="http://apps.facebook.com/myapp/" canvasPage="http://app.facebook.com/myapp" secureCanvasUrl="https://myapp.com/" canvasUrl="http://myapp.com/" cancelUrlPath="http://www.facebook.com/" />
...

By using sdk, you can parse signed request or cookie written by facebook js sdk

FacebookWebContext fbWebContext = new FacebookWebContext();
//Check if user auhtenticated
bool IsAuthenticated = fbWebContext.IsAuthenticated(); 

Here you can have friend count by:

FacebookWebClient fbWebClient = new FacebookWebClient();
dynamic result = fbWebClient.Get("me/friends");
var friends = result["data"];
int frienCount = friends.Count;

For the client side:

<body> 
<div id="fb-root"></div>
<script>

window.fbAsyncInit = function () {
    FB.init({ 
    appId: '123...', 
    status: true, 
    cookie: true, 
    xfbml: true, 
    oauth:true  });
};
(function () {
    var e = document.createElement('script'); e.async = true;
    e.src = document.location.protocol + '//connect.facebook.net/en_US/all.js';
    document.getElementById('fb-root').appendChild(e);            
} ());

<!-- rest of your html -->

</body>

For login & asking permission from javascript

    FB.getLoginStatus(function(response) {
        console.log( response );
        if ((response.status)&&(response.status=='connected')) {
            //successs
        } else {
            //user declined 
           }, {scope:'user_likes, offline_access'}

    });

I prefer in my project to client side login thus not yet registered user have landing page, if for example submit form then I call code block above.

Note: you have to set P3P header for Internet explorer to read/write cookie depending of your server. for IIS, global.asax:

protected void Application_BeginRequest(Object sender, EventArgs e)
{

    HttpContext.Current.Response.AddHeader("p3p", "CP="CAO PSA OUR"");

}

Volià

这篇关于使用 facebook 进行 ASP.NET 网站身份验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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