ASP.NET将Cookie发送到客户端浏览器

时间:2011-06-02 10:59:34

标签: c# .net asp.net cookies httpcontext

我在2个ASP.Net应用程序上集成了单个登录。就此而言,我有一个由主应用程序调用的Web服务。当用户登录时,此Web服务在我的第二个应用程序中对用户进行身份验证,并将我需要提供的身份验证cookie带回客户端浏览器,以便他可以自由导航并登录这两个应用程序。

我打算使用 HttpContext.Current.Response.Cookies.Add(cookie) 来提供新的Cookie,但这似乎不适用于没有Cookie添加了什么......

关于可能出错的任何想法?

这是我的代码:

var service = new localhost.UserManagement();
service.CookieContainer = new CookieContainer();
if (service.AuthenticateUser("test@user.pt", "test"))
{ 
    var collection = service.CookieContainer.GetCookies(new Uri("http://localhost"));
    foreach (Cookie item in collection)
    {
        HttpContext.Current.Response.Cookies.Add(CookieConverter(item));
    }
    HttpContext.Current.Response.Flush();
    return true;
}

return false;

注意: CookieConverter(item) 用于将我收到的Cookie对象转换为HttpCookie

由于


private HttpCookie CookieConverter(Cookie cookie)
        {
            var result = new HttpCookie(cookie.Name);

            result.Value = cookie.Value;
            result.Domain = cookie.Domain;
            result.Expires = cookie.Expires;
            result.Path = cookie.Path;
            result.Secure = cookie.Secure;
            result.HttpOnly = cookie.HttpOnly;

            return result;
        }

1 个答案:

答案 0 :(得分:0)

你应该检查:

  • 收藏是空的?你能设置braeakpoint并检查收集吗?
  • 此代码位于何处? (.aspx页面,Web服务,http处理程序?)
  • 尝试创建简约的“Cookie setter”,只需以任何方式添加简单的Cookie
相关问题