问题
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 !
回答1:
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.)
来源:https://stackoverflow.com/questions/8524459/facebookwebcontext-current-isauthenticated-always-returns-false