FacebookWebContext.Current.IsAuthenticated()总是返回false [英] FacebookWebContext.Current.IsAuthenticated() always returns false

查看:116
本文介绍了FacebookWebContext.Current.IsAuthenticated()总是返回false的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道有没有人可以指出我正确的方向。



我正在使用Facebook c#sdk来验证用户。我已经在我的global.asax中实现了IF SustApplication,所以稍后我可以通过编程方式为不同的Facebook应用程序提供AppId / AppSecret。



这是我当前的设置<我有这个类:

  public class RequestScopedFacebookApplication: b {
public RequestScopedFacebookApplication()
{

}

public IF monitoredApplication当前
{
get
{
var url = HttpContext.Current.Request.Url;
var app = new DefaultFacebookApplication();
app.AppId =xxx;
app.AppSecret =xxx;
return app;
}
}

public string AppId
{
get {return Current.AppId; }
}


public string AppSecret
{
get {return Current.AppSecret; }
}

public string CancelUrlPath
{
get {return Current.CancelUrlPath;
}

public string CanvasPage
{
get {return Current.CanvasPage;
}

public string CanvasUrl
{
get {throw new NotImplementedException();
}

public string SecureCanvasUrl
{
get {throw new NotImplementedException(); }
}

public string SiteUrl
{
get {throw new NotImplementedException(); }
}

public bool UseFacebookBeta
{
get {return Current.UseFacebookBeta; }
}

}



在我的global.asax中:

  void Application_Start(object sender,EventArgs e)
{
/ /应用程序启动时运行的代码
Facebook.FacebookApplication.SetApplication(新的RequestScopedFacebookApplication());
}

一旦我有我的重定向网址,我将用户发送到授权页面在回调页面上,我有这样的东西:

  if(Request.QueryString [code]!= null) 
{

FacebookOAuthResult结果;
if(FacebookOAuthResult.TryParse(Request.Url,out result))
{
if(result.IsSuccess)
{
var code = result.Code;

var oauthClient = new FacebookOAuthClient(FacebookWebContext.Current.Settings);
oauthClient.RedirectUri = new Uri(...);

动态参数= new ExpandoObject();
parameters.redirect_uri =...;
动态tokenResult = oauthClient.ExchangeCodeForAccessToken(代码,参数);

var accessToken = tokenResult.access_token;
var bum = new FacebookClient(accessToken);


}
else
{
var errorDescription = result.ErrorDescription;
var errorReason = result.ErrorReason;
}
}

}

所以现在我有令牌。如果我这样做:

  bum.get(/ me / feed)

这工作正常,我收到一些我的数据。



我遇到的问题是当我尝试从FacebookWebContext.Current.IsAuthenticated()或FacebookWebContext.Current.UserId访问某些内容时,它们总是为false,分别为0 。一旦用户通过身份验证,并且我有access_token,上下文看起来不会被更新。



我需要检查用户是否成功验证并获取他们的UserId和电子邮件在以后的日期。



如果任何帮助将不胜感激!

解决方案

这是预期的行为。



FacebookWebContext.Current.IsAuthorized通过Facebook cookie或Facebook signed_request来检查是否通过身份验证不。因为你使用的是不使用javascript sdk或者在画布应用程序中,它总是返回false。



一旦你得到ExchangeCodeForAccessToken()将它安全地存储在某个地方,然后使用你的自定义登录。然后使用它来知道用户是否登录。 (例如,您可能希望使用FormsAuthentication或甚至卷起您的自定义验证。)


I wonder if someone could kindly point me in the right direction.

I am using the facebook c# sdk to authenticate the user. I have implemented IFacebookApplication in my global.asax, so at a later date I can programmatically provide the AppId/AppSecret for different facebook apps.

Here is my current setup

I have this class:

public class RequestScopedFacebookApplication : IFacebookApplication
{
public RequestScopedFacebookApplication() 
{

}

public IFacebookApplication Current
{
    get
    {
        var url = HttpContext.Current.Request.Url;
        var app = new DefaultFacebookApplication();
        app.AppId = "xxx";
        app.AppSecret = "xxx";
        return app;
    }
}

public string AppId
{
    get { return Current.AppId; }
}


public string AppSecret
{
    get { return Current.AppSecret; }
}

public string CancelUrlPath
{
    get { return Current.CancelUrlPath; }
}

public string CanvasPage
{
    get { return Current.CanvasPage; }
}

public string CanvasUrl
{
    get { throw new NotImplementedException(); }
}

public string SecureCanvasUrl
{
    get { throw new NotImplementedException(); }
}

public string SiteUrl
{
    get { throw new NotImplementedException(); }
}

public bool UseFacebookBeta
{
    get { return Current.UseFacebookBeta; }
}

}

In my global.asax:

void Application_Start(object sender, EventArgs e) 
{
    // Code that runs on application startup
    Facebook.FacebookApplication.SetApplication(new RequestScopedFacebookApplication());
}

Once I have my redirect URL, I send the user of to the authorization page, on the callback page, I have something like this:

if (Request.QueryString["code"] != null)
        {

            FacebookOAuthResult result;
            if (FacebookOAuthResult.TryParse(Request.Url, out result))
            {
                if (result.IsSuccess)
                {
                    var code = result.Code;

                    var oauthClient = new FacebookOAuthClient(FacebookWebContext.Current.Settings);
                    oauthClient.RedirectUri = new Uri("...");

                    dynamic parameters = new ExpandoObject();
                    parameters.redirect_uri = "...";
                    dynamic tokenResult = oauthClient.ExchangeCodeForAccessToken(code, parameters);

                    var accessToken = tokenResult.access_token;
                    var bum = new FacebookClient(accessToken);


                }
                else
                {
                    var errorDescription = result.ErrorDescription;
                    var errorReason = result.ErrorReason;
                }
            }

        }

So now I have the token. If I do something like:

bum.get("/me/feed")

this works fine and I get some my data back.

The issue I am having is that when I try and access something from FacebookWebContext.Current.IsAuthenticated() or FacebookWebContext.Current.UserId, they are always false and 0 respectively. It looks like the Context doesn't get updated once the user has been authenticated and I have the access_token.

I need to be able to check if the user has been successfully authenticated and grab their UserId and email at a later date.

If any help would be greatly appreciated !

解决方案

it is the expected behavior.

FacebookWebContext.Current.IsAuthorized checks either facebook cookie or facebook signed_request to know whether it is authenticated or not. since you are using not using the javascript sdk or inside the canvas app it will always return false.

Once you get the ExchangeCodeForAccessToken() store it securely somewhere and then use your own custom login. Then use that to know whether the user is logged in or not. (for example, you might want to use FormsAuthentication with it or even roll up your custom auth.)

这篇关于FacebookWebContext.Current.IsAuthenticated()总是返回false的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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