C#,找不到适用于 oAuth 和 facebook 服务器端登录过程的 UrlEncoder

本文关键字:登录 服务器端 过程 UrlEncoder facebook 找不到 适用于 oAuth | 更新日期: 2023-09-27 18:31:56

我正在尝试在我的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 Conconding 才能使其工作

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

C#,找不到适用于 oAuth 和 facebook 服务器端登录过程的 UrlEncoder

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

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

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;
  }

万一有人需要它他克斯