I would like to create a demo login service in web api and need to set a cookie on the response. How do I do that? Or are there any better way to do authorization?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Add a reference to
System.Net.Http.Formatting.dlland use theAddCookiesextension method defined in theHttpResponseHeadersExtensionsclass.Here is a blog post describing this approach, and the MSDN topic.
If that assembly isn’t an option for you, here’s my older answer from before this was an option:
Older answer follows
I prefer an approach that stays within the realm of
HttpResponseMessagewithout bleeding into the HttpContext which isn’t as unit testable and does not always apply depending on the host:Then you can include a cookie in the response like this: