ASP.Net 在会话 cookie 中存储用户密码?

发布于 2024-12-22 18:00:33 字数 181 浏览 1 评论 0原文

我知道会员资格提供商将用户名和过期时间存储在加密的 cookie 中,然后使用它来验证用户是否仍然登录会话。

是否也可以将用户密码存储在这个加密的 cookie 中。如果是这样,您将如何在服务器端访问它?

我需要服务器端可用的用户名和密码,因为我需要调用使用这些相同凭据的 Web 服务。有更好的方法来做到这一点吗?

I know the Membership provider stores the user name and an expiration time in an encrypted cookie and then uses that to verify the user is still logged in for a session.

Would it be possible to store the users password in this encrypted cookie as well. If so how would you access it server side?

I need the users username and password available server side because I need to call web services that use those same credentials. Is there some better way to do this?

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

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

发布评论

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

评论(3

德意的啸 2024-12-29 18:00:33

您应该将其存储在会话状态中,该状态永远不会离开服务器。

您还应该尝试更改这些 Web 服务以使用身份验证票证而不是密码(例如 OAuth),因为以纯文本形式存储密码从来都不是一个好主意。

You should store it in session state, which never leaves the server.

You should also try to change those web services to use authentication tickets instead of passwords (eg, OAuth), because it's never a good idea to store passwords in plain text.

故人爱我别走 2024-12-29 18:00:33

是的,你可以这样做。您可以在 FormsAuthenticationTicket 构造函数的 userData 字段中传递编码信息:

  FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(version,
    name, issueDate, expirationDate, isPersistent, yourEncodedData);
  string secureTicket = FormsAuthentication.Encrypt(ticket);
  Response.Cookies.Add(
      new HttpCookie(FormsAuthentication.FormsCookieName, secureTicket));

理想情况下,这应该通过 SSL 连接完成,并且票证 cookie 应该使用 HttpOnly 和 Secure 属性进行标记。

然后,要检索该值:

FormsIdentity id = (FormsIdentity)User.Identity;
FormsAuthenticationTicket ticket = id.Ticket;
string yourEncodedInfo = ticket.UserData;

您也可以设置自己的 cookie,与表单身份验证票证分开。

然而,从安全角度来看,即使加密,直接将密码存储在 cookie 中也不是一个好主意。相反,使用会话状态:

Session["password"] = password;

会话状态也使用 cookie,但 cookie 本身只包含一个密钥。服务器使用该密钥来获取该会话唯一的键/值对字典,该字典保留在服务器上(或序列化到数据库,具体取决于其配置方式)。

Yes, you can do that. You pass the encoded info in the userData field of the FormsAuthenticationTicket constructor:

  FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(version,
    name, issueDate, expirationDate, isPersistent, yourEncodedData);
  string secureTicket = FormsAuthentication.Encrypt(ticket);
  Response.Cookies.Add(
      new HttpCookie(FormsAuthentication.FormsCookieName, secureTicket));

Ideally, this should be done over an SSL connection, and the ticket cookie should be marked with both the HttpOnly and Secure attributes.

Then, to retrieve the value:

FormsIdentity id = (FormsIdentity)User.Identity;
FormsAuthenticationTicket ticket = id.Ticket;
string yourEncodedInfo = ticket.UserData;

You could also just set your own cookie, separate from the forms auth ticket.

However, storing a password directly in a cookie, even if encrypted, is not a good idea from a security perspective. Instead, use Session state:

Session["password"] = password;

Session state also uses a cookie, but the cookie itself only contains a key. The server uses the key to obtain a dictionary of key/value pairs unique to that session, which stay on the server (or get serialized to the DB, depending on how it's configured).

云朵有点甜 2024-12-29 18:00:33

不推荐,但您可以使用 FormsAuthenticationTicket.UserData。

Not recommended but you can use FormsAuthenticationTicket.UserData.

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