在 ASP.net 中保存用户 ID

发布于 2024-07-15 10:38:45 字数 91 浏览 6 评论 0原文

我正在使用 ASP.net 开发一个 Intranet 网站。 我需要在网站中所有页面的所有回发中保留用户 ID。 是否建议将这些信息保存在会话中或其他可用的方式。

I am using ASP.net for developing an intranet website. I need to hold the userid across all postbacks for all the pages in the website. Is it advisable to hold those information in Session or somether way is available.

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

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

发布评论

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

评论(3

远昼 2024-07-22 10:38:45

FormsAuthentication 还能够保存自定义用户 ID,并解决了很多问题您

可以使用 RedirectFromLoginPage 方法,然后使用 FormsAuthentication_OnAuthenticate事件来查找并设置 Page.User 属性,以访问您需要的所有其他逻辑。

FormsAuthentication is also capable of holding a custom userid, and solves a lot of things for you like setting the cookie, login page redirection etc.

You can set the userid using the RedirectFromLoginPage method and then use the FormsAuthentication_OnAuthenticate event to find and set the Page.User property, to access all the other logic you need.

薄暮涼年 2024-07-22 10:38:45

如果使用表单身份验证,则用户名将可用为:

Page.User.Identity.Username

如果您需要在此同时保存另一条信息,则使用 Session。

附言。 我建议使用类来包装会话变量,以实现强类型和潜在的默认值。

If using Forms Authentication, then the username will be available as:

Page.User.Identity.Username

If you need to hold another piece of information alongisde this, then use Session.

PS. I recommend using a class to wrap your session variables for strong-typedness and potential default values.

白馒头 2024-07-22 10:38:45

最常见的方法是使用 ASP.NET 会话对象或使用 cookie。

无论哪种方式都可以很好地工作,但如果您希望即使在会话超时(例如关闭浏览器窗口)后仍保留其用户信息,那么您可能需要研究 cookie。 会话信息将在浏览器关闭或达到活动超时时被处理。

The most common ways of doing this are using either the ASP.NET Session Object or using cookies.

Either way will work well, but if you wish for their user information to persist even after the session has timed out (such as the closing of a browser window), then you would want to look into cookies. Session information will be disposed upon closing of a browser, or the activity timeout has been reached.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文