在 WCF 服务中设置 cookie
我有一个使用 wcf Rest 服务的 asp mvc 应用程序(全部在同一个盒子上)。对于身份验证调用, 我正在尝试在 wcf 休息服务中设置 cookie。
客户端代码 -
HttpResponseMessage resp;
HttpClient client = new HttpClient("http://localhost/auth/login/");
resp = client.Get();
在 Web 服务中,我仅使用 FormsAuthentication 来设置 authcookie。
HttpCookie authCookie = FormsAuthentication.GetAuthCookie("foo", false);
HttpContext.Current.Response.Cookies.Add(authCookie);
假设凭据是硬编码在代码中的 - 如果我物理导航到浏览器页面
http://localhost/auth/login
(代码中的硬编码凭据),我可以看到正在设置身份验证 cookie。但是,如果我只是通过代码调用它(如上所示),则不会设置身份验证 cookie。
我在这里忽略了一些明显的东西吗?
I have a asp mvc application consuming wcf rest services (all on the same box). For authentication calls,
I am trying to set cookies inside a wcf rest service.
Code on the client side -
HttpResponseMessage resp;
HttpClient client = new HttpClient("http://localhost/auth/login/");
resp = client.Get();
In the webservice I just use FormsAuthentication to set an authcookie.
HttpCookie authCookie = FormsAuthentication.GetAuthCookie("foo", false);
HttpContext.Current.Response.Cookies.Add(authCookie);
Assuming the credentials are hardcoded in the code - If I physically navigate to the browserpage
http://localhost/auth/login
(hard code credentials in the code) I can see that the authentication cookie is being set. However, if I just invoke it through code (as shown above) the authentication cookie is not being set.
Is there something obvious that I am overlooking here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当您以编程方式调用 WCF 服务时,它设置的 cookie 存储在 HttpClient 实例中。客户端浏览器永远不会看到它,因此它永远不会设置在其中。所以你需要手动设置它:
When you call the WCF service programatically the cookie it sets is stored in the HttpClient instance. The client browser never sees it so it is never set inside it. So you will need to set it manually: