C# 使用FacebookWebContext时,授权应用未显示为IsAuthorized()
我正在使用Facebook的C#SDK。这主要基于ASPNet-MVC3-JsSdk示例。在我的应用程序中,我有标准的Javascript代码(大部分情况下直接取自示例应用程序) 我基本上想重定向到我的C# 使用FacebookWebContext时,授权应用未显示为IsAuthorized(),c#,asp.net-mvc-3,facebook-c#-sdk,facebook-authentication,C#,Asp.net Mvc 3,Facebook C# Sdk,Facebook Authentication,我正在使用Facebook的C#SDK。这主要基于ASPNet-MVC3-JsSdk示例。在我的应用程序中,我有标准的Javascript代码(大部分情况下直接取自示例应用程序) 我基本上想重定向到我的帐户.FacebookLogon()控制器操作,在这里我检查授权,如果用户已经授权了应用程序,那么我设置一个FormsAuthenticationCookie/令牌 public ActionResult FacebookLogon(string returnURL) { var fbWe
帐户.FacebookLogon()
控制器操作,在这里我检查授权,如果用户已经授权了应用程序,那么我设置一个FormsAuthenticationCookie/令牌
public ActionResult FacebookLogon(string returnURL)
{
var fbWebContext = new FacebookWebContext(FacebookApplication.Current, ControllerContext.HttpContext); // or FacebookWebContext.Current;
if (fbWebContext.IsAuthorized(@MyApp.Controllers.AccountController.ExtendedPermissions.Split(',')))
{
// If the User is Logged in and Authorized
var fbwc = new FacebookWebClient();
dynamic me = fbwc.Get("me");
FormsAuthenticationTicket authTicket = new
FormsAuthenticationTicket(1, //version
me.username, // user name
DateTime.Now, //creation
DateTime.Now.AddMinutes(30), //Expiration
false, //Persistent
me.username);
string encTicket = FormsAuthentication.Encrypt(authTicket);
this.Response.Cookies.Add(new HttpCookie(FormsAuthentication.FormsCookieName, encTicket));
}
return Redirect(Url.Content("~/" + returnURL));
}
在某种程度上,每件事都非常出色:
我点击我的Facebook登录按钮
- 当我点击“登录”时,我会看到授权屏幕
- 我授权扩展权限(使用新的扩展身份验证)
- Javascript将我重定向到我的
方法AccountController.FacebookLogon()
方法检查Facebook是否已授权,而C#SDK表示未授权FacebookLogon()
任何人?
如果使用Windows身份基础,可以使用基于声明的身份,而HttpContext.Current.User将填写脸谱网登录数据。public ActionResult FacebookLogon(string returnURL)
{
var fbWebContext = new FacebookWebContext(FacebookApplication.Current, ControllerContext.HttpContext); // or FacebookWebContext.Current;
if (fbWebContext.IsAuthorized(@MyApp.Controllers.AccountController.ExtendedPermissions.Split(',')))
{
// If the User is Logged in and Authorized
var fbwc = new FacebookWebClient();
dynamic me = fbwc.Get("me");
FormsAuthenticationTicket authTicket = new
FormsAuthenticationTicket(1, //version
me.username, // user name
DateTime.Now, //creation
DateTime.Now.AddMinutes(30), //Expiration
false, //Persistent
me.username);
string encTicket = FormsAuthentication.Encrypt(authTicket);
this.Response.Cookies.Add(new HttpCookie(FormsAuthentication.FormsCookieName, encTicket));
}
return Redirect(Url.Content("~/" + returnURL));
}