0

我正在尝试在我的 C# MVC 应用程序中使用 facebook 进行服务器端登录过程。

在我的应用程序中提交状态更新时,它必须在 facebook 上发布,我重定向到:

https://www.facebook.com/dialog/oauth?client_id={0}&scope={1}&redirect_uri={2}

使用此方法:

Response.Redirect(...);

我的重定向 uri 编码如下:

HttpUtility.UrlEnconde("http://local.mydomain.com/getAuthToken?message=the original message");

回到我的 MVC 应用程序(接受 fb 权限后),getAuthToken 操作向 URI 发出请求:

 https://graph.facebook.com/oauth/access_token?client_id={0}&client_secret={1}&code={2}&redirect_uri={3}

使用此方法:

 using (var wc = new WebClient()){
      wc.DownloadString(...)
 }

我的重定向 uri 再次编码如下:

 HttpUtility.UrlEnconde("http://local.mydomain.com/getAuthToken?message=the original message");

如果“原始消息”不包含空格,它可以工作,但是当我开始添加空格和其他特殊字符时,我得到一个 OAuth 异常:

验证验证码时出错

所以我的问题是,我应该使用什么样的 URL Enconding 才能使其工作

我在这里找到了一些东西,但我遇到了同样的错误,我不知道我是否需要一种特殊的方法来进行重定向,或者我是否需要为这两个请求使用不同的编码。

4

2 回答 2

0

为了解决这个问题,我用它来编码:

static public string EncodeTo64(string toEncode)
  {
     byte[] toEncodeAsBytes
           = Encoding.ASCII.GetBytes(toEncode);
     string returnValue
           = Convert.ToBase64String(toEncodeAsBytes);
     return returnValue;
  }

这要解码

  static public string DecodeFrom64(string encodedData)
  {
     byte[] encodedDataAsBytes
         = Convert.FromBase64String(encodedData);
     string returnValue =
        Encoding.ASCII.GetString(encodedDataAsBytes);
     return returnValue;
  }

万一有人需要它

于 2012-07-13T22:04:51.530 回答
0

我建议你看看 Dino Cheisa 的优秀 OAuth.Manager 类。这会正确执行 URL 编码。您可以从http://cheeso.members.winisp.net/OAuthManager1.1/html/N_OAuth.htm获取

于 2012-04-21T20:01:05.823 回答