Reputation: 35
Just getting started building an app using the v6 of the facebook c# sdk and stuck on the login process for facebook. I have the following code in my page:
string client_id = ConfigurationManager.AppSettings["APIKey"].ToString();
string client_secret = ConfigurationManager.AppSettings["Secret"].ToString();
string redirect_uri = ConfigurationManager.AppSettings["Url"].ToString() + "default2.aspx";
FacebookClient objClient = new FacebookClient();
var fbLoginUrl = objClient.GetLoginUrl(new { client_id = client_id,
client_secret = client_secret,
redirect_uri = redirect_uri,
response_type = "code",
display = "popup",
scope = "manage_pages,publish_stream,read_stream",
state = "" });
//msg.Text = fbLoginUrl.ToString();
Response.Redirect(fbLoginUrl.ToString());
When I go to the page, the redirect to log in never happens. What am I doing wrong?
Upvotes: 1
Views: 755
Reputation: 245
With MVC 3 (look at the published example), i make a view where the login starts. The controller is :
public ActionResult Logon()
{
_fb = new FacebookClient();
var csrfToken = Guid.NewGuid().ToString();
Session["fb_csrf_token"] = csrfToken;
var state = Convert.ToBase64String(Encoding.UTF8.GetBytes(_fb.SerializeJson(new { returnUrl = returnUrl, csrf = csrfToken })));
var fbLoginUrl = _fb.GetLoginUrl(
new
{
client_id = AppId,
client_secret = Appsecret,
redirect_uri = RedirectUri,
response_type = "code",
scope = Scope,
state = state
});
return Redirect(fbLoginUrl.AbsoluteUri);
}
where returnUrl is the connected view area in my app (/home/fbhome) and RedirectUri is the view (loginresult) which will deals the second step :
public ActionResult Loginresult(string code, string state)
{
if (string.IsNullOrWhiteSpace(code) || string.IsNullOrWhiteSpace(state))
return RedirectToAction("Index", "Home");
// first validate the csrf token
_fb = new FacebookClient();
dynamic decodedState;
try
{
decodedState = _fb.DeserializeJson(Encoding.UTF8.GetString(Convert.FromBase64String(state)), null);
var exepectedCsrfToken = Session["fb_csrf_token"] as string;
// make the fb_csrf_token invalid
Session["fb_csrf_token"] = null;
if (!(decodedState is IDictionary<string, object>) || !decodedState.ContainsKey("csrf") || string.IsNullOrWhiteSpace(exepectedCsrfToken) || exepectedCsrfToken != decodedState.csrf)
{
return RedirectToAction("Index", "Home");
}
}
catch
{
// log exception
return RedirectToAction("Index", "Home");
}
try
{
dynamic result = _fb.Post("oauth/access_token",
new
{
client_id = AppId,
client_secret = Appsecret,
redirect_uri = RedirectUri,
code = code
});
Session["fb_access_token"] = result.access_token;
if (result.ContainsKey("expires"))
Session["fb_expires_in"] = DateTime.Now.AddSeconds(result.expires);
if (decodedState.ContainsKey("returnUrl"))
{
if (Url.IsLocalUrl(decodedState.returnUrl))
return Redirect(decodedState.returnUrl);
return Redirect(decodedState.returnUrl + "/notlocal");
}
return RedirectToAction("Index", "Home");
}
catch
{
// log exception
return RedirectToAction("Index", "Home");
}
}
So, as far as i understand, the user enter the site (/home/index) then click on a link to connect (/home/loggon), facebook answer to (/home/loginresult) and the user is redirected to /home/fbhome
Hope this helped
Sorry for my english !
Upvotes: 1