简体   繁体   English

使用C#桌面应用程序在默认浏览器中设置Cookie

[英]Set cookie in default browser using c# desktop application

I am trying to open a url which needs username/psw combination from a desktop app. 我正在尝试从桌面应用程序打开需要用户名/ psw组合的网址。 Desktop app already has an authentication mechanism. 桌面应用程序已具有身份验证机制。 What i want is if someone tries to open the url then i'll pass the access token from desktop app to the web, so that user don't need to login again. 我想要的是,如果有人尝试打开url,那么我会将访问令牌从桌面应用程序传递到Web,这样用户就无需再次登录。 One crude way to do this is to check the default browser using registry and depending on the default browser set the cookie in their local sqllite file(for chrome and firefox). 一种粗略的方法是使用注册表检查默认浏览器,并根据默认浏览器在其本地sqllite文件(对于chrome和firefox)中设置cookie。 But this approach doesn't seems to be the good one. 但是这种方法似乎不是一个好方法。 Is there any other way to do this ? 还有其他方法吗?

The most crude way would be to pass it as a query string parameter. 最粗略的方法是将其作为查询字符串参数传递。 This may not be ideal as you might not want it to show, although you could have the target site do a quick redirect after authenticating. 尽管您可能希望目标站点在身份验证后进行快速重定向,但这可能并不理想,因为您可能不希望显示它。 You also want to be sure to only do this over HTTPS. 您还希望确保仅通过HTTPS执行此操作。

Another way if you'd prefer to POST the data would be to write out a local HTML file with a form that auto submits on load. 如果您希望POST数据,则另一种方法是写出一个本地HTML文件,该文件具有在加载时自动提交的表单。 Have the access token in a hidden field on the form. 将访问令牌放在表单的隐藏字段中。 This would allow you to do a POST. 这将允许您执行POST。

In either case, you can launch a browser to a specific page using Process.Start("https://example.com/mypage") . 无论哪种情况,都可以使用Process.Start("https://example.com/mypage")将浏览器启动到特定页面。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM