带有Cookie的Odata客户端

时间:2015-04-07 14:37:58

标签: c# authentication xamarin odata

已实施以下代码以从Cookie中获取JsessioniD。 webSite使用表单身份验证。

这是我实施的内容。

public override void ViewDidLoad ()
  {
    base.ViewDidLoad ();
    using(var client= new CookieAwareWebClient())
    {
        var values= new NameValueCollection
           {
             {"username","admin"},
             {"password","admin"},
           };
            client.UploadValues("myURL/j_security_check",values);
            Cookie jSessionID = client.ResponseCookies["JSESSIONID"];
            if (jSessionID != null)
            {
                // get the JEssionID here
               string value = jSessionID.Value;
             }
    };
}

public class CookieAwareWebClient : WebClient
{
    public CookieAwareWebClient()
    {
        CookieContainer = new CookieContainer();
        this.ResponseCookies = new CookieCollection();
    }

    public CookieContainer CookieContainer { get; private set; }
    public CookieCollection ResponseCookies { get; set; }

    protected override WebRequest GetWebRequest(Uri address)
    {
        var request = (HttpWebRequest)base.GetWebRequest(address);
        request.CookieContainer = CookieContainer;
        return request;
    }

    protected override WebResponse GetWebResponse(WebRequest request)
    {
        var response = (HttpWebResponse)base.GetWebResponse(request);
        this.ResponseCookies = response.Cookies;
        return response;
    }
}

我得到了JSessionID,现在我的问题是如何使用cookie标头调用odata客户端?

1 个答案:

答案 0 :(得分:0)

我没有在这台PC上安装Xamarin进行测试,但应该如下所示。

没有改变很多,只是检查每个响应上的.ASPXAUTH cookie,如果存在,则存储它。对于每个请求,如果存储了cookie值,则会将其添加到请求中。

public override void ViewDidLoad()
{
    base.ViewDidLoad();

    using (var client = new CookieAwareWebClient())
    {
        var values = new NameValueCollection
        {
            {"username", "admin"},
            {"password", "admin"},
        };

        // Make call to authenticate
        client.UploadValues("myURL/j_security_check", values);

        // Make call to get data or do something as authenticated user
        var dataYouWant = client.DownloadString("myURL/page_with_data");
    }
}

public class CookieAwareWebClient : WebClient
{
    private const string COOKIEKEY = ".ASPXAUTH";
    public string AspAuthCookieValue { get; set; }

    protected override WebRequest GetWebRequest(Uri address)
    {
        var request = (HttpWebRequest) base.GetWebRequest(address);

        if (!string.IsNullOrEmpty(AspAuthCookieValue))
        {
            request.CookieContainer.Add(new Cookie
            (
                COOKIEKEY,
                AspAuthCookieValue
            ));
        }

        return request;
    }

    protected override WebResponse GetWebResponse(WebRequest request)
    {
        var response = (HttpWebResponse) base.GetWebResponse(request);
        var authCookie = response.Cookies[COOKIEKEY];

        if (authCookie != null && !string.IsNullOrEmpty(authCookie.Value))
        {
            AspAuthCookieValue = authCookie.Value;
        }

        return response;
    }
}

不完美,但应该让你知道需要什么。

<强>更新

以前没有使用过Simple.OData但是看过API我相信你应该可以做类似的事情:

public class GetSomeOData
{
    private const string COOKIEKEY = ".ASPXAUTH";
    private string _cookieValue;

    public override void ViewDidLoad()
    {
        base.ViewDidLoad();

        using (var client = new CookieAwareWebClient())
        {
            var values = new NameValueCollection
            {
                {"username", "admin"},
                {"password", "admin"},
            };

            // Make call to authenticate
            client.UploadValues("myURL/j_security_check", values);

            _cookieValue = client.AspAuthCookieValue;

            // Make call to get data or do something as authenticated user
            var dataYouWant = GetOData();
        }
    }

    public dynamic GetOData()
    {
        var feed = new ODataFeed("http://www.your-odata-url.com");
        feed.BeforeRequest += AddCookieToRequest;
        var db = Database.Opener.Open(feed);

        return db.SomeData.FindById(1234);
    }

    public void AddCookieToRequest(HttpRequestMessage request)
    {
        request.Headers.Add("Cookie", String.Format("{0}={1}", COOKIEKEY, _cookieValue));
    }

    public class CookieAwareWebClient : WebClient
    {
        public string AspAuthCookieValue { get; set; }

        protected override WebResponse GetWebResponse(WebRequest request)
        {
            var response = (HttpWebResponse)base.GetWebResponse(request);
            var authCookie = response.Cookies[COOKIEKEY];

            if (authCookie != null && !String.IsNullOrEmpty(authCookie.Value))
            {
                AspAuthCookieValue = authCookie.Value;
            }

            return response;
        }
    }
}

同样,它并不完美,但应该演示如何获取身份验证cookie,然后将其应用于您的OData请求。