3

我在 Windows Phone 设备上使用 RestSharp 客户端时遇到问题。从一开始,我就在线托管了 ASP.NET Web Api 服务。我有一个请求用户地址:POST:http ://my-service-url.com/token ,我在其中发送电子邮件和密码作为正文参数,并在响应中获得 201 状态代码和 cookie。当我在提琴手中这样做时,一切正常。我还对使用 RestSharp 的 API 进行了功能测试,该测试也正常工作:

[Given(@"I fill email and password with correct data and I click log in button")]
public void GivenIFillEmailAndPasswordWithCorrectDataAndIClickLogInButton()
{
        //Delete user if he exists
        _testUserHelper.DeleteTestUser(TestEmail);

        //Create new activated user
        Assert.IsTrue(_testUserHelper.CreateTestUser(TestEmail, TestPassword));

        //Prepare client
        var client = new RestClient(CarRentalsConstants.HostAddress);
        var restRequest = new RestRequest("api/token", Method.POST) { RequestFormat = DataFormat.Json };

        //Add parameters to request
        restRequest.AddBody(new { Email = TestEmail, Password = TestPassword });

        //Perform request
        _response = client.Execute(restRequest);
    }

    [When(@"the log in login process finishes")]
    public void WhenTheLogInLoginProcessFinishes()
    {
        Assert.AreEqual(HttpStatusCode.Created, _response.StatusCode, _response.Content);
        Assert.IsNotNull(_response.Cookies.SingleOrDefault(q => q.Name == ".ASPXAUTH"););
    }

上面的一个可以正常工作,并且 cookie 在响应对象中。

现在我尝试在 Windows Phone 上执行的操作如下所示:

var restRequest = new RestRequest("token", Method.POST) {RequestFormat = DataFormat.Json};
restRequest.AddBody(new {Email = email, Password = password});
myWebClient.ExecuteAsync(restRequest, (restResponse, handle) =>
{
    switch (restResponse.StatusCode)
    {
        case HttpStatusCode.Created:
        {
            var cookie = restResponse.Cookies.SingleOrDefault(q => q.Name == ".ASPXAUTH");
            successLogicDelegate(cookie);
        }
        break;
        case HttpStatusCode.BadRequest:
        {
            HandleBadRequest(restResponse.Content, failureLogicDelegate);
        }
        break;
        default:
        msgBox.Show(StringResources.ServerConnectionError);
        failureLogicDelegate(null);
        break;
   }
});

在这种情况下,响应返回“已创建”状态代码,但 cookie 随后设置为 null。我不知道这里发生了什么,但我相当肯定服务器正在发送这个 cookie,那么它在哪里丢失了?

任何帮助将不胜感激。

4

0 回答 0