ASP.Net 在会话 cookie 中存储用户密码?
我知道会员资格提供商将用户名和过期时间存储在加密的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您应该将其存储在会话状态中,该状态永远不会离开服务器。
您还应该尝试更改这些 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.
是的,你可以这样做。您可以在 FormsAuthenticationTicket 构造函数的 userData 字段中传递编码信息:
理想情况下,这应该通过 SSL 连接完成,并且票证 cookie 应该使用 HttpOnly 和 Secure 属性进行标记。
然后,要检索该值:
您也可以设置自己的 cookie,与表单身份验证票证分开。
然而,从安全角度来看,即使加密,直接将密码存储在 cookie 中也不是一个好主意。相反,使用会话状态:
会话状态也使用 cookie,但 cookie 本身只包含一个密钥。服务器使用该密钥来获取该会话唯一的键/值对字典,该字典保留在服务器上(或序列化到数据库,具体取决于其配置方式)。
Yes, you can do that. You pass the encoded info in the userData field of the FormsAuthenticationTicket constructor:
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:
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 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).
不推荐,但您可以使用 FormsAuthenticationTicket.UserData。
Not recommended but you can use FormsAuthenticationTicket.UserData.