Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I convert an NSHttpCookie to a System.Net.Cookie in MonoTouch?

I have a MonoTouch iPhone app that does federated sign in via the Azure Access Control Service. The login is done via an embedded UIWebView browser. When the login is done, I want to transfer the cookie into my app. I have access to the

NSHttpCookieStorage.SharedStorage.Cookies

collection, so I can find the cookie. But in order to call the back end services, I need to have a

System.Net.Cookie

that I can put into a CookieContainer to send to the service.

How do I convert between the two... is this the only way?

NSHttpCookie cookie = NSHttpCookieStorage.SharedStorage.Cookies[0];
System.Net.Cookie newCookie = new System.Net.Cookie()
    {
        Name = cookie.Name,
        Value = cookie.Value,
        Version = (int) cookie.Version,
        Expires = cookie.ExpiresDate,
        Domain = cookie.Domain,
        Path = cookie.Path,
        Port = cookie.PortList[0].ToString(), // is this correct??
        Secure = cookie.IsSecure,
        HttpOnly = cookie.IsHttpOnly
    };
like image 963
Roy Avatar asked Feb 23 '11 22:02

Roy


1 Answers

Yes, that is how you could convert. Perhaps you should just make an extension method on NSHttpCookie? Then you could call something like:

var c = cookie.ToCLRCookie ();
like image 167
Geoff Norton Avatar answered Oct 06 '22 07:10

Geoff Norton