{"id":18310941,"url":"https://github.com/jimschubert/cookies-issue-187","last_synced_at":"2026-06-20T20:31:00.980Z","repository":{"id":139902204,"uuid":"135656386","full_name":"jimschubert/cookies-issue-187","owner":"jimschubert","description":null,"archived":false,"fork":false,"pushed_at":"2018-06-01T02:17:00.000Z","size":44,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"master","last_synced_at":"2025-04-09T12:11:12.763Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"C#","has_issues":false,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/jimschubert.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2018-06-01T02:12:53.000Z","updated_at":"2018-06-01T02:18:17.000Z","dependencies_parsed_at":null,"dependency_job_id":"d22a8f22-7c51-479d-bf9a-6af36b7cb837","html_url":"https://github.com/jimschubert/cookies-issue-187","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/jimschubert/cookies-issue-187","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jimschubert%2Fcookies-issue-187","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jimschubert%2Fcookies-issue-187/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jimschubert%2Fcookies-issue-187/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jimschubert%2Fcookies-issue-187/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jimschubert","download_url":"https://codeload.github.com/jimschubert/cookies-issue-187/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jimschubert%2Fcookies-issue-187/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":34585195,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-05-26T15:22:16.424Z","status":"online","status_checked_at":"2026-06-20T02:00:06.407Z","response_time":98,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-05T16:15:58.296Z","updated_at":"2026-06-20T20:31:00.963Z","avatar_url":"https://github.com/jimschubert.png","language":"C#","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Cookie passing example\n\nThis is an example for https://github.com/OpenAPITools/openapi-generator/issues/187.\n\nIt demonstrates re-using a cookie in ASP.NET Core 2.0 API between calls. It's very naive; this generates a new Guid if the cookie doesn't exist, or passes the same cookie back to the client if it does exist.\n\n## Server\n\nYou can start the server (`cd server/src/Org.OpenAPITools \u0026\u0026 dotnet run`), then curl without a cookie:\n\n```\ncurl -v http://localhost:5000/api/pets\n```\nAnd with a cookie:\n\n```\ncurl --cookie 'session-id=fc1467d9d76144c4beeb49646d7cc76a' -v http://localhost:5000/api/pets\n```\nto see how the server implementation behaves.\n\nNOTE: Querying via http://localhost:5000/swagger/ or another tool like Postman most likely won't display the cookie header.\n\n## Client\n\nThe client doesn't persist cookies automatically across queries. This is up to a consumer to maintain.\n\nThis differs a bit from a browser-side framework like fetch or xhr, where you can set a \"credentials\" flag because those frameworks either ensure a single instance per action or\nmaintain a cookiejar in a single-threaded JavaScript environment.\n\nC# is a little different. We have no control over whether or not an implementer has instantiated a client as a singleton object or if a Configuration instance is shared. In fact, the original \nC# client implementation of the template was written with both the Configuration and the ApiClient being singletons which weren't thread-safe. This has been mitigated a bit by creating better structured \nconstructors on the Api generated types and on ApiClient. Lastly, we have no way of knowing if the client should parrot back cookies (and to which endpoints). We provide partial methods for implementing extensions\nspecifically for this reason. Users can intercept the request, response, and error handling.\n\nI have a new template I'll be prposing to the community, likely for the OpenAPI Generator 4.x release. This new template will provide an abstraction \naround RESTful APIs similar to what RestSharp gives, but it decouples the generated template code from RestSharp. This allows consumers to implement other requestor libs, such as .NET's own HttpClient.\n\nThis sample demonstrates how to intercept the request and response to naively pass any existing cookie back to the server. Don't consider this thread-safe or even production-ready, it's merely an example of passing cookies presented by the service back to the service.\n\nTo do this, I've implemented a partial extension on ApiClient. (See `ApiClient.Mine.cs`):\n\n```\nusing System;\nusing System.Linq;\nusing RestSharp;\n\nnamespace Org.OpenAPITools.Client\n{\n    public partial class ApiClient\n    {\n        private static String CookieKey = \"session-id\";\n        private String cookie = null;\n        partial void InterceptRequest(IRestRequest request)\n        {\n            if (cookie == null) return;\n            Console.WriteLine(\"Add cookie to request: \" + cookie);\n            request.AddCookie(CookieKey, cookie);\n        }\n\n        partial void InterceptResponse(IRestRequest request, IRestResponse response)\n        {\n            var setCookie = response.Cookies.First(c =\u003e c.Name == CookieKey);\n            if (setCookie != null)\n            {\n                cookie = setCookie.Value;\n                Console.WriteLine(\"Cookie on response: \" + cookie);\n            }\n        }\n    }\n}\n```\n\nAssuming you have the server running from the first section in this README, you can build and run the client to evaluate:\n\n```\n$ cd client\n$ sh build.sh\n$ csharp -r:bin/JsonSubTypes.dll -r:bin/Newtonsoft.Json.dll -r:bin/Org.OpenAPITools.dll -r:bin/RestSharp.dll\nMono C# Shell, type \"help;\" for help\n\nEnter statements below.\ncsharp\u003e using Org.OpenAPITools.Client;\ncsharp\u003e using Org.OpenAPITools.Api;\ncsharp\u003e using Org.OpenAPITools.Model;\ncsharp\u003e Configuration.Default.BasePath = \"http://localhost:5000/api\";\ncsharp\u003e var api = new DefaultApi();\ncsharp\u003e api.PetsGet();\nCookie on response: 8ea9aa2dccb14801b7d43ff3d0e9013b\n{ class Pet {\n  Id: 1\n  Name: Jim\n  Tag: Person\n}\n, class Pet {\n  Id: 2\n  Name: Socks\n  Tag: Cat\n}\n, class Pet {\n  Id: 3\n  Name: Fido\n  Tag: Dog\n}\n, class Pet {\n  Id: 4\n  Name: Pookie\n  Tag: Snake\n}\n }\ncsharp\u003e api.PetsGet();\nAdd cookie to request: 8ea9aa2dccb14801b7d43ff3d0e9013b\nCookie on response: 8ea9aa2dccb14801b7d43ff3d0e9013b\n{ class Pet {\n  Id: 1\n  Name: Jim\n  Tag: Person\n}\n, class Pet {\n  Id: 2\n  Name: Socks\n  Tag: Cat\n}\n, class Pet {\n  Id: 3\n  Name: Fido\n  Tag: Dog\n}\n, class Pet {\n  Id: 4\n  Name: Pookie\n  Tag: Snake\n}\n }\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjimschubert%2Fcookies-issue-187","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjimschubert%2Fcookies-issue-187","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjimschubert%2Fcookies-issue-187/lists"}