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

この場合、応答は「Created」ステータス コードを返しますが、Cookie は null に設定されます。ここで何が起こっているのかわかりませんが、サーバーがこの Cookie を送信していることは確かです。

どんな助けでも本当に感謝します。

4

0 に答える 0