Facebook身份验证在pageload中工作,但不在按钮中工作?MVC#
我已经试了两天了,但我不明白可能是什么问题Facebook身份验证在pageload中工作,但不在按钮中工作?MVC#,c#,facebook,asp.net-mvc-4,C#,Facebook,Asp.net Mvc 4,我已经试了两天了,但我不明白可能是什么问题 我有一个到facebook的身份验证,我将一个用户重定向到facebook,这样他们就可以进行身份验证,然后我 回拨到我的网站,这样我就能得到他们注册到我的数据库所需的信息 问题:当按下按钮时,此功能不起作用,但在myactionresult中有facebookauthentication方法时,此功能起作用 (没有httppost)那么你知道为什么会发生这种情况吗 这是我的密码: public override ActionResult Ind
actionresult中有facebookauthentication
方法时,此功能起作用
(没有httppost)那么你知道为什么会发生这种情况吗
这是我的密码:
public override ActionResult Index(ExternalLoginBlock model)
{
return PartialView(model);
}
[HttpPost]
public ActionResult Facebook(ExternalLoginBlock currentBlock)
{
JObject json = CheckAuthorization();
FacebookInformation user = GetFacebookInfo(json);
SignIn(user);
var model = new ExternalLoginBlockViewModel(currentBlock);
return RedirectToAction("Index");
}
public JObject CheckAuthorization()
{
string app_id = "xxxxxx";
string app_secret = "xxxxxx";
string scope = "publish_stream,manage_pages";
if (Request["code"] == null)
{
Response.Redirect(string.Format(
"https://graph.facebook.com/oauth/authorize?client_id={0}&redirect_uri={1}&scope={2}",
app_id, "http://global.iobffp.com/SIGN-IN/", scope)); // this redirect the user and then the
// "code" gets value.
}
if(Request["code"] != null) // if it goes in here everythings fine.
{
Dictionary<string, string> tokens = new Dictionary<string, string>();
string url = string.Format("https://graph.facebook.com/oauth/access_token?client_id={0}&redirect_uri={1}&scope={2}&code={3}&client_secret={4}",
app_id, Request.Url.AbsoluteUri, scope, Request["code"].ToString(), app_secret);
HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
{
StreamReader reader = new StreamReader(response.GetResponseStream());
string vals = reader.ReadToEnd();
foreach (string token in vals.Split('&'))
{
//meh.aspx?token1=steve&token2=jake&...
tokens.Add(token.Substring(0, token.IndexOf("=")),
token.Substring(token.IndexOf("=") + 1, token.Length - token.IndexOf("=") - 1));
}
}
string access_token = tokens["access_token"];
var client = new FacebookClient(access_token);
var query = client.Get("/me?fields=picture,id,name,email,birthday,hometown");
JsonObject result1;
result1 = (Facebook.JsonObject)client.Get(query);
json = JObject.Parse(result1.ToString());
return json;
}
return json;
}
public override ActionResult索引(ExternalLoginBlock模型)
{
返回局部视图(模型);
}
[HttpPost]
公共行动结果Facebook(外部登录块currentBlock)
{
JObject json=CheckAuthorization();
FacebookInformation user=GetFacebookInfo(json);
签名(用户);
var模型=新的外部LoginBlockViewModel(currentBlock);
返回操作(“索引”);
}
公共作业项目检查授权()
{
字符串app_id=“xxxxxx”;
字符串app_secret=“xxxxxx”;
string scope=“发布\u流,管理\u页面”;
如果(请求[“代码”]==null)
{
重定向(string.Format(
"https://graph.facebook.com/oauth/authorize?client_id={0}&redirect_uri={1}&scope={2}“,
应用程序id,“http://global.iobffp.com/SIGN-IN/“,scope));//这将重定向用户,然后重定向
//“代码”获取值。
}
if(Request[“code”]!=null)//如果它在这里,一切都好。
{
字典标记=新字典();
字符串url=string.Format(“https://graph.facebook.com/oauth/access_token?client_id={0}&redirect_uri={1}&scope={2}&code={3}&client_secret={4}”,
app_id,Request.Url.AbsoluteUri,作用域,Request[“code”]。ToString(),app_secret);
HttpWebRequest-request=WebRequest.Create(url)为HttpWebRequest;
使用(HttpWebResponse=request.GetResponse()作为HttpWebResponse)
{
StreamReader=新的StreamReader(response.GetResponseStream());
字符串vals=reader.ReadToEnd();
foreach(vals.Split('&')中的字符串标记)
{
//meh.aspx?token1=steve&token2=jake&。。。
tokens.Add(token.Substring(0,token.IndexOf(“=”)),
子字符串(token.IndexOf(“=”)+1,token.Length-token.IndexOf(“=”-1));
}
}
字符串访问令牌=令牌[“访问令牌”];
var client=新的FacebookClient(访问令牌);
var query=client.Get(“/me?fields=picture、id、name、email、生日、家乡”);
JsonObject结果1;
result1=(Facebook.JsonObject)client.Get(query);
json=JObject.Parse(result1.ToString());
返回json;
}
返回json;
}
所以问题是。如果我的overrideactionresult
中有这个,一切都正常,但是如果它在我的httppost
facebookactionresult
中,它就不起作用了。
有什么想法吗