4

我正在尝试将 WCF Rest Starter Kit 与RightScale 的 Login API一起使用,这似乎相当简单易用。

编辑 - 这是我写的关于使用 Powershell 使用 API的博客文章

编辑 - 为 RightScale API - NRightAPI创建了一个通用的 .NET 包装器

它就像使用 CURL 时看起来一样简单。为了让我获得登录 cookie,我需要做的就是:

curl -v -c rightcookie -u 用户名:密码“ https://my.rightscale.com/api/acct/accountid/login?api_version=1.0

我收到以下 cookie:

HTTP/1.1 204 无内容日期:2009 年 12 月 25
日星期五 12:29:24 GMT 服务器:Mongrel 1.1.3 状态:204 无内容 X-运行时:0.06121
内容类型:text/html;charset=utf-8
Content-Length: 0
Cache-Control: no-cache
添加 cookie _session_id="488a8d9493579b9473fbcfb94b3a7b8e5e3" 为域 my.rightscale.com, path /, expire 0
Set-Cookie: _session_id=488a8d9493579b9473fbcfb94b3a7b8e5e3; 路径=/; 安全变化:接受编码

但是,当我使用以下 C# 代码时:

HttpClient http = new HttpClient(" https://my.rightscale.com/api/accountid/login?api_version=1.0 ");
http.TransportSettings.UseDefaultCredentials = false;
http.TransportSettings.MaximumAutomaticRedirections = 0;
http.TransportSettings.Credentials = new NetworkCredential("username", "password");
Console.WriteLine(http.Get().Content.ReadAsString());

我得到一个重定向,而不是 HTTP 204:

您正在被<a> href="https://my.rightscale.com/dashboard">重定向<a>

如何让 WCF REST 入门工具包与 RighScale API 一起使用?

4

1 回答 1

6

我需要在我的请求中添加“授权:基本”标头。

除了我发布的初始代码:

HttpClient http = new HttpClient(" https://my.rightscale.com/api/acct/accountid/login?api_version=1.0 ");
http.TransportSettings.UseDefaultCredentials = false;
http.TransportSettings.MaximumAutomaticRedirections = 0;
http.TransportSettings.Credentials = new NetworkCredential("username", "password");

我需要添加授权标头以及带有用户名/密码的 REST 请求,如下所示:

byte[] authbytes = Encoding.ASCII.GetBytes(string.Concat("username",":", "password"));
string base64 = Convert.ToBase64String(authbytes);
string authentication = string.Concat("授权:基本", base64);
http.DefaultHeaders.Add(授权);

然后当我提出请求时:

Console.WriteLine(http.Get().Content.ReadAsString());

我收到了 HTTP 204 以及我正在寻找的会话 cookie。我能说什么,提琴手太棒了:)!

于 2009-12-26T13:50:33.697 回答