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

发布于 2024-11-13 23:07:53 字数 1433 浏览 3 评论 0原文

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

我计划使用 HttpContext.Current.Response.Cookies.Add(cookie) 来传递新的 cookie,但这似乎不起作用,因为没有添加 cookie所以...

对可能出什么问题有什么想法吗?

这是我的代码:

var service = new localhost.UserManagement();
service.CookieContainer = new CookieContainer();
if (service.AuthenticateUser("[email protected]", "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;
        }

I'm integrating a single sign on over 2 ASP.Net applications. For that matter i have a web service that is called by the main app. when a user logs in. this web service authenticates the user in my second application and brings back the authentication cookies i need to deliver to the client browser so he can navigate freely and logged in both applications.

I was planning to use HttpContext.Current.Response.Cookies.Add(cookie) in order to deliver the new cookies but this seems not to work as no cookies are added what so ever...

Any ideas on what might be going wrong?

here is my code:

var service = new localhost.UserManagement();
service.CookieContainer = new CookieContainer();
if (service.AuthenticateUser("[email protected]", "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;

Note: CookieConverter(item) is used to convert Cookie object i receive to HttpCookie

Thanks


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;
        }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

自由如风 2024-11-20 23:07:53

您应该检查:

  • 集合为空?你能设置断点并检查集合吗?
  • 该代码位于哪里? (.aspx 页面、Web 服务、http 处理程序?)
  • 尝试创建简约的“Cookie 设置器”,只需以任何方式添加简单的 cookie

You should check:

  • collection is empty? Could you set braeakpoint and check collection?
  • where is this code located? (.aspx page, web service, http handler?)
  • try to create minimalistic "Cookie setter" that just add simple cookie in any way
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文