Skip to main content
 首页 » 编程设计

c#之在静态 web 方法中使用 Request 和 Response 变量

2024年01月13日30shangdawei

这是我的代码,

[WebMethod] 
public static bool FirstAccess() 
{ 
    string app_id = "139295982898884"; 
    string app_secret = "b5d2a88b56898610d54da2af498e3220"; 
    //string post_login_url = "http://dev.fitbook.com.asp1-23.ord1-1.websitetestlink.com/v4/FitbookContest.aspx"; 
    string post_login_url = "http://localhost:4327/FitbookContest.aspx"; 
    string scope = "publish_stream,manage_pages"; 
 
    string code = HttpContext.Current.Request["code"] ?? ""; 
 
    try 
    { 
        if (code == "") 
        { 
            string dialog_url = "http://www.facebook.com/dialog/oauth?" + "client_id=" + app_id + "&redirect_uri=" + HttpContext.Current.Server.UrlEncode(post_login_url) + "&scope=publish_stream"; 
            // HttpContext.Current.Response.Write(dialog_url); 
            //HttpContext.Current.Response.Redirect(dialog_url, true); 
            return false; 
        } 
        else 
        { 
            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, HttpContext.Current.Request.Url.AbsoluteUri, scope, HttpContext.Current.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)); 
                } 
            } 
            access_token = tokens["access_token"]; 
            TestFB.GetProfilePic(code); 
            return true; 
        } 
    } 
    catch (Exception ex) 
    { 
        return false; 
    } 
} 

这里的行是

HttpContext.Current.Request["code"] ?? ""; 

和

HttpContext.Current.Response.Redirect(dialog_url, true); 

不工作。那么有没有其他解决方案呢?有什么方法可以使用吗?

请您参考如下方法:

你试过吗,

string dialog_url = "~http://www.facebook.com/dialog/oauth?" + "client_id=" + app_id + "&redirect_uri=" + HttpContext.Current.Server.UrlEncode(post_login_url) + "&scope=publish_stream"; 

并且,redirect as false表示当前页面的执行是否应该终止

HttpContext.Current.Response.Redirect(dialog_url,false); 

简单地试试这个:

string code = HttpContext.Current.Request["code"]; 
 
if (code==null) 
{ 
  // code 
} 

而且我认为问题出在线路上。您没有指定访问 token

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, HttpContext.Current.Request.Url.AbsoluteUri, scope, HttpContext.Current.Request["code"].ToString(), app_secret);