简体   繁体   English

使用会话变量有多安全 - asp.net/c#

[英]how safe is it to use session variables - asp.net / c#

So basically i'm wondering how safe is my way of using Session variables.所以基本上我想知道我使用 Session 变量的方式有多安全。

I have a login form where user types his username/password, it gets parametrized then queried, if username/password exists, then a userID is returned from db table.我有一个登录表单,用户输入他的用户名/密码,它被参数化然后查询,如果用户名/密码存在,则从 db 表返回一个用户 ID。 This is unique for every user.这对每个用户来说都是独一无二的。

when i have this value, this is where i'm wondering whether this way is safe way of storing the userID inside the session variable uID?当我有这个值时,这就是我想知道这种方式是否是将用户 ID 存储在会话变量 uID 中的安全方式? anyhow this is how i do it,无论如何,这就是我的做法,

Session["uID"] = (int)dt.DefaultView[0]["userID"];

FormsAuthentication.RedirectFromLoginPage(username.Text, false);

Response.Redirect("userPage.aspx", false);

then the page is redirected to another page where i use the session variable to fetch the users tables from the db.然后页面被重定向到另一个页面,在那里我使用会话变量从数据库中获取用户表。

Thanks in advance for your feedback提前感谢您的反馈

Session state is kept entirely server-side, no matter which storage method you use (in-memory, session state server or database).无论您使用哪种存储方法(内存、会话状态服务器或数据库),会话状态都完全保留在服务器端。

So unless your server is hacked, Session variables are safe.所以除非你的服务器被黑,否则会话变量是安全的。 And in case your server does get hacked, the hacker would only have access to the data in his own session, unless he finds a way to analyze the IIS process' memory.如果您的服务器确实被黑客入侵,黑客将只能访问他自己会话中的数据,除非他找到一种方法来分析 IIS 进程的内存。

Very safe, .NET session variables are not the same as cookie variables which can be viewed from the client side, Session variables in this instance are only accessible from the C# code.非常安全,.NET 会话变量与可以从客户端查看的 cookie 变量不同,此实例中的会话变量只能从 C# 代码访问。

So you can be safe in the knowledge that the Session variable can't be edited by anyone/thing other than the code running the background.因此,您可以放心,除了运行后台的代码之外,任何人/事物都无法编辑 Session 变量。

Not fully related to your question, but might be good to know in your case:与您的问题不完全相关,但在您的情况下可能很好了解:

You can also store a whole object in the Session, so you could store a user object in session such as您还可以在会话中存储整个对象,因此您可以在会话中存储用户对象,例如

user_Class user = new user_Class();
user.UID = 1;
Session["User"] = user;

Then you load it back in on load of each page.然后在加载每个页面时将其重新加载。

user_Class user = (user_Class)Session["User"];

Then you could get user.UID from session each time.然后您每次都可以从会话中获取 user.UID。

All good until your website outgrows a single server.一切都很好,直到您的网站超过单个服务器。 Then you have to migrate your session provider to a state server or back it off with sql server which ends up being a little sucky.然后,您必须将会话提供程序迁移到状态服务器或使用 sql server其关闭,这最终会变得有点糟糕。

See http://msdn.microsoft.com/en-us/library/ms178201%28v=vs.80%29.aspx for a comprehensive list of issues around session security.有关会话安全问题的完整列表,请参阅http://msdn.microsoft.com/en-us/library/ms178201%28v=vs.80%29.aspx

When it comes to sessions you can very well rest assured that the data is not directly accessible.当涉及到会话时,您可以非常放心,数据是不可直接访问的。 If for some reason your application ever returns data directly from the session that could potentially be exploited but there's seldom any reason to do this so the risk is fairly minimal.如果由于某种原因您的应用程序直接从可能被利用的会话中返回数据,但很少有任何理由这样做,因此风险相当小。

The riskiest part about sessions comes in the form of session hijacking.会话最危险的部分是会话劫持。 See, even though all your data is stored safely on the server we still have that whole "HTTP is stateless" issue to deal with.看,即使您的所有数据都安全地存储在服务器上,我们仍然需要处理整个“HTTP 无状态”问题。 So some kind of identifier has to be stored on the client so that the server can look up the proper session data.所以某种标识符必须存储在客户端上,以便服务器可以查找正确的会话数据。 But if somehow another system gets ahold of that ID then they can pretend to be you for as long as the server keeps the session active.但是,如果另一个系统以某种方式获得了该 ID,那么只要服务器保持会话处于活动状态,它们就可以假装是您。

Aside from continuously addressing any cross site scripting potential in your website there isn't really much you can do about this without a secure connection.除了不断解决您网站中的任何跨站点脚本潜力之外,如果没有安全连接,您实际上无能为力。 Even then it can be improperly implemented.即便如此,它也可能被不当实施。

You are still vulnerable even if YOU SERVER IS NOT COMPROMISED session can be easily hijack by using MITM Attack and when an attacker gets your session he can do anything what you can do.即使您的服务器没有受到损害,您仍然容易受到攻击,会话可以通过使用MITM Attack轻松劫持,并且当攻击者获得您的会话时,他可以做任何您可以做的事情。

You can use techniques to avoid session hijack but remember you are still vulnerable if there is a coding problem or etc which leave your application vulnerable.您可以使用技术来避免会话劫持,但请记住,如果存在编码问题或其他使您的应用程序易受攻击的问题,您仍然容易受到攻击。

Using SSL使用 SSL

SSL your site SSL您的网站

在此处输入图片说明

Generate Hash生成哈希

Protecting Session 保护会话

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

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