WebAPI和表单认证cookicontainer

本文关键字:cookicontainer 认证 表单 WebAPI | 更新日期: 2023-09-27 18:11:01

我成功登录到一个MVC 4 Web API站点,并获得一个ASPXAUTH Set-Cookie:

    private void Login()
    {
        var client = new RestClient("http://QQQQ/MobileAPI");
        client.CookieContainer = new CookieContainer();
        var request = new RestRequest {Resource = "/api/account/Logon", Method = Method.POST};
        request.AddObject(new {Username = "john", Password = "secret"});
        var responseLogin = client.Execute(request);
        if (responseLogin.StatusCode == HttpStatusCode.OK)
            ProcessData(client.CookieContainer);
    }

您可以看到,我将CookieContainer传递给ProcessData()函数,然后处理位于[AUTHORIZE]控制器后面的RESTful命令。

ProcessData()调用失败并返回HttpStatusCode。未经授权的

问题:我假设我没有正确发送身份验证?

    private void ProcessData(CookieContainer cookieJar)
    {
        var client = new RestClient("QQQQQ/MobileAPI");
        client.CookieContainer = cookieJar;
        var request = new RestRequest {Resource = "/api/values/5", Method = Method.GET};
        var responseForUser = client.ExecuteAsGet<AppUser>(request, "GET");
        if (responseForUser.StatusCode == HttpStatusCode.OK)
        {
            AppUser app = responseForUser.Data;
            Console.WriteLine(app.CUSAName);
        }
        if (responseForUser.StatusCode == HttpStatusCode.Unauthorized)
        {
            Console.WriteLine("FAILED");
        }
    }

WebAPI和表单认证cookicontainer

我只需要用IISRESET重置服务器。也许它的状态很糟糕。

一旦我这样做了,上面的代码在Android设备上运行得很好。