我正在尝试设置 Facebook Connect 身份验证,但拉回访问令牌需要 30 秒或更长时间。当我通过网络浏览器浏览到生成的 URL 时,它会在不到 1 秒的时间内返回。花了这么长时间的代码行是using (StreamReader sr = new StreamReader(request.GetResponse().GetResponseStream()))
我尝试替换WebRequest
为WebClient
希望它会更快,但没有这样的运气。一旦我有了令牌,下一个WebRequest
实际提取用户数据的速度就非常快了。我知道这不是正常行为,因为我已经看到很多网站非常快速地进行此身份验证。任何建议将不胜感激。这是我正在使用的代码:
public ActionResult FBLogin()
{
string uri = string.Format("http://{0}{1}/Shared/FBLogin", Request.Url.Host, Request.UrlIsDefaultPort ? string.Empty : string.Format(":{0}", Request.Url.Port));
string code = Request["Code"];
string state = Request["State"];
if (Request["Code"].IsNullOrEmptyTrim())
{
state = MD5.GetMD5String(Guid.NewGuid().ToString());
Session["State"] = state;
string redirectUrl = string.Format("https://www.facebook.com/dialog/oauth?client_id={0}&redirect_uri={1}&scope=email&state={2}", Settings.FacebookAppID, uri, state);
return Redirect(redirectUrl);
}
else
{
if (state.Equals(Session["State"]))
{
Session["State"] = null;
string tokenUrl = string.Format("https://graph.facebook.com/oauth/access_token?client_id={0}&redirect_uri={1}&client_secret={2}&code={3}", Settings.FacebookAppID, uri, CryptoProvider.DecryptString(Settings.FacebookSecretKey), code);
string token = null;
WebRequest request = WebRequest.Create(tokenUrl);
using (StreamReader sr = new StreamReader(request.GetResponse().GetResponseStream()))
{
token = ParseQueryString(sr.ReadToEnd())["access_token"];
}
if (token != null)
{
string userUrl = string.Format("https://graph.facebook.com/me?access_token={0}", token);
request = WebRequest.Create(userUrl);
dynamic user;
using (StreamReader sr = new StreamReader(request.GetResponse().GetResponseStream()))
{
JavaScriptSerializer serializer = new JavaScriptSerializer();
user = serializer.DeserializeObject(sr.ReadToEnd());
}
if (user != null)
{
// handle login
}
}
}
return Redirect(Request.Cookies["LastURL"].Value);
}
}
提前致谢。