如何将NSHttpCookie转换为MonoTouch中的System.Net.Cookie?

Roy*_*Roy 6 iphone xamarin.ios

我有一个MonoTouch iPhone应用程序通过Azure访问控制服务进行联合登录.登录通过嵌入式UIWebView浏览器完成.登录完成后,我想将cookie转移到我的应用程序中.我可以访问

NSHttpCookieStorage.SharedStorage.Cookies
Run Code Online (Sandbox Code Playgroud)

集合,所以我可以找到cookie.但是为了调用后端服务,我需要有一个

System.Net.Cookie
Run Code Online (Sandbox Code Playgroud)

我可以放入CookieContainer发送到服务.

我如何在两者之间进行转换......这是唯一的方法吗?

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
    };
Run Code Online (Sandbox Code Playgroud)

Geo*_*ton 3

是的,这就是你可以转换的方式。也许你应该在 NSHttpCookie 上创建一个扩展方法?然后你可以这样调用:

var c = cookie.ToCLRCookie ();
Run Code Online (Sandbox Code Playgroud)