Я новичок в приложениях Facebook и недавно прочитал доступный документ. Затем я начал получать CK SDK от NuGet и прочитал get started tutorial.Простой вход с Facebook C# SDK не работает
Я немного изменил его. У меня есть 3 страницы:
- Default.aspx: есть кнопка «войти с Facebook»
- Protected.aspx: страницы, что я в конечном счете хочу быть недоступны без первого входа в систему
DoSignIn. aspx: содержит запрос на токен и перенаправляет обратно в Default.aspx
На Default.aspx у меня есть кнопка, которая перенаправляет меня на страницу Protected.aspx. На нем у меня есть ярлык, показывающий мне токен.
Теперь, мой вопрос заключается в следующем:.
- При нажатии на кнопку «войти с Facebook», кнопка не изменится, чтобы показать, что я вошел в это только показывает, что я вошел в систему, если я обновляю страницу.
- Если я нажму кнопку, чтобы перенаправить меня на страницу Protected.aspx, я не вижу токен в ярлыке, потому что страница DoSignIn.aspx никогда не вызывалась.
Если кто-то может указать мне на то, что я делаю неправильно (поскольку я почти скопировал пример учебника), я был бы очень благодарен. Я провел последние 3 часа, играя с этим, и искал Google и все вокруг и не знаю, где искать больше.
Спасибо за ваше время.
PS. У меня есть последняя Facebook C# SDK и App ID, кажется, работает хорошо, как когда я вручную обновить свой браузер, я действительно вошел в
добавить код для 3-х страниц:.
Default.aspx :
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<div id="fb-root">
</div>
<script type="text/javascript">
window.fbAsyncInit = function() {
FB.init({
appId: 'I_INSERTED_MY_APP_ID_HERE', // App ID
status: true, // check login status
cookie: true, // enable cookies to allow the server to access the session
xfbml: true // parse XFBML
});
FB.Event.subscribe('auth.authResponseChange', function (response) {
if (response.status === 'connected') {
// the user is logged in and has authenticated your
// app, and response.authResponse supplies
// the user's ID, a valid access token, a signed
// request, and the time the access token
// and signed request each expire
var uid = response.authResponse.userID;
var accessToken = response.authResponse.accessToken;
// - Handle the access token -
// Do a post to the server to finish the logon
// This is a form post since we don't want to use AJAX
var form = document.createElement("form");
form.setAttribute("method", 'post');
form.setAttribute("action", 'DoSignIn.aspx');
var field = document.createElement("input");
field.setAttribute("type", "hidden");
field.setAttribute("name", 'accessToken');
field.setAttribute("value", accessToken);
form.appendChild(field);
document.body.appendChild(form);
form.submit();
} else if (response.status === 'not_authorized') {
// the user is logged in to Facebook,
// but has not authenticated your app
} else {
// the user isn't logged in to Facebook.
}
});
};
// Load the SDK Asynchronously
(function (d) {
var js, id = 'facebook-jssdk', ref = d.getElementsByTagName('script')[0];
if (d.getElementById(id)) { return; }
js = d.createElement('script'); js.id = id; js.async = true;
js.src = "//connect.facebook.net/en_US/all.js";
ref.parentNode.insertBefore(js, ref);
} (document));
</script>
<form id="form1" runat="server">
<div>
<div class="fb-login-button" data-show-faces="true" data-width="400" data-max-rows="1">
Log in with Facebook</div>
<br />
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Go to protected" />
</div>
</form>
</body>
</html>
Default.aspx.cs:
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
Response.Redirect("Protected.aspx");
}
}
DoSignIn.aspx.cs:
public partial class DoSignIn : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
var accessToken = Request["accessToken"];
Session["AccessToken"] = accessToken;
Response.Redirect("Default.aspx");
}
}
Protected.aspx.cs:
public partial class Protected : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (Session["AccessToken"] != null)
{
Label1.Text = Session["AccessToken"].ToString();
}
else
{
Label1.Text = "Not authenticated";
}
}
Вы получаете ошибки Javascript при загрузке страницы по умолчанию? – shashi
Нет, нет ошибки javascript. – ilovebigmacs