C#中基于Cookie的身份验证

时间:2014-07-01 11:14:13

标签: c# rest authentication cookies restsharp

我的两个wpf应用程序存在很大问题。

我们在服务器上更改基本的基于http TO cookie的身份验证令牌。这两个应用程序使用RestSharp通过旧的基本http auth验证用户(一切正常):

try
{
    var client = new RestClient("https://" + _endpointAddr + "/");
    var request = new RestRequest("auth/", Method.GET);

    request.Credentials = _credential;

    var response = client.Execute(request);
    if (response.StatusCode == HttpStatusCode.Unauthorized) return false;
        return response.StatusCode == HttpStatusCode.OK; // Status Code is 403 NOW
}...

更改为基于cookie后,我们从服务器获取状态码403。我已经读过restsharp可以使用cookies。如何通过令牌编辑代码并使用基于cookie的身份验证对用户进行身份验证?谢谢

1 个答案:

答案 0 :(得分:0)

所以我通过添加这个来解决它:

client.FollowRedirects = false; // very important in my case
... request.addParameters ...
... client.execute(request) ...

if (response.StatusCode == HttpStatusCode.Found)
{
      if (response.Cookies.Count == 1)
      {
             msg.Cookie = response.Cookies[0].Value;
             msg.ReturnValue = true;
      }
}